Skip to content

Merge branch 2.8 into 3.2 #21673

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class AutowirePass implements CompilerPassInterface
private $definedTypes = array();
private $types;
private $ambiguousServiceTypes = array();
private $usedTypes = array();

/**
* {@inheritdoc}
Expand All @@ -45,6 +46,15 @@ public function process(ContainerBuilder $container)
$this->completeDefinition($id, $definition);
}
}

foreach ($this->usedTypes as $type => $id) {
if (isset($this->usedTypes[$type]) && isset($this->ambiguousServiceTypes[$type])) {
$classOrInterface = class_exists($type) ? 'class' : 'interface';
$matchingServices = implode(', ', $this->ambiguousServiceTypes[$type]);

throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $type, $id, $classOrInterface, $matchingServices));
}
}
} finally {
spl_autoload_unregister($throwingAutoloader);

Expand All @@ -54,6 +64,7 @@ public function process(ContainerBuilder $container)
$this->definedTypes = array();
$this->types = null;
$this->ambiguousServiceTypes = array();
$this->usedTypes = array();
}
}

Expand Down Expand Up @@ -128,9 +139,11 @@ private function completeDefinition($id, Definition $definition)

if (isset($this->types[$typeHint->name])) {
$value = new Reference($this->types[$typeHint->name]);
$this->usedTypes[$typeHint->name] = $id;
} else {
try {
$value = $this->createAutowiredDefinition($typeHint, $id);
$this->usedTypes[$typeHint->name] = $id;
} catch (RuntimeException $e) {
if ($parameter->allowsNull()) {
$value = null;
Expand Down Expand Up @@ -188,6 +201,7 @@ private function populateAvailableType($id, Definition $definition)
foreach ($definition->getAutowiringTypes() as $type) {
$this->definedTypes[$type] = true;
$this->types[$type] = $id;
unset($this->ambiguousServiceTypes[$type]);
}

if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,15 +191,16 @@ public function testTypeNotGuessableWithTypeSet()
$container = new ContainerBuilder();

$container->register('a1', __NAMESPACE__.'\Foo');
$container->register('a2', __NAMESPACE__.'\Foo')->addAutowiringType(__NAMESPACE__.'\Foo');
$container->register('a2', __NAMESPACE__.'\Foo');
$container->register('a3', __NAMESPACE__.'\Foo')->addAutowiringType(__NAMESPACE__.'\Foo');
$aDefinition = $container->register('a', __NAMESPACE__.'\NotGuessableArgument');
$aDefinition->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);

$this->assertCount(1, $container->getDefinition('a')->getArguments());
$this->assertEquals('a2', (string) $container->getDefinition('a')->getArgument(0));
$this->assertEquals('a3', (string) $container->getDefinition('a')->getArgument(0));
}

public function testWithTypeSet()
Expand Down Expand Up @@ -510,6 +511,31 @@ public function testEmptyStringIsKept()

$this->assertEquals(array(new Reference('a'), '', new Reference('lille')), $container->getDefinition('foo')->getArguments());
}

/**
* @dataProvider provideAutodiscoveredAutowiringOrder
*
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMEssage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". Multiple services exist for this interface (autowired.Symfony\Component\DependencyInjection\Tests\Compiler\CollisionA, autowired.Symfony\Component\DependencyInjection\Tests\Compiler\CollisionB).
*/
public function testAutodiscoveredAutowiringOrder($class)
{
$container = new ContainerBuilder();

$container->register('a', __NAMESPACE__.'\\'.$class)
->setAutowired(true);

$pass = new AutowirePass();
$pass->process($container);
}

public function provideAutodiscoveredAutowiringOrder()
{
return array(
array('CannotBeAutowiredForwardOrder'),
array('CannotBeAutowiredReverseOrder'),
);
}
}

class Foo
Expand Down Expand Up @@ -591,6 +617,20 @@ public function __construct(CollisionInterface $collision)
}
}

class CannotBeAutowiredForwardOrder
{
public function __construct(CollisionA $a, CollisionInterface $b, CollisionB $c)
{
}
}

class CannotBeAutowiredReverseOrder
{
public function __construct(CollisionA $a, CollisionB $c, CollisionInterface $b)
{
}
}

class Lille
{
}
Expand Down