diff --git a/CHANGELOG.md b/CHANGELOG.md index 8efe2c73..1fe65926 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## 1.2.2 under development - Enh #354: Add ability to use container as a factory (@xepozz) +- Enh #353: Add shortcut for tag reference #333 (@xepozz) ## 1.2.1 December 23, 2022 diff --git a/README.md b/README.md index 787f1439..614b5dd5 100644 --- a/README.md +++ b/README.md @@ -315,7 +315,7 @@ $container = new Container($config); Now you can get tagged services from the container in the following way: ```php -$container->get('tag@car'); +$container->get(\Yiisoft\Di\Reference\TagReference::to('car')); ``` The result is an array that has two instances: `BlueCarService` and `RedCarService`. @@ -539,8 +539,9 @@ $container = new Container($config); To run benchmarks execute the next command -`composer require phpbench/phpbench` -`$ ./vendor/bin/phpbench run` +```shell +./vendor/bin/phpbench run +``` Result example 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 f56ab05e..971c7484 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; @@ -108,8 +108,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]); } @@ -485,7 +485,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); } @@ -515,7 +515,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 @@ -expectException(Error::class); + new TagReference(); + } + + public function testConstructor(): void + { + $reflection = new \ReflectionClass(TagReference::class); + $reflectionMethod = $reflection->getConstructor(); + $this->assertTrue($reflectionMethod->isPrivate()); + if (PHP_VERSION_ID < 81000) { + $reflectionMethod->setAccessible(true); + } + + $reflectionMethod->invoke($reflection->newInstanceWithoutConstructor()); + } + + public function testAliases(): void + { + $this->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 testExtractWrongTagDelimiter(): void + { + $this->expectException(InvalidArgumentException::class); + TagReference::extractTagFromAlias('tag#test'); + } + + public function testExtractWrongTagFormat(): void + { + $this->expectException(InvalidArgumentException::class); + TagReference::extractTagFromAlias('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); + } +}