From cc94a9748cddf622cef03e47dd16faa5b5df8c31 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Mon, 4 Nov 2024 18:01:18 +0300 Subject: [PATCH 1/8] Add `TagReference::makeId()` method + Improve docs --- CHANGELOG.md | 2 +- README.md | 32 ++++++++++++----- src/Reference/TagReference.php | 14 ++++++-- .../Unit/Reference/TagReference/Resolve/A.php | 9 +++++ .../Unit/Reference/TagReference/Resolve/B.php | 9 +++++ .../Reference/TagReference/Resolve/Main.php | 10 ++++++ .../Resolve/TagReferenceResolveTest.php | 34 +++++++++++++++++++ .../{ => TagReference}/TagReferenceTest.php | 7 +++- 8 files changed, 104 insertions(+), 13 deletions(-) create mode 100644 tests/Unit/Reference/TagReference/Resolve/A.php create mode 100644 tests/Unit/Reference/TagReference/Resolve/B.php create mode 100644 tests/Unit/Reference/TagReference/Resolve/Main.php create mode 100644 tests/Unit/Reference/TagReference/Resolve/TagReferenceResolveTest.php rename tests/Unit/Reference/{ => TagReference}/TagReferenceTest.php (91%) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3860f1c6..ad24c5b7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.3.1 under development -- no changes in this release. +- New #380: Add `TagReference::makeId()` method (@vjik) ## 1.3.0 October 14, 2024 diff --git a/README.md b/README.md index cff1b2ba..d44607c7 100644 --- a/README.md +++ b/README.md @@ -338,14 +338,6 @@ $config = ContainerConfig::create() $container = new Container($config); ``` -Now you can get tagged services from the container in the following way: - -```php -$container->get(\Yiisoft\Di\Reference\TagReference::to('car')); -``` - -The result is an array that has two instances: `BlueCarService` and `RedCarService`. - Another way to tag services is setting tags via container constructor: ```php @@ -367,6 +359,30 @@ $config = ContainerConfig::create() $container = new Container($config); ``` +### Tagged services getting + +Now you can get tagged services from the container in the following way: + +```php +$container->get(\Yiisoft\Di\Reference\TagReference::makeId('car')); +``` + +The result is an array that has two instances: `BlueCarService` and `RedCarService`. + +### Usage tagged services in configuration + +Use `TagReference` to get tagged services in configuration: + +```php +[ + Garage::class => [ + '__construct()' => [ + \Yiisoft\Di\Reference\TagReference::to('car'), + ], + ], +], +``` + ## Resetting services state Despite stateful services isn't a great practice, these are often inevitable. When you build long-running diff --git a/src/Reference/TagReference.php b/src/Reference/TagReference.php index f0322047..31753d70 100644 --- a/src/Reference/TagReference.php +++ b/src/Reference/TagReference.php @@ -4,10 +4,13 @@ namespace Yiisoft\Di\Reference; +use InvalidArgumentException; use Yiisoft\Definitions\Reference; +use function sprintf; + /** - * TagReference is a helper class used to specify a reference to a tag. + * `TagReference` is a helper class used to specify a reference to a tag. * For example, `TagReference::to('my-tag')` specifies a reference to all services that are tagged with `tag@my-tag`. */ final class TagReference @@ -20,13 +23,18 @@ private function __construct() public static function to(string $tag): Reference { - return Reference::to(self::PREFIX . $tag); + return Reference::to(self::makeId($tag)); + } + + public static function makeId(string $tag): string + { + return self::PREFIX . $tag; } public static function extractTagFromAlias(string $alias): string { if (!str_starts_with($alias, self::PREFIX)) { - throw new \InvalidArgumentException(sprintf('Alias "%s" is not a tag alias.', $alias)); + throw new InvalidArgumentException(sprintf('Alias "%s" is not a tag alias.', $alias)); } return substr($alias, 4); } diff --git a/tests/Unit/Reference/TagReference/Resolve/A.php b/tests/Unit/Reference/TagReference/Resolve/A.php new file mode 100644 index 00000000..c390fddc --- /dev/null +++ b/tests/Unit/Reference/TagReference/Resolve/A.php @@ -0,0 +1,9 @@ +withDefinitions([ + Main::class => [ + '$data' => TagReference::to('letters') + ] + ]) + ->withTags([ + 'letters' => [A::class, B::class], + ]) + ); + + $main = $container->get(Main::class); + + $this->assertCount(2, $main->data); + $this->assertInstanceOf(A::class, $main->data[0]); + $this->assertInstanceOf(B::class, $main->data[1]); + } +} diff --git a/tests/Unit/Reference/TagReferenceTest.php b/tests/Unit/Reference/TagReference/TagReferenceTest.php similarity index 91% rename from tests/Unit/Reference/TagReferenceTest.php rename to tests/Unit/Reference/TagReference/TagReferenceTest.php index f1237da4..89a9ca1c 100644 --- a/tests/Unit/Reference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReference/TagReferenceTest.php @@ -2,7 +2,7 @@ declare(strict_types=1); -namespace Yiisoft\Di\Tests\Unit\Reference; +namespace Yiisoft\Di\Tests\Unit\Reference\TagReference; use Error; use InvalidArgumentException; @@ -69,4 +69,9 @@ public function has($id): bool $this->assertEquals('tag@test', $result); } + + public function testMakeId(): void + { + $this->assertSame('tag@test', TagReference::makeId('test')); + } } From 51afbcf99707a6884fe91218f23c303355bed7c5 Mon Sep 17 00:00:00 2001 From: StyleCI Bot Date: Mon, 4 Nov 2024 15:01:47 +0000 Subject: [PATCH 2/8] Apply fixes from StyleCI --- .../TagReference/Resolve/TagReferenceResolveTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Unit/Reference/TagReference/Resolve/TagReferenceResolveTest.php b/tests/Unit/Reference/TagReference/Resolve/TagReferenceResolveTest.php index a966f089..3cab17b0 100644 --- a/tests/Unit/Reference/TagReference/Resolve/TagReferenceResolveTest.php +++ b/tests/Unit/Reference/TagReference/Resolve/TagReferenceResolveTest.php @@ -17,8 +17,8 @@ public function testBase(): void ContainerConfig::create() ->withDefinitions([ Main::class => [ - '$data' => TagReference::to('letters') - ] + '$data' => TagReference::to('letters'), + ], ]) ->withTags([ 'letters' => [A::class, B::class], From 33e153bfaaede50c5604f1559e61def2f29933f0 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 11:20:03 +0300 Subject: [PATCH 3/8] Update README.md Co-authored-by: Alexey Rogachev --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index d44607c7..9eb88f26 100644 --- a/README.md +++ b/README.md @@ -359,7 +359,7 @@ $config = ContainerConfig::create() $container = new Container($config); ``` -### Tagged services getting +### Getting tagged services Now you can get tagged services from the container in the following way: From c2cc06b1d99155a15a9171a8fd1e80252b7364d1 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 11:20:09 +0300 Subject: [PATCH 4/8] Update README.md Co-authored-by: Alexey Rogachev --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9eb88f26..dd8fc8f8 100644 --- a/README.md +++ b/README.md @@ -369,7 +369,7 @@ $container->get(\Yiisoft\Di\Reference\TagReference::makeId('car')); The result is an array that has two instances: `BlueCarService` and `RedCarService`. -### Usage tagged services in configuration +### Using tagged services in configuration Use `TagReference` to get tagged services in configuration: From 98cd88d04dfd7e7bb0bf483d4d1ed75768727233 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 11:20:18 +0300 Subject: [PATCH 5/8] Update README.md Co-authored-by: Alexey Rogachev --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index dd8fc8f8..8708df31 100644 --- a/README.md +++ b/README.md @@ -361,7 +361,7 @@ $container = new Container($config); ### Getting tagged services -Now you can get tagged services from the container in the following way: +You can get tagged services from the container in the following way: ```php $container->get(\Yiisoft\Di\Reference\TagReference::makeId('car')); From 66951c82a68b42605a97ba91ebc87643e3523cd8 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 11:20:25 +0300 Subject: [PATCH 6/8] Update src/Reference/TagReference.php Co-authored-by: Alexey Rogachev --- src/Reference/TagReference.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reference/TagReference.php b/src/Reference/TagReference.php index 31753d70..ec09a3e9 100644 --- a/src/Reference/TagReference.php +++ b/src/Reference/TagReference.php @@ -10,7 +10,7 @@ use function sprintf; /** - * `TagReference` is a helper class used to specify a reference to a tag. + * Helper class used to specify a reference to a tag. * For example, `TagReference::to('my-tag')` specifies a reference to all services that are tagged with `tag@my-tag`. */ final class TagReference From 2564009e4d207a42461c7e464a64815e6e609031 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 11:20:34 +0300 Subject: [PATCH 7/8] Update src/Reference/TagReference.php Co-authored-by: Alexander Makarov --- src/Reference/TagReference.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Reference/TagReference.php b/src/Reference/TagReference.php index ec09a3e9..24d8a6bc 100644 --- a/src/Reference/TagReference.php +++ b/src/Reference/TagReference.php @@ -26,7 +26,7 @@ public static function to(string $tag): Reference return Reference::to(self::makeId($tag)); } - public static function makeId(string $tag): string + public static function id(string $tag): string { return self::PREFIX . $tag; } From 7bab0ce6946a316ea8216ce3cfd85c6bdf911093 Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 11:22:31 +0300 Subject: [PATCH 8/8] fix --- CHANGELOG.md | 2 +- README.md | 2 +- src/Reference/TagReference.php | 2 +- tests/Unit/Reference/TagReference/TagReferenceTest.php | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad24c5b7..00d33475 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,7 +2,7 @@ ## 1.3.1 under development -- New #380: Add `TagReference::makeId()` method (@vjik) +- New #380: Add `TagReference::id()` method (@vjik) ## 1.3.0 October 14, 2024 diff --git a/README.md b/README.md index 8708df31..0262e793 100644 --- a/README.md +++ b/README.md @@ -364,7 +364,7 @@ $container = new Container($config); You can get tagged services from the container in the following way: ```php -$container->get(\Yiisoft\Di\Reference\TagReference::makeId('car')); +$container->get(\Yiisoft\Di\Reference\TagReference::id('car')); ``` The result is an array that has two instances: `BlueCarService` and `RedCarService`. diff --git a/src/Reference/TagReference.php b/src/Reference/TagReference.php index 24d8a6bc..52a2af11 100644 --- a/src/Reference/TagReference.php +++ b/src/Reference/TagReference.php @@ -23,7 +23,7 @@ private function __construct() public static function to(string $tag): Reference { - return Reference::to(self::makeId($tag)); + return Reference::to(self::id($tag)); } public static function id(string $tag): string diff --git a/tests/Unit/Reference/TagReference/TagReferenceTest.php b/tests/Unit/Reference/TagReference/TagReferenceTest.php index 89a9ca1c..ff66d5d8 100644 --- a/tests/Unit/Reference/TagReference/TagReferenceTest.php +++ b/tests/Unit/Reference/TagReference/TagReferenceTest.php @@ -70,8 +70,8 @@ public function has($id): bool $this->assertEquals('tag@test', $result); } - public function testMakeId(): void + public function testId(): void { - $this->assertSame('tag@test', TagReference::makeId('test')); + $this->assertSame('tag@test', TagReference::id('test')); } }