[DI] Exclude inline services declared in XML from autowiring candidates

This commit is contained in:
Nicolas Grekas 2017-10-08 16:29:36 +02:00
parent b43bdf398d
commit d90e7212ea
4 changed files with 27 additions and 2 deletions

View File

@ -345,7 +345,7 @@ class AutowirePass extends AbstractRecursivePass
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;
}

View File

@ -412,7 +412,7 @@ class XmlFileLoader extends FileLoader
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);

View File

@ -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 @@ class AutowirePassTest extends TestCase
$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());
}
}

View File

@ -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>