8000 [DI] Autowiring and factories are incompatible with each others · symfony/symfony@554bdd2 · 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 554bdd2

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

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
@@ -284,6 +284,10 @@ private function getReflectionClass($id, Definition $definition)
284284
return $this->reflectionClasses[$id];
285285
}
286286

287+
if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
288+
throw new RuntimeException(sprintf('Service "%s" can use either autowiring or a factory, not both.', $id));
289+
}
290+
287291
// Cannot use reflection if the class isn't set
288292
if (!$class = $definition->getClass()) {
289293
return false;

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