Skip to content
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

Add TagReference::makeId() method + Improve docs #380

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all 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.3.1 under development

- no changes in this release.
- New #380: Add `TagReference::id()` method (@vjik)

## 1.3.0 October 14, 2024

Expand Down
32 changes: 24 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 11 additions & 3 deletions src/Reference/TagReference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
}
Expand Down
9 changes: 9 additions & 0 deletions tests/Unit/Reference/TagReference/Resolve/A.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Tests\Unit\Reference\TagReference\Resolve;

final class A
{
}
9 changes: 9 additions & 0 deletions tests/Unit/Reference/TagReference/Resolve/B.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Tests\Unit\Reference\TagReference\Resolve;

final class B
{
}
10 changes: 10 additions & 0 deletions tests/Unit/Reference/TagReference/Resolve/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Tests\Unit\Reference\TagReference\Resolve;

final class Main
{
public array $data = [];
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Yiisoft\Di\Tests\Unit\Reference\TagReference\Resolve;

use PHPUnit\Framework\TestCase;
use Yiisoft\Di\Container;
use Yiisoft\Di\ContainerConfig;
use Yiisoft\Di\Reference\TagReference;

final class TagReferenceResolveTest extends TestCase
{
public function testBase(): void
{
$container = new Container(
ContainerConfig::create()
->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]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'));
}
}
Loading