Skip to content

Commit

Permalink
Add shortcut for tag reference #333
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 13, 2024
1 parent d6ab9e7 commit ce6ed46
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 28 deletions.
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);
}
}
54 changes: 54 additions & 0 deletions tests/Unit/Reference/TagReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Tests\Unit\Reference;

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

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

0 comments on commit ce6ed46

Please sign in to comment.