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
-------
9b601633a7
[DI] Autowiring and factories are incompatible with each others
This commit is contained in:
commit
bad24d3935
@ -74,6 +74,10 @@ class AutowirePass implements CompilerPassInterface
|
|||||||
*/
|
*/
|
||||||
private function completeDefinition($id, Definition $definition)
|
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)) {
|
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -465,6 +465,22 @@ class AutowirePassTest extends TestCase
|
|||||||
array('CannotBeAutowiredReverseOrder'),
|
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
|
class Foo
|
||||||
|
Reference in New Issue
Block a user