Skip to content

Commit fd0912a

Browse files
authored
fix(container): allow enums in the #[Tag] attribute (#1506)
1 parent e8c60ea commit fd0912a

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-3
lines changed

packages/container/src/Exceptions/TaggedDependencyCouldNotBeResolved.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44

55
namespace Tempest\Container\Exceptions;
66

7+
use BackedEnum;
78
use Exception;
89
use Tempest\Container\Dependency;
910
use Tempest\Container\DependencyChain;
11+
use UnitEnum;
1012

1113
final class TaggedDependencyCouldNotBeResolved extends Exception implements ContainerException
1214
{
13-
public function __construct(DependencyChain $chain, Dependency $brokenDependency, string $tag)
15+
public function __construct(DependencyChain $chain, Dependency $brokenDependency, string|UnitEnum $tag)
1416
{
1517
$stack = $chain->all();
1618
$stack[] = $brokenDependency;
1719

20+
$tag = match (true) {
21+
is_string($tag) => $tag,
22+
$tag instanceof BackedEnum => (string) $tag->value,
23+
$tag instanceof UnitEnum => $tag->name,
24+
};
25+
1826
$message = PHP_EOL . PHP_EOL . "Could not resolve tagged dependency {$brokenDependency->getName()}#{$tag}, did you forget to define an initializer for it?" . PHP_EOL;
1927

2028
if (count($stack) < 2) {

packages/container/src/Tag.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
namespace Tempest\Container;
66

77
use Attribute;
8+
use UnitEnum;
89

910
#[Attribute]
1011
final readonly class Tag
1112
{
1213
public function __construct(
13-
public string $name,
14+
public string|UnitEnum $name,
1415
) {}
1516
}

packages/container/tests/ContainerTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Tempest\Container\Exceptions\InvokedCallableWasInvalid;
1313
use Tempest\Container\Exceptions\TaggedDependencyCouldNotBeResolved;
1414
use Tempest\Container\GenericContainer;
15+
use Tempest\Container\Tests\Fixtures\AutowireWithEnumTags;
1516
use Tempest\Container\Tests\Fixtures\BuiltinArrayClass;
1617
use Tempest\Container\Tests\Fixtures\BuiltinDependencyArrayInitializer;
1718
use Tempest\Container\Tests\Fixtures\BuiltinDependencyBoolInitializer;
@@ -609,4 +610,26 @@ public function test_has_tags_support(): void
609610
$this->assertSame('A', $container->get(HasTagObject::class, 'tagA')->name);
610611
$this->assertSame('B', $container->get(HasTagObject::class, 'tagB')->name);
611612
}
613+
614+
public function test_tag_attribute_with_enum(): void
615+
{
616+
$container = new GenericContainer();
617+
618+
$container->singleton(
619+
TaggedDependency::class,
620+
new TaggedDependency('foo'),
621+
tag: EnumTag::FOO,
622+
);
623+
624+
$container->singleton(
625+
TaggedDependency::class,
626+
new TaggedDependency('bar'),
627+
tag: EnumTag::BAR,
628+
);
629+
630+
$dependency = $container->get(AutowireWithEnumTags::class);
631+
632+
$this->assertSame('foo', $dependency->foo->name);
633+
$this->assertSame('bar', $dependency->bar->name);
634+
}
612635
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Tempest\Container\Tests\Fixtures;
4+
5+
use Tempest\Container\Tag;
6+
7+
class AutowireWithEnumTags
8+
{
9+
public function __construct(
10+
#[Tag(EnumTag::FOO)]
11+
public TaggedDependency $foo,
12+
#[Tag(EnumTag::BAR)]
13+
public TaggedDependency $bar,
14+
) {}
15+
}

tests/Integration/Core/DiscoveryCacheTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ final class DiscoveryCacheTest extends FrameworkIntegrationTestCase
1414
{
1515
public function test_exception_with_unserializable_discovery_items(): void
1616
{
17-
$this->assertException(CouldNotStoreDiscoveryCache::class, function () {
17+
$this->assertException(CouldNotStoreDiscoveryCache::class, function (): void {
1818
$discoveryCache = $this->container->get(DiscoveryCache::class);
1919

2020
$location = new DiscoveryLocation('Test\\', '.');

0 commit comments

Comments
 (0)