From e2138ada239621e9e8f1c12a2ba91af096938454 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Carlos=20Abreu=20Jim=C3=A9nez?= Date: Fri, 7 Jun 2019 16:29:36 -0400 Subject: [PATCH 01/12] Define default priority inside service class With autoconfigure functions we don't have to do anything to inject tagged services into a collector using interfaces to define tags. But if we need some order we have to use priority, the problem is that priority only can be set by configuration file when it is a simple value. With this change you could instead of put 0 by defualt take a look if the static method `getDefaultPriority` exists in the service class and return its default value for the service. Example: --- .../DependencyInjection/Compiler/PriorityTaggedServiceTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 5f04eadef8279..4b93206147949 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -53,7 +53,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container $services = []; foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) { - $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : 0; + $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : (method_exists($serviceId, 'getDefaultPriority') ? $serviceId::getDefaultPriority() : 0); if (null === $indexAttribute && !$needsIndexes) { $services[$priority][] = new Reference($serviceId); From bd88e1ef43dd0b76884dab933510b2d611b48da5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Carlos=20Abreu=20Jim=C3=A9nez?= Date: Sun, 9 Jun 2019 23:02:03 -0400 Subject: [PATCH 02/12] Update YamlFileLoader.php It looks like `YamlFileLoader` makes `index_by` mandatory when you define the tagged service using `arguments: [!tagged { tag: 'app.handler'}]` syntax. It's needed to accept others parameters like the new `default_priority_method` without specify `index_by` and the `XmlFielLoader` seems to accept it. --- .../Component/DependencyInjection/Loader/YamlFileLoader.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index cbb39ae0357d7..3418a3b842f1f 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -737,7 +737,7 @@ private function resolveServices($value, $file, $isParameter = false) throw new InvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by" and "default_index_method".', $value->getTag(), implode('"", "', $diff))); } - $argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'], $argument['default_index_method'] ?? null, $forLocator); + $argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator); if ($forLocator) { $argument = new ServiceLocatorArgument($argument); From 2df617e1e851fa0cacf1b0eec4aef8f010870983 Mon Sep 17 00:00:00 2001 From: pcabreus Date: Mon, 10 Jun 2019 12:48:46 -0400 Subject: [PATCH 03/12] added ability to define an priority method for a tagged collection --- .../Argument/TaggedIteratorArgument.php | 18 ++++-- .../DependencyInjection/CHANGELOG.md | 1 + .../Compiler/PriorityTaggedServiceTrait.php | 61 ++++++++++++++++--- .../DependencyInjection/Dumper/XmlDumper.php | 4 ++ .../DependencyInjection/Dumper/YamlDumper.php | 19 +++--- .../Configurator/ContainerConfigurator.php | 25 ++++++-- .../Loader/XmlFileLoader.php | 2 +- .../Loader/YamlFileLoader.php | 6 +- .../schema/dic/services/services-1.0.xsd | 1 + .../Tests/Compiler/IntegrationTest.php | 24 ++++++++ .../Tests/Dumper/XmlDumperTest.php | 18 ++++++ .../Tests/Dumper/YamlDumperTest.php | 12 ++++ .../Tests/Fixtures/FooTagClass.php | 5 ++ .../services_with_tagged_priority_method.xml | 17 ++++++ .../services_with_tagged_priority_method.yml | 22 +++++++ .../Tests/Loader/XmlFileLoaderTest.php | 17 ++++++ .../Tests/Loader/YamlFileLoaderTest.php | 17 ++++++ 17 files changed, 240 insertions(+), 29 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml diff --git a/src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php b/src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php index c88b174169054..3fb6900ce5094 100644 --- a/src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php +++ b/src/Symfony/Component/DependencyInjection/Argument/TaggedIteratorArgument.php @@ -22,14 +22,16 @@ class TaggedIteratorArgument extends IteratorArgument private $indexAttribute; private $defaultIndexMethod; private $needsIndexes = false; + private $defaultPriorityMethod; /** - * @param string $tag The name of the tag identifying the target services - * @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection - * @param string|null $defaultIndexMethod The static method that should be called to get each service's key when their tag doesn't define the previous attribute - * @param bool $needsIndexes Whether indexes are required and should be generated when computing the map + * @param string $tag The name of the tag identifying the target services + * @param string|null $indexAttribute The name of the attribute that defines the key referencing each service in the tagged collection + * @param string|null $defaultIndexMethod The static method that should be called to get each service's key when their tag doesn't define the previous attribute + * @param bool $needsIndexes Whether indexes are required and should be generated when computing the map + * @param string|null $defaultPriorityMethod The static method that should be called to get the default priority of the service */ - public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false) + public function __construct(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false, string $defaultPriorityMethod = null) { parent::__construct([]); @@ -41,6 +43,7 @@ public function __construct(string $tag, string $indexAttribute = null, string $ $this->indexAttribute = $indexAttribute; $this->defaultIndexMethod = $defaultIndexMethod ?: ('getDefault'.str_replace(' ', '', ucwords(preg_replace('/[^a-zA-Z0-9\x7f-\xff]++/', ' ', $indexAttribute ?? ''))).'Name'); $this->needsIndexes = $needsIndexes; + $this->defaultPriorityMethod = $defaultPriorityMethod; } public function getTag() @@ -62,4 +65,9 @@ public function needsIndexes(): bool { return $this->needsIndexes; } + + public function getDefaultPriorityMethod(): ?string + { + return $this->defaultPriorityMethod; + } } diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 152a17ce9a46f..bab5b366aff68 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -5,6 +5,7 @@ CHANGELOG ----- * deprecated support for short factories and short configurators in Yaml + * added ability to define an priority method for a tagged collection 4.3.0 ----- diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 4b93206147949..a9a8b33da80de 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -41,19 +41,69 @@ trait PriorityTaggedServiceTrait */ private function findAndSortTaggedServices($tagName, ContainerBuilder $container) { - $indexAttribute = $defaultIndexMethod = $needsIndexes = null; + $indexAttribute = $defaultIndexMethod = $needsIndexes = $defaultPriorityMethod = null; if ($tagName instanceof TaggedIteratorArgument) { $indexAttribute = $tagName->getIndexAttribute(); $defaultIndexMethod = $tagName->getDefaultIndexMethod(); $needsIndexes = $tagName->needsIndexes(); + $defaultPriorityMethod = $tagName->getDefaultPriorityMethod(); $tagName = $tagName->getTag(); } $services = []; foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) { - $priority = isset($attributes[0]['priority']) ? $attributes[0]['priority'] : (method_exists($serviceId, 'getDefaultPriority') ? $serviceId::getDefaultPriority() : 0); + $class = $container->getDefinition($serviceId)->getClass(); + $class = $container->getParameterBag()->resolveValue($class) ?: null; + + if (!$r = $container->getReflectionClass($class)) { + throw new InvalidArgumentException( sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); + } + + $priority = 0; + if ($defaultPriorityMethod && $r->hasMethod($defaultPriorityMethod)) { + if (!($rpm = $r->getMethod($defaultPriorityMethod))->isStatic()) { + throw new InvalidArgumentException( + sprintf( + 'Method "%s::%s()" should be static: tag "%s" on service "%s" is missing default priority method.', + $class, + $defaultPriorityMethod, + $tagName, + $serviceId + ) + ); + } + + if (!$rpm->isPublic()) { + throw new InvalidArgumentException( + sprintf( + 'Method "%s::%s()" should be public: tag "%s" on service "%s" is missing default priority method.', + $class, + $defaultPriorityMethod, + $tagName, + $serviceId + ) + ); + } + + $priority = $rpm->invoke(null); + + if (!\is_integer($priority)) { + throw new InvalidArgumentException( + sprintf( + 'Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s" is missing default priority method.', + $class, + $defaultPriorityMethod, + \gettype($priority), + $tagName, + $serviceId + ) + ); + } + } + + $priority = $attributes[0]['priority'] ?? $priority; if (null === $indexAttribute && !$needsIndexes) { $services[$priority][] = new Reference($serviceId); @@ -61,19 +111,12 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container continue; } - $class = $container->getDefinition($serviceId)->getClass(); - $class = $container->getParameterBag()->resolveValue($class) ?: null; - if (null !== $indexAttribute && isset($attributes[0][$indexAttribute])) { $services[$priority][$attributes[0][$indexAttribute]] = new TypedReference($serviceId, $class, ContainerBuilder::EXCEPTION_ON_INVALID_REFERENCE, $attributes[0][$indexAttribute]); continue; } - if (!$r = $container->getReflectionClass($class)) { - throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); - } - $class = $r->name; if (!$r->hasMethod($defaultIndexMethod)) { diff --git a/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php index 42ee0a25ff959..e2fe608c82182 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/XmlDumper.php @@ -297,6 +297,10 @@ private function convertParameters(array $parameters, $type, \DOMElement $parent $element->setAttribute('default-index-method', $tag->getDefaultIndexMethod()); } } + + if (null !== $tag->getDefaultPriorityMethod()) { + $element->setAttribute('default-priority-method', $tag->getDefaultPriorityMethod()); + } } elseif ($value instanceof IteratorArgument) { $element->setAttribute('type', 'iterator'); $this->convertParameters($value->getValues(), $type, $element, 'key'); diff --git a/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php index 89dae636de023..e48aea5297046 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php @@ -235,19 +235,24 @@ private function dumpValue($value) $tag = $value; if ($value instanceof TaggedIteratorArgument || ($value instanceof ServiceLocatorArgument && $tag = $value->getTaggedIteratorArgument())) { - if (null === $tag->getIndexAttribute()) { - $content = $tag->getTag(); - } else { - $content = [ - 'tag' => $tag->getTag(), - 'index_by' => $tag->getIndexAttribute(), - ]; + $content = ['tag' => $tag->getTag()]; + + if (null !== $tag->getIndexAttribute()) { + $content['index_by'] = $tag->getIndexAttribute(); if (null !== $tag->getDefaultIndexMethod()) { $content['default_index_method'] = $tag->getDefaultIndexMethod(); } } + if (null !== $tag->getDefaultPriorityMethod()) { + $content['default_priority_method'] = $tag->getDefaultPriorityMethod(); + } + + if (count($content) === 1) { + $content = $tag->getTag(); + } + return new TaggedValue($value instanceof TaggedIteratorArgument ? 'tagged' : 'tagged_locator', $content); } diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php index 87b066faf5030..fdd6d3a0e5b5c 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php @@ -115,22 +115,39 @@ function iterator(array $values): IteratorArgument /** * Creates a lazy iterator by tag name. + * + * @param string $tag + * @param string|null $indexAttribute + * @param string|null $defaultIndexMethod + * @param bool $needsIndexes + * @param string|null $defaultPriorityMethod + * @return TaggedIteratorArgument */ -function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null): TaggedIteratorArgument +function tagged(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false, string $defaultPriorityMethod = null): TaggedIteratorArgument { - return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod); + return new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod,$needsIndexes, $defaultPriorityMethod); } /** * Creates a service locator by tag name. + * + * @param string $tag + * @param string $indexAttribute + * @param string|null $defaultIndexMethod + * @param bool $needsIndexes + * @param string|null $defaultPriorityMethod + * @return ServiceLocatorArgument */ -function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null): ServiceLocatorArgument +function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null, bool $needsIndexes = true, string $defaultPriorityMethod = null): ServiceLocatorArgument { - return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, true)); + return new ServiceLocatorArgument(new TaggedIteratorArgument($tag, $indexAttribute, $defaultIndexMethod, $needsIndexes, $defaultPriorityMethod)); } /** * Creates an expression. + * + * @param string $expression + * @return Expression */ function expr(string $expression): Expression { diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index bbe2d5569579b..3e4a82e7ef6fd 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -553,7 +553,7 @@ private function getArgumentsAsPhp(\DOMElement $node, $name, $file, $lowercase = throw new InvalidArgumentException(sprintf('Tag "<%s>" with type="%s" has no or empty "tag" attribute in "%s".', $name, $type, $file)); } - $arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null, $forLocator); + $arguments[$key] = new TaggedIteratorArgument($arg->getAttribute('tag'), $arg->getAttribute('index-by') ?: null, $arg->getAttribute('default-index-method') ?: null, $forLocator, $arg->getAttribute('default-priority-method') ?: null); if ($forLocator) { $arguments[$key] = new ServiceLocatorArgument($arguments[$key]); diff --git a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php index 3418a3b842f1f..2256072100545 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/YamlFileLoader.php @@ -733,11 +733,11 @@ private function resolveServices($value, $file, $isParameter = false) } if (\is_array($argument) && isset($argument['tag']) && $argument['tag']) { - if ($diff = array_diff(array_keys($argument), ['tag', 'index_by', 'default_index_method'])) { - throw new InvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by" and "default_index_method".', $value->getTag(), implode('"", "', $diff))); + if ($diff = array_diff(array_keys($argument), ['tag', 'index_by', 'default_index_method', 'default_priority_method'])) { + throw new InvalidArgumentException(sprintf('"!%s" tag contains unsupported key "%s"; supported ones are "tag", "index_by", "default_index_method" and "default_priority_method".', $value->getTag(), implode('"", "', $diff))); } - $argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator); + $argument = new TaggedIteratorArgument($argument['tag'], $argument['index_by'] ?? null, $argument['default_index_method'] ?? null, $forLocator, $argument['default_priority_method'] ?? null); if ($forLocator) { $argument = new ServiceLocatorArgument($argument); diff --git a/src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd b/src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd index 05efb9067f82d..741611a8be22f 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd +++ b/src/Symfony/Component/DependencyInjection/Loader/schema/dic/services/services-1.0.xsd @@ -236,6 +236,7 @@ + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php index 83d3e23d3ba5a..698e3e1accf43 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/IntegrationTest.php @@ -402,6 +402,30 @@ public function testTaggedServiceLocatorWithDefaultIndex() ]; $this->assertSame($expected, ['baz' => $serviceLocator->get('baz')]); } + + public function testTaggedServiceWithPriorityMethod() + { + $container = new ContainerBuilder(); + $container->register(BarTagClass::class) + ->setPublic(true) + ->addTag('foo_bar', ['priority' => 10]) + ; + $container->register(FooTagClass::class) + ->setPublic(true) + ->addTag('foo_bar') + ; + $container->register(FooBarTaggedClass::class) + ->addArgument(new TaggedIteratorArgument('foo_bar', null, null, false, 'getDefaultPriority')) + ->setPublic(true) + ; + + $container->compile(); + + $s = $container->get(FooBarTaggedClass::class); + + $param = iterator_to_array($s->getParam()->getIterator()); + $this->assertSame([0 => $container->get(FooTagClass::class), 1 => $container->get(BarTagClass::class)], $param); + } } class ServiceSubscriberStub implements ServiceSubscriberInterface diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php index cc80a69455b97..676ee81b9bab0 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/XmlDumperTest.php @@ -220,6 +220,24 @@ public function testTaggedArguments() $this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_with_tagged_arguments.xml', $dumper->dump()); } + public function testTaggedWithDefaultPriorityMethod() + { + $taggedIterator = new TaggedIteratorArgument('foo_tag', null, null, false, 'foopriority'); + $container = new ContainerBuilder(); + $container->register('foo', 'Foo')->addTag('foo_tag'); + $container->register('foo_tagged_iterator', 'Bar') + ->setPublic(true) + ->addArgument($taggedIterator) + ; + $container->register('foo_tagged_locator', 'Bar') + ->setPublic(true) + ->addArgument(new ServiceLocatorArgument($taggedIterator)) + ; + + $dumper = new XmlDumper($container); + $this->assertStringEqualsFile(self::$fixturesPath.'/xml/services_with_tagged_priority_method.xml', $dumper->dump()); + } + public function testDumpAbstractServices() { $container = include self::$fixturesPath.'/containers/container_abstract.php'; diff --git a/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php b/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php index 72901c855e414..8742f7e8390b1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Dumper/YamlDumperTest.php @@ -109,6 +109,18 @@ public function testTaggedArguments() $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_tagged_argument.yml', $dumper->dump()); } + public function testTaggedWithDefaultPriorityMethod() + { + $taggedIterator = new TaggedIteratorArgument('foo', null, null, false, 'foopriority'); + $container = new ContainerBuilder(); + $container->register('foo_service', 'Foo')->addTag('foo'); + $container->register('foo_service_tagged_iterator', 'Bar')->addArgument($taggedIterator); + $container->register('foo_service_tagged_locator', 'Bar')->addArgument(new ServiceLocatorArgument($taggedIterator)); + + $dumper = new YamlDumper($container); + $this->assertStringEqualsFile(self::$fixturesPath.'/yaml/services_with_tagged_priority_method.yml', $dumper->dump()); + } + private function assertEqualYamlStructure($expected, $yaml, $message = '') { $parser = new Parser(); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooTagClass.php b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooTagClass.php index c1279b9a9feeb..7ec13a33d5486 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooTagClass.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/FooTagClass.php @@ -8,4 +8,9 @@ public static function getDefaultFooName() { return 'foo_tag_class'; } + + public static function getDefaultPriority() + { + return 20; + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml new file mode 100644 index 0000000000000..184c9385e2012 --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml new file mode 100644 index 0000000000000..aaf009b2a6dcb --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml @@ -0,0 +1,22 @@ + +services: + service_container: + class: Symfony\Component\DependencyInjection\ContainerInterface + public: true + synthetic: true + foo_service: + class: Foo + tags: + - { name: foo } + foo_service_tagged_iterator: + class: Bar + arguments: [!tagged { tag: foo, default_priority_method: foopriority }] + foo_service_tagged_locator: + class: Bar + arguments: [!tagged_locator { tag: foo, default_priority_method: foopriority }] + Psr\Container\ContainerInterface: + alias: service_container + public: false + Symfony\Component\DependencyInjection\ContainerInterface: + alias: service_container + public: false diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php index b57f10c597f53..7e618b6eb96d8 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/XmlFileLoaderTest.php @@ -336,6 +336,23 @@ public function testParseTaggedArgumentsWithIndexBy() $this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_tagged_locator')->getArgument(0)); } + public function testParseTaggedArgumentsWithPriority() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml')); + $loader->load('services_with_tagged_priority_method.xml'); + + $this->assertCount(1, $container->getDefinition('foo')->getTag('foo_tag')); + $this->assertCount(1, $container->getDefinition('foo_tagged_iterator')->getArguments()); + $this->assertCount(1, $container->getDefinition('foo_tagged_locator')->getArguments()); + + $taggedIterator = new TaggedIteratorArgument('foo_tag', null, null, false, 'foopriority'); + $this->assertEquals($taggedIterator, $container->getDefinition('foo_tagged_iterator')->getArgument(0)); + + $taggedIterator = new TaggedIteratorArgument('foo_tag', null, null, true, 'foopriority'); + $this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_tagged_locator')->getArgument(0)); + } + /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException */ diff --git a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php index 0aa30f288c701..0fb4307b27c81 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Loader/YamlFileLoaderTest.php @@ -317,6 +317,23 @@ public function testTaggedArgumentsWithIndex() $this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_service_tagged_locator')->getArgument(0)); } + public function testParseTaggedArgumentsWithPriority() + { + $container = new ContainerBuilder(); + $loader = new YamlFileLoader($container, new FileLocator(self::$fixturesPath.'/yaml')); + $loader->load('services_with_tagged_priority_method.yml'); + + $this->assertCount(1, $container->getDefinition('foo_service')->getTag('foo')); + $this->assertCount(1, $container->getDefinition('foo_service_tagged_iterator')->getArguments()); + $this->assertCount(1, $container->getDefinition('foo_service_tagged_locator')->getArguments()); + + $taggedIterator = new TaggedIteratorArgument('foo', null, null, false, 'foopriority'); + $this->assertEquals($taggedIterator, $container->getDefinition('foo_service_tagged_iterator')->getArgument(0)); + + $taggedIterator = new TaggedIteratorArgument('foo', null, null, true, 'foopriority'); + $this->assertEquals(new ServiceLocatorArgument($taggedIterator), $container->getDefinition('foo_service_tagged_locator')->getArgument(0)); + } + public function testNameOnlyTagsAreAllowedAsString() { $container = new ContainerBuilder(); From b6069b258a445fad5b9f962e35d6394fa3b61c52 Mon Sep 17 00:00:00 2001 From: pcabreus Date: Mon, 10 Jun 2019 13:10:43 -0400 Subject: [PATCH 04/12] fixed fabbot.io code standard --- .../Compiler/PriorityTaggedServiceTrait.php | 4 ++-- .../DependencyInjection/Dumper/YamlDumper.php | 6 +++--- .../Loader/Configurator/ContainerConfigurator.php | 13 ++++++++----- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index a9a8b33da80de..2ee835c5f5c4a 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -58,7 +58,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container $class = $container->getParameterBag()->resolveValue($class) ?: null; if (!$r = $container->getReflectionClass($class)) { - throw new InvalidArgumentException( sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); } $priority = 0; @@ -89,7 +89,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container $priority = $rpm->invoke(null); - if (!\is_integer($priority)) { + if (!\is_int($priority)) { throw new InvalidArgumentException( sprintf( 'Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s" is missing default priority method.', diff --git a/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php b/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php index 4ef15e7cba975..22f9dc1714fe1 100644 --- a/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php +++ b/src/Symfony/Component/DependencyInjection/Dumper/YamlDumper.php @@ -244,15 +244,15 @@ private function dumpValue($value) $content['default_index_method'] = $tag->getDefaultIndexMethod(); } } - + if (null !== $tag->getDefaultPriorityMethod()) { $content['default_priority_method'] = $tag->getDefaultPriorityMethod(); } - if (count($content) === 1) { + if (1 === \count($content)) { $content = $tag->getTag(); } - + return new TaggedValue($value instanceof TaggedIteratorArgument ? 'tagged_iterator' : 'tagged_locator', $content); } diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php index ef100f3bf3835..2283d135a1200 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php @@ -128,11 +128,12 @@ function tagged(string $tag, string $indexAttribute = null, string $defaultIndex /** * Creates a lazy iterator by tag name. * - * @param string $tag + * @param string $tag * @param string|null $indexAttribute * @param string|null $defaultIndexMethod - * @param bool $needsIndexes + * @param bool $needsIndexes * @param string|null $defaultPriorityMethod + * * @return TaggedIteratorArgument */ function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false, string $defaultPriorityMethod = null): TaggedIteratorArgument @@ -143,11 +144,12 @@ function tagged_iterator(string $tag, string $indexAttribute = null, string $def /** * Creates a service locator by tag name. * - * @param string $tag - * @param string $indexAttribute + * @param string $tag + * @param string $indexAttribute * @param string|null $defaultIndexMethod - * @param bool $needsIndexes + * @param bool $needsIndexes * @param string|null $defaultPriorityMethod + * * @return ServiceLocatorArgument */ function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null, bool $needsIndexes = true, string $defaultPriorityMethod = null): ServiceLocatorArgument @@ -159,6 +161,7 @@ function tagged_locator(string $tag, string $indexAttribute, string $defaultInde * Creates an expression. * * @param string $expression + * * @return Expression */ function expr(string $expression): Expression From 54988242b54ef95e00bb44d8933fc242774ea68a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Carlos=20Abreu=20Jim=C3=A9nez?= Date: Tue, 11 Jun 2019 10:10:52 -0400 Subject: [PATCH 05/12] fixed spell Co-Authored-By: Valentin Udaltsov --- src/Symfony/Component/DependencyInjection/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/CHANGELOG.md b/src/Symfony/Component/DependencyInjection/CHANGELOG.md index 3b53225364608..db16595902901 100644 --- a/src/Symfony/Component/DependencyInjection/CHANGELOG.md +++ b/src/Symfony/Component/DependencyInjection/CHANGELOG.md @@ -6,7 +6,7 @@ CHANGELOG * deprecated support for short factories and short configurators in Yaml * deprecated `tagged` in favor of `tagged_iterator` - * added ability to define an priority method for a tagged collection + * added ability to define a priority method for a tagged collection 4.3.0 ----- From 8ab9cfd998e3c2bdb2e380f72671fec5dc1fd47c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Carlos=20Abreu=20Jim=C3=A9nez?= Date: Tue, 11 Jun 2019 10:12:55 -0400 Subject: [PATCH 06/12] compare using null Co-Authored-By: Valentin Udaltsov --- .../DependencyInjection/Compiler/PriorityTaggedServiceTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 2ee835c5f5c4a..3c24cba323970 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -57,7 +57,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container $class = $container->getDefinition($serviceId)->getClass(); $class = $container->getParameterBag()->resolveValue($class) ?: null; - if (!$r = $container->getReflectionClass($class)) { + if (null !== $r = $container->getReflectionClass($class)) { throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); } From f48eea91431e39a61e5dcdb9ba70bc1e32cd5d45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Carlos=20Abreu=20Jim=C3=A9nez?= Date: Tue, 11 Jun 2019 10:15:42 -0400 Subject: [PATCH 07/12] better exception decription Co-Authored-By: Valentin Udaltsov --- .../DependencyInjection/Compiler/PriorityTaggedServiceTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 3c24cba323970..2de75f00002d6 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -66,7 +66,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container if (!($rpm = $r->getMethod($defaultPriorityMethod))->isStatic()) { throw new InvalidArgumentException( sprintf( - 'Method "%s::%s()" should be static: tag "%s" on service "%s" is missing default priority method.', + 'Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" must be static.', $class, $defaultPriorityMethod, $tagName, From c021fc8452703627c01dfbdf50c398355b039cfe Mon Sep 17 00:00:00 2001 From: pcabreus Date: Tue, 11 Jun 2019 10:30:05 -0400 Subject: [PATCH 08/12] fixed some code standard --- .../Compiler/PriorityTaggedServiceTrait.php | 37 +++---------------- .../Configurator/ContainerConfigurator.php | 20 ---------- 2 files changed, 6 insertions(+), 51 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index 2de75f00002d6..c83639de437d6 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -63,43 +63,18 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container $priority = 0; if ($defaultPriorityMethod && $r->hasMethod($defaultPriorityMethod)) { - if (!($rpm = $r->getMethod($defaultPriorityMethod))->isStatic()) { - throw new InvalidArgumentException( - sprintf( - 'Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" must be static.', - $class, - $defaultPriorityMethod, - $tagName, - $serviceId - ) - ); + if (!($priorityReflMethod = $r->getMethod($defaultPriorityMethod))->isStatic()) { + throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" must be static.', $class, $defaultPriorityMethod, $tagName, $serviceId)); } - if (!$rpm->isPublic()) { - throw new InvalidArgumentException( - sprintf( - 'Method "%s::%s()" should be public: tag "%s" on service "%s" is missing default priority method.', - $class, - $defaultPriorityMethod, - $tagName, - $serviceId - ) - ); + if (!$priorityReflMethod->isPublic()) { + throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing default priority method.', $class, $defaultPriorityMethod, $tagName, $serviceId)); } - $priority = $rpm->invoke(null); + $priority = $priorityReflMethod->invoke(null); if (!\is_int($priority)) { - throw new InvalidArgumentException( - sprintf( - 'Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s" is missing default priority method.', - $class, - $defaultPriorityMethod, - \gettype($priority), - $tagName, - $serviceId - ) - ); + throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s" is missing default priority method.', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId)); } } diff --git a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php index 2283d135a1200..0f571cf826b9a 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php +++ b/src/Symfony/Component/DependencyInjection/Loader/Configurator/ContainerConfigurator.php @@ -127,14 +127,6 @@ function tagged(string $tag, string $indexAttribute = null, string $defaultIndex /** * Creates a lazy iterator by tag name. - * - * @param string $tag - * @param string|null $indexAttribute - * @param string|null $defaultIndexMethod - * @param bool $needsIndexes - * @param string|null $defaultPriorityMethod - * - * @return TaggedIteratorArgument */ function tagged_iterator(string $tag, string $indexAttribute = null, string $defaultIndexMethod = null, bool $needsIndexes = false, string $defaultPriorityMethod = null): TaggedIteratorArgument { @@ -143,14 +135,6 @@ function tagged_iterator(string $tag, string $indexAttribute = null, string $def /** * Creates a service locator by tag name. - * - * @param string $tag - * @param string $indexAttribute - * @param string|null $defaultIndexMethod - * @param bool $needsIndexes - * @param string|null $defaultPriorityMethod - * - * @return ServiceLocatorArgument */ function tagged_locator(string $tag, string $indexAttribute, string $defaultIndexMethod = null, bool $needsIndexes = true, string $defaultPriorityMethod = null): ServiceLocatorArgument { @@ -159,10 +143,6 @@ function tagged_locator(string $tag, string $indexAttribute, string $defaultInde /** * Creates an expression. - * - * @param string $expression - * - * @return Expression */ function expr(string $expression): Expression { From 2d8fd1f778488a091587e7c6bf3673ea0c07c002 Mon Sep 17 00:00:00 2001 From: pcabreus Date: Tue, 11 Jun 2019 10:34:39 -0400 Subject: [PATCH 09/12] fixed some code standard --- .../Compiler/PriorityTaggedServiceTrait.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index c83639de437d6..a60a8960cbc90 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -68,13 +68,13 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container } if (!$priorityReflMethod->isPublic()) { - throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be public: tag "%s" on service "%s" is missing default priority method.', $class, $defaultPriorityMethod, $tagName, $serviceId)); + throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" should be public.', $class, $defaultPriorityMethod, $tagName, $serviceId)); } $priority = $priorityReflMethod->invoke(null); if (!\is_int($priority)) { - throw new InvalidArgumentException(sprintf('Method "%s::%s()" should return an integer, got %s: tag "%s" on service "%s" is missing default priority method.', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId)); + throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" should return an integer, got %s.', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId)); } } From 6b51ac50c8aa3d987d3da751e7b6989c15ed821b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pedro=20Carlos=20Abreu=20Jim=C3=A9nez?= Date: Wed, 12 Jun 2019 08:24:12 -0400 Subject: [PATCH 10/12] fixed arguments order Co-Authored-By: Kevin Verschaeve --- .../DependencyInjection/Compiler/PriorityTaggedServiceTrait.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index a60a8960cbc90..d0f9448b6cdd3 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -74,7 +74,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container $priority = $priorityReflMethod->invoke(null); if (!\is_int($priority)) { - throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" should return an integer, got %s.', $class, $defaultPriorityMethod, \gettype($priority), $tagName, $serviceId)); + throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" should return an integer, got %s.', $class, $defaultPriorityMethod, $tagName, $serviceId, \gettype($priority))); } } From b9dd5eb67fb19832ba2892f2c7da61d8fdaf83bb Mon Sep 17 00:00:00 2001 From: pcabreus Date: Wed, 12 Jun 2019 09:17:28 -0400 Subject: [PATCH 11/12] using the `tagged_iterator` instead deprecated `tagged` --- .../Tests/Fixtures/xml/services_with_tagged_priority_method.xml | 2 +- .../Fixtures/yaml/services_with_tagged_priority_method.yml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml index 184c9385e2012..d446bfc855a27 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_with_tagged_priority_method.xml @@ -6,7 +6,7 @@ - + diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml index aaf009b2a6dcb..743751f4ed52b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/services_with_tagged_priority_method.yml @@ -10,7 +10,7 @@ services: - { name: foo } foo_service_tagged_iterator: class: Bar - arguments: [!tagged { tag: foo, default_priority_method: foopriority }] + arguments: [!tagged_iterator { tag: foo, default_priority_method: foopriority }] foo_service_tagged_locator: class: Bar arguments: [!tagged_locator { tag: foo, default_priority_method: foopriority }] From 41f5e99465297441ab029abad63498b64a53cf31 Mon Sep 17 00:00:00 2001 From: pcabreus Date: Wed, 12 Jun 2019 09:19:36 -0400 Subject: [PATCH 12/12] optional priority method unless you define it --- .../Compiler/PriorityTaggedServiceTrait.php | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php index d0f9448b6cdd3..b2abe5e3f5270 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/PriorityTaggedServiceTrait.php @@ -56,14 +56,11 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container foreach ($container->findTaggedServiceIds($tagName, true) as $serviceId => $attributes) { $class = $container->getDefinition($serviceId)->getClass(); $class = $container->getParameterBag()->resolveValue($class) ?: null; - - if (null !== $r = $container->getReflectionClass($class)) { - throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); - } + $reflectionClass = $container->getReflectionClass($class); $priority = 0; - if ($defaultPriorityMethod && $r->hasMethod($defaultPriorityMethod)) { - if (!($priorityReflMethod = $r->getMethod($defaultPriorityMethod))->isStatic()) { + if (null !== $defaultPriorityMethod && null !== $reflectionClass && $reflectionClass->hasMethod($defaultPriorityMethod)) { + if (!($priorityReflMethod = $reflectionClass->getMethod($defaultPriorityMethod))->isStatic()) { throw new InvalidArgumentException(sprintf('Default priority method "%s::%s()" of the "%s"-tagged collection on service "%s" must be static.', $class, $defaultPriorityMethod, $tagName, $serviceId)); } @@ -92,9 +89,13 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container continue; } - $class = $r->name; + if (null === $reflectionClass) { + throw new InvalidArgumentException(sprintf('Class "%s" used for service "%s" cannot be found.', $class, $serviceId)); + } + + $class = $reflectionClass->name; - if (!$r->hasMethod($defaultIndexMethod)) { + if (!$reflectionClass->hasMethod($defaultIndexMethod)) { if ($needsIndexes) { $services[$priority][$serviceId] = new TypedReference($serviceId, $class); @@ -104,7 +105,7 @@ private function findAndSortTaggedServices($tagName, ContainerBuilder $container throw new InvalidArgumentException(sprintf('Method "%s::%s()" not found: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute)); } - if (!($rm = $r->getMethod($defaultIndexMethod))->isStatic()) { + if (!($rm = $reflectionClass->getMethod($defaultIndexMethod))->isStatic()) { throw new InvalidArgumentException(sprintf('Method "%s::%s()" should be static: tag "%s" on service "%s" is missing "%s" attribute.', $class, $defaultIndexMethod, $tagName, $serviceId, $indexAttribute)); }