Skip to content

Commit

Permalink
bug #22258 [DI] Autowiring and factories are incompatible with each o…
Browse files Browse the repository at this point in the history
…thers (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
  • Loading branch information
fabpot committed Apr 3, 2017
2 parents ee10bf2 + 9b60163 commit bad24d3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
Expand Up @@ -74,6 +74,10 @@ public function process(ContainerBuilder $container)
*/
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;
}
Expand Down
Expand Up @@ -465,6 +465,22 @@ public function provideAutodiscoveredAutowiringOrder()
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
Expand Down

0 comments on commit bad24d3

Please sign in to comment.