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 @@ -62,25 +62,26 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
$class = $container->getParameterBag()->resolveValue($class) ?: null;
$checkTaggedItem = !$definition->hasTag(80000 <= \PHP_VERSION_ID && $definition->isAutoconfigured() ? 'container.ignore_attributes' : $tagName);

if ($class) {
$defaultPriority = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultPriorityMethod, $tagName, 'priority', $checkTaggedItem);
$defaultIndex = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
}

foreach ($attributes as $attribute) {
$index = $priority = null;

if (isset($attribute['priority'])) {
$priority = $attribute['priority'];
} elseif (null === $defaultPriority && $defaultPriorityMethod && $class) {
$defaultPriority = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultPriorityMethod, $tagName, 'priority', $checkTaggedItem);
}
$priority = $priority ?? $defaultPriority ?? $defaultPriority = 0;

if (null === $indexAttribute && !$defaultIndexMethod && !$needsIndexes) {
if (null === $indexAttribute && null === $defaultIndex && !$needsIndexes) {
$services[] = [$priority, ++$i, null, $serviceId, null];
continue 2;
}

if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
$index = $attribute[$indexAttribute];
} elseif (null === $defaultIndex && $defaultPriorityMethod && $class) {
$defaultIndex = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
}
$index = $index ?? $defaultIndex ?? $defaultIndex = $serviceId;

Expand Down Expand Up @@ -119,13 +120,13 @@ class PriorityTaggedServiceUtil
/**
* @return string|int|null
*/
public static function getDefault(ContainerBuilder $container, string $serviceId, string $class, string $defaultMethod, string $tagName, ?string $indexAttribute, bool $checkTaggedItem)
public static function getDefault(ContainerBuilder $container, string $serviceId, string $class, ?string $defaultMethod, string $tagName, ?string $indexAttribute, bool $checkTaggedItem)
{
if (!($r = $container->getReflectionClass($class)) || (!$checkTaggedItem && !$r->hasMethod($defaultMethod))) {
return null;
}

if ($checkTaggedItem && !$r->hasMethod($defaultMethod)) {
if ($checkTaggedItem && (!$defaultMethod || !$r->hasMethod($defaultMethod))) {
foreach ($r->getAttributes(AsTaggedItem::class) as $attribute) {
return 'priority' === $indexAttribute ? $attribute->newInstance()->priority : $attribute->newInstance()->index;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,14 +214,26 @@ public function testTaggedItemAttributes()
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();

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

// Test TaggedIteratorArgument
$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
$expected = [
'service3' => new TypedReference('service3', HelloNamedService2::class),
'hello' => new TypedReference('service2', HelloNamedService::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));
$this->assertEquals($expected, $services);

// Test string
$services = $priorityTaggedServiceTraitImplementation->test($tag->getTag(), $container);
$expected = [
0 => new Reference('service3'),
'hello' => new TypedReference('service2', HelloNamedService::class),
1 => new Reference('service1'),
];
$this->assertSame(array_keys($expected), array_keys($services));
$this->assertEquals($expected, $services);
}
}

Expand Down