From ce6ed460c9a99a1e493e5c1e3fdadc0ad265272f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Wed, 14 Feb 2024 00:28:51 +0700 Subject: [PATCH] Add shortcut for tag reference #333 --- src/CompositeContainer.php | 4 +- src/Container.php | 10 ++--- src/Helpers/TagHelper.php | 21 --------- src/Reference/TagReference.php | 38 ++++++++++++++++ tests/Unit/Reference/TagReferenceTest.php | 54 +++++++++++++++++++++++ 5 files changed, 99 insertions(+), 28 deletions(-) delete mode 100644 src/Helpers/TagHelper.php create mode 100644 src/Reference/TagReference.php create mode 100644 tests/Unit/Reference/TagReferenceTest.php diff --git a/src/CompositeContainer.php b/src/CompositeContainer.php index b921bc0f..f3d99e29 100644 --- a/src/CompositeContainer.php +++ b/src/CompositeContainer.php @@ -8,7 +8,7 @@ use Psr\Container\ContainerInterface; use RuntimeException; use Throwable; -use Yiisoft\Di\Helpers\TagHelper; +use Yiisoft\Di\Reference\TagReference; use function is_string; @@ -54,7 +54,7 @@ public function get($id) return $stateResetter; } - if (TagHelper::isTagAlias($id)) { + if (TagReference::isTagAlias($id)) { $tags = []; foreach ($this->containers as $container) { if (!$container instanceof Container) { diff --git a/src/Container.php b/src/Container.php index 45065e0a..f29df055 100644 --- a/src/Container.php +++ b/src/Container.php @@ -17,7 +17,7 @@ use Yiisoft\Definitions\Helpers\DefinitionValidator; use Yiisoft\Di\Helpers\DefinitionNormalizer; use Yiisoft\Di\Helpers\DefinitionParser; -use Yiisoft\Di\Helpers\TagHelper; +use Yiisoft\Di\Reference\TagReference; use function array_key_exists; use function array_keys; @@ -101,8 +101,8 @@ public function __construct(ContainerConfigInterface $config) */ public function has(string $id): bool { - if (TagHelper::isTagAlias($id)) { - $tag = TagHelper::extractTagFromAlias($id); + if (TagReference::isTagAlias($id)) { + $tag = TagReference::extractTagFromAlias($id); return isset($this->tags[$tag]); } @@ -472,7 +472,7 @@ private function addDefinitionToStorage(string $id, $definition): void */ private function build(string $id) { - if (TagHelper::isTagAlias($id)) { + if (TagReference::isTagAlias($id)) { return $this->getTaggedServices($id); } @@ -502,7 +502,7 @@ private function build(string $id) private function getTaggedServices(string $tagAlias): array { - $tag = TagHelper::extractTagFromAlias($tagAlias); + $tag = TagReference::extractTagFromAlias($tagAlias); $services = []; if (isset($this->tags[$tag])) { foreach ($this->tags[$tag] as $service) { diff --git a/src/Helpers/TagHelper.php b/src/Helpers/TagHelper.php deleted file mode 100644 index d38c089e..00000000 --- a/src/Helpers/TagHelper.php +++ /dev/null @@ -1,21 +0,0 @@ -assertFalse(TagReference::isTagAlias('test')); + $this->assertFalse(TagReference::isTagAlias('tag#test')); + $this->assertTrue(TagReference::isTagAlias('tag@test')); + } + + public function testExtractTag(): void + { + $this->assertEquals('test', TagReference::extractTagFromAlias('tag@test')); + } + + public function testExtractWrongTag(): void + { + $this->expectException(InvalidArgumentException::class); + TagReference::extractTagFromAlias('test'); + + $this->expectException(InvalidArgumentException::class); + TagReference::extractTagFromAlias('tag#test'); + } + + public function testReference(): void + { + $reference = TagReference::to('test'); + $spyContainer = new class implements ContainerInterface { + public function get($id) + { + return $id; + } + + public function has($id): bool + { + return true; + } + }; + + $result = $reference->resolve($spyContainer); + + $this->assertEquals('tag@test', $result); + } +}