8000 bug #24491 [DI] Exclude inline services declared in XML from autowiri… · pierredup/symfony@f756fb8 · GitHub
[go: up one dir, main page]

Skip to content

Commit f756fb8

Browse files
committed
bug symfony#24491 [DI] Exclude inline services declared in XML from autowiring candidates (nicolas-grekas)
This PR was merged into the 3.4 branch. Discussion ---------- [DI] Exclude inline services declared in XML from autowiring candidates | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | yes | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#24311 | License | MIT | Doc PR | - As reported in symfony#24311, inline services should not be candidates for autowiring. This PR fixes the issue, but is submitted against 3.4 because there is a potential BC break here, for ppl that didn't realize they relied on this (already deprecated) behavior. We *could* not merge this PR and consider the deprecation is fine - but in practice, the WTF is hitting several ppl already AFAIK, so we should close that door IMHO. Commits ------- d90e721 [DI] Exclude inline services declared in XML from autowiring candidates
2 parents e241897 + d90e721 commit f756fb8

File tree

4 files changed

+27
-2
lines changed

4 files changed

+27
-2
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private function populateAvailableType($id, Definition $definition)
345345
unset($this->ambiguousServiceTypes[$type]);
346346
}
347347

348-
if ($definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) {
348+
if (preg_match('/^\d+_[^~]++~[._a-zA-Z\d]{7}$/', $id) || $definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) {
349349
return;
350350
}
351351

src/Symfony/Component/DependencyInjection/Loader/XmlFileLoader.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ private function processAnonymousServices(\DOMDocument $xml, $file, $defaults)
412412
foreach ($nodes as $node) {
413413
if ($services = $this->getChildren($node, 'service')) {
414414
// give it a unique name
415-
$id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $node->getAttribute('class')).$suffix);
415+
$id = sprintf('%d_%s', ++$count, preg_replace('/^.*\\\\/', '', $services[0]->getAttribute('class')).'~'.$suffix);
416416
$node->setAttribute('id', $id);
417417
$node->setAttribute('service', $id);
418418

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

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

1414
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Config\FileLocator;
1516
use Symfony\Component\DependencyInjection\Compiler\AutowireRequiredMethodsPass;
1617
use Symfony\Component\DependencyInjection\Compiler\AutowirePass;
1718
use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
1819
use Symfony\Component\DependencyInjection\ContainerBuilder;
1920
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
21+
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
2022
use Symfony\Component\DependencyInjection\Reference;
2123
use Symfony\Component\DependencyInjection\Tests\Fixtures\includes\FooVariadic;
2224
use Symfony\Component\DependencyInjection\TypedReference;
@@ -869,4 +871,16 @@ public function testExceptionWhenAliasDoesNotExist()
869871
$pass = new AutowirePass();
870872
$pass->process($container);
871873
}
874+
875+
public function testInlineServicesAreNotCandidates()
876+
{
877+
$container = new ContainerBuilder();
878+
$loader = new XmlFileLoader($container, new FileLocator(realpath(__DIR__.'/../Fixtures/xml')));
879+
$loader->load('services_inline_not_candidate.xml');
880+
881+
$pass = new AutowirePass();
882+
$pass->process($container);
883+
884+
$this->assertSame(array(), $container->getDefinition('autowired')->getArguments());
885+
}
90FF 872886
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<container xmlns="http://symfony.com/schema/dic/services" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
3+
<services>
4+
<service id="foo" class="stdClass">
5+
<argument type="service">
6+
<service class="Symfony\Component\DependencyInjection\Tests\Compiler\D"/>
7+
</argument>
8+
</service>
9+
<service id="autowired" class="Symfony\Component\DependencyInjection\Tests\Compiler\E" autowire="true"/>
10+
</services>
11+
</container>

0 commit comments

Comments
 (0)
0