[DI] Autowiring and factories are incompatible with each others

This commit is contained in:
Nicolas Grekas 2017-04-03 20:00:43 +02:00
parent 994e90c129
commit 9b601633a7
2 changed files with 20 additions and 0 deletions

View File

@ -83,6 +83,10 @@ class AutowirePass implements CompilerPassInterface
*/
private function completeDefinition($id, Definition $definition)
{
if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
throw new RuntimeException(sprintf('Service "%s" can use either autowiring or a factory, not both.', $id));
}
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
return;
}

View File

@ -486,6 +486,22 @@ class AutowirePassTest extends TestCase
array('CannotBeAutowiredReverseOrder'),
);
}
/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Service "a" can use either autowiring or a factory, not both.
*/
public function testWithFactory()
{
$container = new ContainerBuilder();
$container->register('a', __NAMESPACE__.'\A')
->setFactory('foo')
->setAutowired(true);
$pass = new AutowirePass();
$pass->process($container);
}
}
class Foo