Skip to content

Commit bbbfcc2

Browse files
[DI] ignore extra tags added by autoconfiguration in PriorityTaggedServiceTrait
1 parent d246e94 commit bbbfcc2

File tree

2 files changed

+106
-58
lines changed

2 files changed

+106
-58
lines changed

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

Lines changed: 73 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1717
use Symfony\Component\DependencyInjection\Reference;
18-
use Symfony\Component\DependencyInjection\TypedReference;
1918

2019
/**
2120
* Trait that allows a generic method to find and sort service by priority option in the tag.
@@ -54,88 +53,104 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container
5453

5554
foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) {
5655
$class = $r = null;
57-
5856
$defaultPriority = null;
5957
$defaultIndex = null;
6058

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

6462
if (isset($attribute['priority'])) {
65-
$priority = $attribute['priority'];
63+
$priority = (int) $attribute['priority'];
6664
} elseif (null === $defaultPriority && $defaultPriorityMethod) {
67-
$class = $container->getDefinition($serviceId)->getClass();
68-
$class = $container->getParameterBag()->resolveValue($class) ?: null;
69-
70-
if (($r = ($r ?? $container->getReflectionClass($class))) && $r->hasMethod($defaultPriorityMethod)) {
71-
if (!($rm = $r->getMethod($defaultPriorityMethod))->isStatic()) {
72-
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s".', $class, $defaultPriorityMethod, $tagName, $serviceId));
73-
}
74-
75-
if (!$rm->isPublic()) {
76-
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s".', $class, $defaultPriorityMethod, $tagName, $serviceId));
77-
}
78-
79-
$defaultPriority = $rm->invoke(null);
80-
81-
if (!\is_int($defaultPriority)) {
82-
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s".', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId));
83-
}
84-
}
65+
[$defaultPriority, $class, $r] = PriorityTaggedServiceUtil::getDefaultPriority($container, $serviceId, $defaultPriorityMethod, $r, $tagName);
8566
}
67+
$priority = $priority ?? $defaultPriority ?? $defaultPriority = 0;
8668

87-
$priority = $priority ?? $defaultPriority ?? 0;
69+
if (null === $indexAttribute && !$needsIndexes) {
70+
$services[$priority][] = new Reference($serviceId);
71+
continue 2;
72+
}
8873

8974
if (null !== $indexAttribute && isset($attribute[$indexAttribute])) {
9075
$index = $attribute[$indexAttribute];
91-
} elseif (null === $defaultIndex && null === $indexAttribute && !$needsIndexes) {
92-
// With partially associative array, insertion to get next key is simpler.
93-
$services[$priority][] = null;
94-
end($services[$priority]);
95-
$defaultIndex = key($services[$priority]);
9676
} elseif (null === $defaultIndex && $defaultIndexMethod) {
97-
$class = $container->getDefinition($serviceId)->getClass();
98-
$class = $container->getParameterBag()->resolveValue($class) ?: null;
77+
[$defaultIndex, $class, $r] = PriorityTaggedServiceUtil::getDefaultIndex($container, $serviceId, $defaultIndexMethod, $r, $tagName, $indexAttribute);
78+
}
79+
$index = $index ?? $defaultIndex ?? $defaultIndex = $serviceId;
9980

100-
if (($r = ($r ?? $container->getReflectionClass($class))) && $r->hasMethod($defaultIndexMethod)) {
101-
if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) {
102-
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
103-
}
81+
$services[$priority][$index] = new Reference($serviceId);
82+
}
83+
}
10484

105-
if (!$rm->isPublic()) {
106-
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
107-
}
85+
if ($services) {
86+
krsort($services);
87+
$services = array_merge(...$services);
88+
}
10889

109-
$defaultIndex = $rm->invoke(null);
90+
return $services;
91+
}
92+
}
11093

111-
if (!\is_string($defaultIndex)) {
112-
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return a string, got %s: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, \gettype($defaultIndex), $tagName, $serviceId, $indexAttribute));
113-
}
114-
}
94+
/**
95+
* @internal
96+
*/
97+
class PriorityTaggedServiceUtil
98+
{
99+
/**
100+
* Gets the index defined by the default index method.
101+
*/
102+
public static function getDefaultIndex(ContainerBuilder $container, string $serviceId, string $defaultIndexMethod, ?\ReflectionClass $r, string $tagName, string $indexAttribute): ?array
103+
{
104+
$class = $container->getDefinition($serviceId)->getClass();
105+
$class = $container->getParameterBag()->resolveValue($class) ?: null;
115106

116-
$defaultIndex = $defaultIndex ?? $serviceId;
117-
}
107+
if (!($r = $r ?? $container->getReflectionClass($class)) || !$r->hasMethod($defaultIndexMethod)) {
108+
return null;
109+
}
118110

119-
$index = $index ?? $defaultIndex;
111+
if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) {
112+
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should be static or tag "%s" on service "%s" is missing attribute "%s".', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
113+
}
120114

121-
$reference = null;
122-
if (!$class || 'stdClass' === $class) {
123-
$reference = new Reference($serviceId);
124-
} elseif ($index === $serviceId) {
125-
$reference = new TypedReference($serviceId, $class);
126-
} else {
127-
$reference = new TypedReference($serviceId, $class, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, \is_string($index) ? $index : null);
128-
}
115+
if (!$rm->isPublic()) {
116+
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should be public or tag "%s" on service "%s" is missing attribute "%s".', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute));
117+
}
129118

130-
$services[$priority][$index] = $reference;
131-
}
119+
$defaultIndex = $rm->invoke(null);
120+
121+
if (!\is_string($defaultIndex)) {
122+
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should return a string (got %s) or tag "%s" on service "%s" is missing attribute "%s".', $class, $defaultIndexMethod, \gettype($defaultIndex), $tagName, $serviceId, $indexAttribute));
132123
}
133124

134-
if ($services) {
135-
krsort($services);
136-
$services = array_merge(...$services);
125+
return [$defaultIndex, $class, $r];
126+
}
127+
128+
/**
129+
* Gets the priority defined by the default priority method.
130+
*/
131+
public static function getDefaultPriority(ContainerBuilder $container, string $serviceId, string $defaultPriorityMethod, ?\ReflectionClass $r, string $tagName): ?array
132+
{
133+
$class = $container->getDefinition($serviceId)->getClass();
134+
$class = $container->getParameterBag()->resolveValue($class) ?: null;
135+
136+
if (!($r = $r ?? $container->getReflectionClass($class)) || !$r->hasMethod($defaultPriorityMethod)) {
137+
return null;
137138
}
138139

139-
return $services;
140+
if (!($rm = $r->getMethod($defaultPriorityMethod))->isStatic()) {
141+
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should be static or tag "%s" on service "%s" is missing attribute "priority".', $class, $defaultPriorityMethod, $tagName, $serviceId));
142+
}
143+
144+
if (!$rm->isPublic()) {
145+
throw new InvalidArgumentException(sprintf('Either method "%s::%s()" should be public or tag "%s" on service "%s" is missing attribute "priority".', $class, $defaultPriorityMethod, $tagName, $serviceId));
146+
}
147+
148+
$defaultPriority = $rm->invoke(null);
149+
150+
if (!\is_int($defaultPriority)) {
151+
throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return an integer (got %s) or tag "%s" on service "%s" is missing attribute "priority".', $class, $defaultPriorityMethod, \gettype($defaultPriority), $tagName, $serviceId));
152+
}
153+
154+
return [$defaultPriority, $class, $r];
140155
}
141156
}

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
namespace Symfony\Component\DependencyInjection\Tests\Compiler;
1313

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
1516
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
1617
use Symfony\Component\DependencyInjection\ContainerBuilder;
1718
use Symfony\Component\DependencyInjection\Reference;
19+
use Symfony\Component\DependencyInjection\Tests\Fixtures\BarTagClass;
1820

1921
class PriorityTaggedServiceTraitTest extends TestCase
2022
{
@@ -85,6 +87,37 @@ public function testWithEmptyArray()
8587
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
8688
$this->assertEquals([], $priorityTaggedServiceTraitImplementation->test('my_custom_tag', $container));
8789
}
90+
91+
public function testOnlyTheFirstNonIndexedTagIsListed()
92+
{
93+
$container = new ContainerBuilder();
94+
$container->register('service1')->addTag('my_custom_tag', ['foo' => 'bar']);
95+
96+
$definition = $container->register('service2', BarTagClass::class);
97+
$definition->addTag('my_custom_tag', ['priority' => 100]);
98+
$definition->addTag('my_custom_tag', ['foo' => 'a']);
99+
$definition->addTag('my_custom_tag', ['foo' => 'b']);
100+
$definition->addTag('my_custom_tag', []);
101+
102+
$priorityTaggedServiceTraitImplementation = new PriorityTaggedServiceTraitImplementation();
103+
104+
$expected = [
105+
new Reference('service2'),
106+
new Reference('service1'),
107+
];
108+
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test('my_custom_tag', $container));
109+
110+
$tag = new TaggedIteratorArgument('my_custom_tag', 'foo');
111+
$expected = [
112+
'bar_tag_class' => new Reference('service2'),
113+
'bar' => new Reference('service1'),
114+
'a' => new Reference('service2'),
115+
'b' => new Reference('service2'),
116+
];
117+
$services = $priorityTaggedServiceTraitImplementation->test($tag, $container);
118+
$this->assertSame(array_keys($expected), array_keys($services));
119+
$this->assertEquals($expected, $priorityTaggedServiceTraitImplementation->test($tag, $container));
120+
}
88121
}
89122

90123
class PriorityTaggedServiceTraitImplementation

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy