Skip to content

Commit 811bef4

Browse files
[DI] Autowiring and factories are incompatible with each others
1 parent a5f3f12 commit 811bef4

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

Compiler/AutowirePass.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,10 @@ public function process(ContainerBuilder $container)
8383
*/
8484
private function completeDefinition($id, Definition $definition)
8585
{
86+
if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
87+
throw new RuntimeException(sprintf('Service "%s" can use either autowiring or a factory, not both.', $id));
88+
}
89+
8690
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
8791
return;
8892
}

Tests/Compiler/AutowirePassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,22 @@ public function provideAutodiscoveredAutowiringOrder()
486486
array('CannotBeAutowiredReverseOrder'),
487487
);
488488
}
489+
490+
/**
491+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
492+
* @expectedExceptionMessage Service "a" can use either autowiring or a factory, not both.
493+
*/
494+
public function testWithFactory()
495+
{
496+
$container = new ContainerBuilder();
497+
498+
$container->register('a', __NAMESPACE__.'\A')
499+
->setFactory('foo')
500+
->setAutowired(true);
501+
502+
$pass = new AutowirePass();
503+
$pass->process($container);
504+
}
489505
}
490506

491507
class Foo

0 commit comments

Comments
 (0)