[DI] Autowiring and factories are incompatible with each others
This commit is contained in:
parent
994e90c129
commit
9b601633a7
@ -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;
|
||||
}
|
||||
|
@ -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
|
||||
|
Reference in New Issue
Block a user