Skip to content

[DependencyInjection] Make #[AsTaggedItem] repeatable #59088

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 1 commit into from
Jan 2, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*
* @author Nicolas Grekas <p@tchwork.com>
*/
#[\Attribute(\Attribute::TARGET_CLASS)]
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::IS_REPEATABLE)]
class AsTaggedItem
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/DependencyInjection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

7.3
---

* Make `#[AsTaggedItem]` repeatable

7.2
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam

$services[] = [$priority, ++$i, $index, $serviceId, $class];
}

if ($class) {
$attributes = (new \ReflectionClass($class))->getAttributes(AsTaggedItem::class);
$attributeCount = \count($attributes);

foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();

if (!$instance->index && 1 < $attributeCount) {
throw new InvalidArgumentException(\sprintf('Attribute "%s" on class "%s" cannot have an empty index when repeated.', AsTaggedItem::class, $class));
}

$services[] = [$instance->priority ?? 0, ++$i, $instance->index ?? $serviceId, $serviceId, $class];
}
}
}

uasort($services, static fn ($a, $b) => $b[0] <=> $a[0] ?: $a[1] <=> $b[1]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ public function testTaggedItemAttributes()
$container->register('service5', HelloNamedService2::class)
->setAutoconfigured(true)
->addTag('my_custom_tag');
$container->register('service6', MultiTagHelloNamedService::class)
->setAutoconfigured(true)
->addTag('my_custom_tag');

(new ResolveInstanceofConditionalsPass())->process($container);

Expand All @@ -226,14 +229,33 @@ public function testTaggedItemAttributes()
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar', exclude: ['service4', 'service5']);
$expected = [
'service3' => new TypedReference('service3', HelloNamedService2::class),
'multi_hello_2' => new TypedReference('service6', MultiTagHelloNamedService::class),
'hello' => new TypedReference('service2', HelloNamedService::class),
'multi_hello_1' => new TypedReference('service6', MultiTagHelloNamedService::class),
'service1' => new TypedReference('service1', FooTagClass::class),
];

$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
$this->assertSame(array_keys($expected), array_keys($services));
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
}

public function testTaggedItemAttributesRepeatedWithoutNameThrows()
{
$container = new ContainerBuilder();
$container->register('service1', MultiNoNameTagHelloNamedService::class)
->setAutoconfigured(true)
->addTag('my_custom_tag');

(new ResolveInstanceofConditionalsPass())->process($container);
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar', exclude: ['service4', 'service5']);

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Attribute "Symfony\Component\DependencyInjection\Attribute\AsTaggedItem" on class "Symfony\Component\DependencyInjection\Tests\Compiler\MultiNoNameTagHelloNamedService" cannot have an empty index when repeated.');

(new PriorityTaggedServiceTraitImplementation())->test($tag, $container);
}

public function testResolveIndexedTags()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -283,6 +305,18 @@ class HelloNamedService2
{
}

#[AsTaggedItem(index: 'multi_hello_1', priority: 1)]
#[AsTaggedItem(index: 'multi_hello_2', priority: 2)]
class MultiTagHelloNamedService
{
}

#[AsTaggedItem(priority: 1)]
#[AsTaggedItem(priority: 2)]
class MultiNoNameTagHelloNamedService
{
}

interface HelloInterface
{
public static function getFooBar(): string;
Expand Down
Loading