diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 7aa2d2a64e80d..cea19ddfa0cc6 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -339,6 +339,10 @@ private function set($type, $id) private function createAutowiredDefinition(\ReflectionClass $typeHint, $id) { if (isset($this->ambiguousServiceTypes[$typeHint->name])) { + if ($this->container->has($typeHint->name)) { + return new Reference($typeHint->name); + } + $classOrInterface = $typeHint->isInterface() ? 'interface' : 'class'; $matchingServices = implode(', ', $this->ambiguousServiceTypes[$typeHint->name]); diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 0b8ab9374afe4..79e7e3431b0c1 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -119,6 +119,28 @@ public function testTypeCollision() $pass->process($container); } + public function testTypeCollisionWithInterfaceAsServiceId() + { + $container = new ContainerBuilder(); + + $container->register('c1', CollisionA::class); + $container->register('c2', CollisionB::class); + $container->register('c3', CollisionB::class); + + $container->register(CollisionInterface::class, 'c3'); + + $aDefinition = $container->register('a', AutowiredInterface::class); + $aDefinition->setAutowired(true); + + $pass = new AutowirePass(); + $pass->process($container); + + $aDefinition = $container->getDefinition('a'); + + $this->assertCount(1, $aDefinition->getArguments()); + $this->assertEquals(new Reference(__NAMESPACE__.'\CollisionInterface'), $aDefinition->getArgument(0)); + } + /** * @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException * @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" for the service "a". Multiple services exist for this class (a1, a2). @@ -656,6 +678,16 @@ public function __construct(CollisionInterface $collision) } } +class AutowiredInterface +{ + public $collision; + + public function __construct(CollisionInterface $collision) + { + $this->collision = $collision; + } +} + class Lille { }