[DependencyInjection] Autowing: exclude abstract definitons

This commit is contained in:
Kévin Dunglas 2015-11-03 16:40:26 -08:00
parent 71d502a174
commit faefc6074f
2 changed files with 21 additions and 0 deletions

View File

@ -138,11 +138,17 @@ class AutowirePass implements CompilerPassInterface
*/ */
private function populateAvailableType($id, Definition $definition) private function populateAvailableType($id, Definition $definition)
{ {
// Never use abstract services
if ($definition->isAbstract()) {
return;
}
foreach ($definition->getAutowiringTypes() as $type) { foreach ($definition->getAutowiringTypes() as $type) {
$this->definedTypes[$type] = true; $this->definedTypes[$type] = true;
$this->types[$type] = $id; $this->types[$type] = $id;
} }
// Cannot use reflection if the class isn't set
if (!$definition->getClass()) { if (!$definition->getClass()) {
return; return;
} }

View File

@ -216,6 +216,21 @@ class AutowirePassTest extends \PHPUnit_Framework_TestCase
$pass = new AutowirePass(); $pass = new AutowirePass();
$pass->process($container); $pass->process($container);
} }
public function testDontUseAbstractServices()
{
$container = new ContainerBuilder();
$container->register('abstract_foo', __NAMESPACE__.'\Foo')->setAbstract(true);
$container->register('foo', __NAMESPACE__.'\Foo');
$container->register('bar', __NAMESPACE__.'\Bar')->setAutowired(true);
$pass = new AutowirePass();
$pass->process($container);
$arguments = $container->getDefinition('bar')->getArguments();
$this->assertSame('foo', (string) $arguments[0]);
}
} }
class Foo class Foo