From 9b601633a724bf50d8479bf70e46bd33dc19ae5c Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 3 Apr 2017 20:00:43 +0200 Subject: [PATCH] [DI] Autowiring and factories are incompatible with each others --- .../Compiler/AutowirePass.php | 4 ++++ .../Tests/Compiler/AutowirePassTest.php | 16 ++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php index 9454d1bd4b..8acf2856d5 100644 --- a/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php +++ b/src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php @@ -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; } diff --git a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php index 047801ceba..6c17fcd66b 100644 --- a/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php +++ b/src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php @@ -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