8000 bug #22258 [DI] Autowiring and factories are incompatible with each o… · symfony/symfony@bad24d3 · GitHub
[go: up one dir, main page]

Skip to content

Commit bad24d3

Browse files
committed
bug #22258 [DI] Autowiring and factories are incompatible with each others (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [DI] Autowiring and factories are incompatible with each others | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Commits ------- 9b60163 [DI] Autowiring and factories are incompatible with each others
2 parents ee10bf2 + 9b60163 commit bad24d3

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
@@ -74,6 +74,10 @@ public function process(ContainerBuilder $container)
7474
*/
7575
private function completeDefinition($id, Definition $definition)
7676
{
77+
if ($definition->getFactory() || $definition->getFactoryClass(false) || $definition->getFactoryService(false)) {
78+
throw new RuntimeException(sprintf('Service "%s" can use either autowiring or a factory, not both.', $id));
79+
}
80+
7781
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
7882
return;
7983
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/AutowirePassTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -465,6 +465,22 @@ public function provideAutodiscoveredAutowiringOrder()
465465
array('CannotBeAutowiredReverseOrder'),
466466
);
467467
}
468+
469+
/**
470+
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
471+
* @expectedExceptionMessage Service "a" can use either autowiring or a factory, not both.
472+
*/
473+
public function testWithFactory()
474+
{
475+
$container = new ContainerBuilder();
476+
477+
$container->register('a', __NAMESPACE__.'\A')
478+
->setFactory('foo')
479+
->setAutowired(true);
480+
481+
$pass = new AutowirePass();
482+
$pass->process($container);
483+
}
468484
}
469485

470486
class Foo

0 commit comments

Comments
 (0)
0