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:
Fabien Potencier 2017-04-03 15:14:48 -07:00
commit bad24d3935
2 changed files with 20 additions and 0 deletions

View File

@ -74,6 +74,10 @@ class AutowirePass implements CompilerPassInterface
*/
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)) {
return;
}

View File

@ -465,6 +465,22 @@ class AutowirePassTest extends TestCase
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