Skip to content

Commit

Permalink
bug #24491 [DI] Exclude inline services declared in XML from autowiri…
Browse files Browse the repository at this point in the history
…ng 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 | #24311
| License       | MIT
| Doc PR        | -

As reported in #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
  • Loading branch information
fabpot committed Oct 9, 2017
2 parents e241897 + d90e721 commit f756fb8
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 2 deletions.
Expand Up @@ -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;
}

Expand Down
Expand Up @@ -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);

Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<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">
<services>
<service id="foo" class="stdClass">
<argument type="service">
<service class="Symfony\Component\DependencyInjection\Tests\Compiler\D"/>
</argument>
</service>
<service id="autowired" class="Symfony\Component\DependencyInjection\Tests\Compiler\E" autowire="true"/>
</services>
</container>

0 comments on commit f756fb8

Please sign in to comment.