Skip to content

Commit

Permalink
Merge branch 'master' into reset-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
xepozz committed Feb 23, 2024
2 parents 684e8c0 + aeb0856 commit 9ddc47e
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 31 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.2.2 under development

- Enh #354: Add ability to use container as a factory (@xepozz)
- 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 @@ -539,8 +539,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 @@ -108,8 +108,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 @@ -485,7 +485,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 @@ -515,7 +515,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);
}
}
76 changes: 76 additions & 0 deletions tests/Unit/Reference/TagReferenceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?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());
if (PHP_VERSION_ID < 81000) {
$reflectionMethod->setAccessible(true);
}

$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);
}
}

0 comments on commit 9ddc47e

Please sign in to comment.