Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 620b0ba

Browse files
committed
[DependencyInjection] Fix PriorityTaggedServiceTrait for AsTaggedItem
If `$tagName` is string, `$defaultPriorityMethod` will be null, and `PriorityTaggedServiceUtil::getDefault` won't work for tagged services. This will cause `AsTaggedItem` not working for these tag names.
1 parent 81a2a6c commit 620b0ba

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,25 +62,26 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
6262
$class = $container->getParameterBag()->resolveValue($class) ?: null;
6363
$checkTaggedItem = !$definition->hasTag(80000 <= \PHP_VERSION_ID && $definition->isAutoconfigured() ? 'container.ignore_attributes' : $tagName);
6464

65+
if ($class) {
66+
$defaultPriority = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultPriorityMethod, $tagName, 'priority', $checkTaggedItem);
67+
$defaultIndex = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultIndexMethod ?? 'getDefaultName', $tagName, $indexAttribute, $checkTaggedItem);
68+
}
69+
6570
foreach ($attributes as $attribute) {
6671
$index = $priority = null;
6772

6873
if (isset($attribute['priority'])) {
6974
$priority = $attribute['priority'];
70-
} elseif (null === $defaultPriority && $defaultPriorityMethod && $class) {
71-
$defaultPriority = PriorityTaggedServiceUtil::getDefault($container, $serviceId, $class, $defaultPriorityMethod, $tagName, 'priority', $checkTaggedItem);
7275
}
7376
$priority = $priority ?? $defaultPriority ?? $defaultPriority = 0;
7477

75-
if (null === $indexAttribute && !$defaultIndexMethod && !$needsIndexes) {
76-
$services[] = [$priority, ++$i, null, $serviceId, null];
78+
if (null === $indexAttribute && null === $defaultIndex && !$needsIndexes) {
79+
$services[] = [$priority, ++$i, null, $serviceId, $class];
7780
continue 2;
7881
}
7982

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

@@ -119,13 +120,13 @@ class PriorityTaggedServiceUtil
119120
/**
120121
* @return string|int|null
121122
*/
122-
public static function getDefault(ContainerBuilder $container, string $serviceId, string $class, string $defaultMethod, string $tagName, ?string $indexAttribute, bool $checkTaggedItem)
123+
public static function getDefault(ContainerBuilder $container, string $serviceId, string $class, ?string $defaultMethod, string $tagName, ?string $indexAttribute, bool $checkTaggedItem)
123124
{
124125
if (!($r = $container->getReflectionClass($class)) || (!$checkTaggedItem && !$r->hasMethod($defaultMethod))) {
125126
return null;
126127
}
127128

128-
if ($checkTaggedItem && !$r->hasMethod($defaultMethod)) {
129+
if ($checkTaggedItem && (!$defaultMethod || !$r->hasMethod($defaultMethod))) {
129130
foreach ($r->getAttributes(AsTaggedItem::class) as $attribute) {
130131
return 'priority' === $indexAttribute ? $attribute->newInstance()->priority : $attribute->newInstance()->index;
131132
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/PriorityTaggedServiceTraitTest.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function testOnlyTheFirstNonIndexedTagIsListed()
108108
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
109109

110110
$expected = [
111-
new Reference('service2'),
111+
new TypedReference('service2', BarTagClass::class),
112112
new Reference('service1'),
113113
];
114114
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test('my_custom_tag', $container));
@@ -214,14 +214,26 @@ public function testTaggedItemAttributes()
214214
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
215215

216216
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo', 'getFooBar');
217+
218+
// Test TaggedIteratorArgument
219+
$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
217220
$expected = [
218221
'service3' => new TypedReference('service3', HelloNamedService2::class),
219222
'hello' => new TypedReference('service2', HelloNamedService::class),
220223
'service1' => new TypedReference('service1', FooTagClass::class),
221224
];
222-
$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
223225
$this->assertSame(array_keys($expected), array_keys($services));
224-
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
226+
$this->assertEquals($expected, $services);
227+
228+
// Test string
229+
$services = $priorityTaggedServiceTraitImplementation->test($tag->getTag(), $container);
230+
$expected = [
231+
0 => new TypedReference('service3', HelloNamedService2::class),
232+
'hello' => new TypedReference('service2', HelloNamedService::class),
233+
1 => new TypedReference('service1', FooTagClass::class),
234+
];
235+
$this->assertSame(array_keys($expected), array_keys($services));
236+
$this->assertEquals($expected, $services);
225237
}
226238
}
227239

0 commit comments

Comments
 (0)