8000 [DI] Autowiring and factories are incompatible with each others · symfony/symfony@9b60163 · GitHub
[go: up one dir, main page]

Skip to content
Sign in

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9b60163

Browse files
[DI] Autowiring and factories are incompatible with each others
1 parent 994e90c commit 9b60163

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/Symfony/Component/DependencyInjection/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
}

src/Symfony/Component/DependencyInjection/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)
0