From 4faab46e46d9ea5ba37e0fe643b5e01c88b86e3c Mon Sep 17 00:00:00 2001 From: Sergei Predvoditelev Date: Tue, 5 Nov 2024 14:04:05 +0300 Subject: [PATCH] Add `TagReference::id()` method + Improve docs (#380) * Add `TagReference::makeId()` method + Improve docs * Apply fixes from StyleCI * Update README.md Co-authored-by: Alexey Rogachev * Update README.md Co-authored-by: Alexey Rogachev * Update README.md Co-authored-by: Alexey Rogachev * Update src/Reference/TagReference.php Co-authored-by: Alexey Rogachev * Update src/Reference/TagReference.php Co-authored-by: Alexander Makarov * fix --------- Co-authored-by: StyleCI Bot Co-authored-by: Alexey Rogachev Co-authored-by: Alexander Makarov --- 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..00d33475 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::id()` method (@vjik) ## 1.3.0 October 14, 2024 diff --git a/README.md b/README.md index cff1b2ba..0262e793 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); ``` +### Getting tagged services + +You can get tagged services from the container in the following way: + +```php +$container->get(\Yiisoft\Di\Reference\TagReference::id('car')); +``` + +The result is an array that has two instances: `BlueCarService` and `RedCarService`. + +### Using 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..52a2af11 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. + * 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::id($tag)); + } + + public static function id(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..ff66d5d8 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 testId(): void + { + $this->assertSame('tag@test', TagReference::id('test')); + } }