From ce6ed460c9a99a1e493e5c1e3fdadc0ad265272f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Wed, 14 Feb 2024 00:28:51 +0700 Subject: [PATCH 1/9] 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); + } +} From 51f6980624de8df462ea5b478a60d34e1beeb1d1 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Tue, 13 Feb 2024 17:29:11 +0000 Subject: [PATCH 2/9] Apply fixes from StyleCI --- tests/Unit/Reference/TagReferenceTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReferenceTest.php index 46864937..42754443 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReferenceTest.php @@ -35,7 +35,7 @@ public function testExtractWrongTag(): void public function testReference(): void { $reference = TagReference::to('test'); - $spyContainer = new class implements ContainerInterface { + $spyContainer = new class () implements ContainerInterface { public function get($id) { return $id; From 8756a9d5d69a71dc571cf8e6779813df9e28ea0f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 23 Feb 2024 13:32:58 +0700 Subject: [PATCH 3/9] Split test onto two ones --- tests/Unit/Reference/TagReferenceTest.php | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReferenceTest.php index 42754443..7f303e34 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReferenceTest.php @@ -23,13 +23,16 @@ public function testExtractTag(): void $this->assertEquals('test', TagReference::extractTagFromAlias('tag@test')); } - public function testExtractWrongTag(): void + public function testExtractWrongTagDelimiter(): void { $this->expectException(InvalidArgumentException::class); - TagReference::extractTagFromAlias('test'); + TagReference::extractTagFromAlias('tag#test'); + } + public function testExtractWrongTagFormat(): void + { $this->expectException(InvalidArgumentException::class); - TagReference::extractTagFromAlias('tag#test'); + TagReference::extractTagFromAlias('test'); } public function testReference(): void From efd5fffbd390ef162d3facb72ea3cd9cf06b00c5 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 23 Feb 2024 13:39:27 +0700 Subject: [PATCH 4/9] Add CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c11cac6d..76121f14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.2.2 under development -- no changes in this release. +- Enh #353: Add shortcut for tag reference #333 (@xepozz) ## 1.2.1 December 23, 2022 From 8e43aa2ed0b1efe642e418102bf32f751253dbbb Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 23 Feb 2024 13:40:39 +0700 Subject: [PATCH 5/9] Add README.md line --- README.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 0e8a3665..e2a2f0fc 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`. @@ -504,8 +504,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 From ff016215c0ca55fcfe772bf99fefed877ba3e1e0 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 23 Feb 2024 13:44:16 +0700 Subject: [PATCH 6/9] Add test --- tests/Unit/Reference/TagReferenceTest.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReferenceTest.php index 7f303e34..a571caa5 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReferenceTest.php @@ -4,6 +4,8 @@ namespace Yiisoft\Di\Tests\Unit\Reference; +use Error; +use Exception; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; @@ -11,6 +13,12 @@ final class TagReferenceTest extends TestCase { + public function testClosedConstructor(): void + { + $this->expectException(Error::class); + new TagReference(); + } + public function testAliases(): void { $this->assertFalse(TagReference::isTagAlias('test')); From c883425c2540e893b7b31bb05a0712f9c3d6f9bd Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Fri, 23 Feb 2024 06:44:27 +0000 Subject: [PATCH 7/9] Apply fixes from StyleCI --- tests/Unit/Reference/TagReferenceTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReferenceTest.php index a571caa5..ec2e131b 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReferenceTest.php @@ -5,7 +5,6 @@ namespace Yiisoft\Di\Tests\Unit\Reference; use Error; -use Exception; use InvalidArgumentException; use PHPUnit\Framework\TestCase; use Psr\Container\ContainerInterface; From 5dac0168e1992393a9d0d967836c9b6bb1773ea3 Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 23 Feb 2024 13:56:30 +0700 Subject: [PATCH 8/9] Cover private constructor --- tests/Unit/Reference/TagReferenceTest.php | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReferenceTest.php index ec2e131b..e255b363 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReferenceTest.php @@ -12,12 +12,21 @@ final class TagReferenceTest extends TestCase { - public function testClosedConstructor(): void + public function testConstructorIsPrivate(): void { $this->expectException(Error::class); new TagReference(); } + public function testConstructor(): void + { + $reflection = new \ReflectionClass(TagReference::class); + $reflectionMethod = $reflection->getConstructor(); + $this->assertTrue($reflectionMethod->isPrivate()); + + $reflectionMethod->invoke($reflection->newInstanceWithoutConstructor()); + } + public function testAliases(): void { $this->assertFalse(TagReference::isTagAlias('test')); From 0be02f67880500b8ecc6d35b7dbc3687f6301e1f Mon Sep 17 00:00:00 2001 From: Dmitrii Derepko Date: Fri, 23 Feb 2024 14:16:57 +0700 Subject: [PATCH 9/9] Fix test --- tests/Unit/Reference/TagReferenceTest.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReferenceTest.php index e255b363..9abfe046 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReferenceTest.php @@ -23,6 +23,9 @@ 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()); }