Skip to content

Add shortcut for tag reference #333 #353

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions src/CompositeContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 5 additions & 5 deletions src/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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) {
Expand Down
21 changes: 0 additions & 21 deletions src/Helpers/TagHelper.php

This file was deleted.

38 changes: 38 additions & 0 deletions src/Reference/TagReference.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Reference;

use Yiisoft\Definitions\Reference;

/**
* TagReference is a helper class that is 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
{
private const PREFIX = 'tag@';

private function __construct()
{
}

public static function to(string $tag): Reference
{
return Reference::to(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));
}
return substr($alias, 4);
}

public static function isTagAlias(string $id): bool
{
return str_starts_with($id, self::PREFIX);
}
}
73 changes: 73 additions & 0 deletions tests/Unit/Reference/TagReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Tests\Unit\Reference;

use Error;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Yiisoft\Di\Reference\TagReference;

final class TagReferenceTest extends TestCase
{
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'));
$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);
}
}