From d90e7212ea359d57b904b5fbf22c9199ddc762d9 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Sun, 8 Oct 2017 16:29:36 +0200 Subject: [PATCH] [DI] Exclude inline services declared in XML from autowiring candidates --- .../DependencyInjection/Compiler/AutowirePass.php | 2 +- .../DependencyInjection/Loader/XmlFileLoader.php | 2 +- .../Tests/Compiler/AutowirePassTest.php | 14 ++++++++++++++ .../Fixtures/xml/services_inline_not_candidate.xml | 11 +++++++++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_inline_not_candidate.xml diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index b7bcb1309c1f..98293a319822 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -345,7 +345,7 @@ private function populateAvailableType($id, Definition $definition) unset($this->ambiguousServiceTypes[$type]); } - if ($definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) { + if (preg_match('/^\d+_[^~]++~[._a-zA-Z\d]{7}$/', $id) || $definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) { return; } diff --git a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php index e5a43e391d20..c5ff57e97a84 100644 --- a/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php @@ -412,7 +412,7 @@ private function processAnonymousServices(\DOMDocument $xml, $file, $defaults) foreach ($nodes as $node) { if ($services = $this->getChildren($node, 'service')) { // give it a unique name - $id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $node->getAttribute('class')).$suffix); + $id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')).'~'.$suffix); $node->setAttribute('id', $id); $node->setAttribute('service', $id); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 2c68eb3d4c94..a4a1cdb9b251 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -12,11 +12,13 @@ namespace Symfony\Component\DependencyInjection\Tests\Compiler; use PHPUnit\Framework\TestCase; +use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass; use Symfony\Component\DependencyInjection\Compiler\AutowirePass; use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\Exception\RuntimeException; +use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic; use Symfony\Component\DependencyInjection\TypedReference; @@ -869,4 +871,16 @@ public function testExceptionWhenAliasDoesNotExist() $pass = new AutowirePass(); $pass->process($container); } + + public function testInlineServicesAreNotCandidates() + { + $container = new ContainerBuilder(); + $loader = new XmlFileLoader($container, new FileLocator(realpath(__DIR__.'/../Fixtures/xml'))); + $loader->load('services_inline_not_candidate.xml'); + + $pass = new AutowirePass(); + $pass->process($container); + + $this->assertSame(array(), $container->getDefinition('autowired')->getArguments()); + } } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_inline_not_candidate.xml b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_inline_not_candidate.xml new file mode 100644 index 000000000000..72560585396a --- /dev/null +++ b/src/Symfony/Component/DependencyInjection/Tests/Fixtures/xml/services_inline_not_candidate.xml @@ -0,0 +1,11 @@ + + + + + + + + + + +