Skip to content
Closed
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 @@ -67,44 +67,43 @@ private function findAndSortTaggedServices(string|TaggedIteratorArgument $tagNam
$reflector = null !== $class ? $container->getReflectionClass($class) : null;
$checkTaggedItem = !$definition->hasTag($definition->isAutoconfigured() ? 'container.ignore_attributes' : $tagName);

foreach ($attributes as $attribute) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This loop is about looping over the various tags added explicitly in the service definition. Anything that skips this loop is broken by design. Explicit tag attributes must win over the AsTaggedItem attribute providing autoconfigured fallback values (see the priority of values in PriorityTaggedServiceUtil::getDefault())

$index = $priority = null;
$asTaggedItemAttributes = $reflector && $checkTaggedItem ? $reflector->getAttributes(AsTaggedItem::class) : [];

if (isset($attribute['priority'])) {
$priority = $attribute['priority'];
} elseif (null === $defaultPriority && $defaultPriorityMethod && $reflector) {
$defaultPriority = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultPriorityMethod, $tagName, 'priority', $checkTaggedItem);
}
$priority ??= $defaultPriority ??= 0;
if (1 < \count($asTaggedItemAttributes)) {
foreach ($asTaggedItemAttributes as $attribute) {
$instance = $attribute->newInstance();

if (null === $indexAttribute && !$defaultIndexMethod && !$needsIndexes) {
$services[] = [$priority, ++$i, null, $serviceId, null];
continue 2;
}
if (!$instance->index) {
throw new InvalidArgumentException(\sprintf('Attribute "%s" on class "%s" cannot have an empty index when repeated.', AsTaggedItem::class, $class));
}

if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
$index = $parameterBag->resolveValue($attribute[$indexAttribute]);
}
if (null === $index && null === $defaultIndex && $defaultPriorityMethod && $reflector) {
$defaultIndex = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
$services[] = [$instance->priority ?? 0, ++$i, $instance->index, $serviceId, $class];
}
$index ??= $defaultIndex ??= $definition->getTag('container.decorator')[0]['id'] ?? $serviceId;

$services[] = [$priority, ++$i, $index, $serviceId, $class];
}
} else {
foreach ($attributes as $attribute) {
$index = $priority = null;

if ($reflector) {
$attributes = $reflector->getAttributes(AsTaggedItem::class);
$attributeCount = \count($attributes);
if (isset($attribute['priority'])) {
$priority = $attribute['priority'];
} elseif (null === $defaultPriority && $defaultPriorityMethod && $reflector) {
$defaultPriority = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultPriorityMethod, $tagName, 'priority', $checkTaggedItem);
}
$priority ??= $defaultPriority ??= 0;

foreach ($attributes as $attribute) {
$instance = $attribute->newInstance();
if (null === $indexAttribute && !$defaultIndexMethod && !$needsIndexes) {
$services[] = [$priority, ++$i, null, $serviceId, null];
continue 2;
}

if (!$instance->index && 1 < $attributeCount) {
throw new InvalidArgumentException(\sprintf('Attribute "%s" on class "%s" cannot have an empty index when repeated.', AsTaggedItem::class, $class));
if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
$index = $parameterBag->resolveValue($attribute[$indexAttribute]);
}
if (null === $index && null === $defaultIndex && $defaultPriorityMethod && $reflector) {
$defaultIndex = PriorityTaggedServiceUtil::getDefault($serviceId, $reflector, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
}
$index ??= $defaultIndex ??= $definition->getTag('container.decorator')[0]['id'] ?? $serviceId;

$services[] = [$instance->priority ?? 0, ++$i, $instance->index ?? $serviceId, $serviceId, $class];
$services[] = [$priority, ++$i, $index, $serviceId, $class];
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,46 @@ public function testTaggedItemAttributesRepeatedWithoutNameThrows()
(new PriorityTaggedServiceTraitImplementation())->test($tag, $container);
}

public function testIgnoreAttributesIsRespected()
{
$container = new ContainerBuilder();

$container->register('service1', SingleTaggedItemService::class)
->setAutoconfigured(true)
->addTag('my_custom_tag')
->addTag('container.ignore_attributes');

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

$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar');

$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);

$this->assertArrayHasKey('service1', $services);
$this->assertArrayNotHasKey('single_hello', $services);
$this->assertCount(1, $services);
}

public function testSingleAsTaggedItemDoesNotDuplicate()
{
$container = new ContainerBuilder();

$container->register('service1', SingleTaggedItemService::class)
->setAutoconfigured(true)
->addTag('my_custom_tag');

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

$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar');

$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);

$this->assertArrayHasKey('single_hello', $services);
$this->assertCount(1, $services);
}

public function testResolveIndexedTags()
{
$container = new ContainerBuilder();
Expand Down Expand Up @@ -317,6 +357,11 @@ class MultiNoNameTagHelloNamedService
{
}

#[AsTaggedItem(index: 'single_hello', priority: 5)]
class SingleTaggedItemService
{
}

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