Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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 @@ -339,6 +339,10 @@ private function set($type, $id)
private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
{
if (isset($this->ambiguousServiceTypes[$typeHint->name])) {
if ($this->container->has($typeHint->name)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't this be moved here to avoid building the map when not necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, gonna try

return new Reference($typeHint->name);
}

$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
$matchingServices = implode(', ', $this->ambiguousServiceTypes[$typeHint->name]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ public function testTypeCollision()
$pass->process($container);
}

public function testTypeCollisionWithInterfaceAsServiceId()
{
$container = new ContainerBuilder();

$container->register('c1', CollisionA::class);
$container->register('c2', CollisionB::class);
$container->register('c3', CollisionB::class);

$container->register(CollisionInterface::class, 'c3');

$aDefinition = $container->register('a', AutowiredInterface::class);
$aDefinition->setAutowired(true);

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

$aDefinition = $container->getDefinition('a');

$this->assertCount(1, $aDefinition->getArguments());
$this->assertEquals(new Reference(__NAMESPACE__.'\CollisionInterface'), $aDefinition->getArgument(0));
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\Foo" for the service "a". Multiple services exist for this class (a1, a2).
Expand Down Expand Up @@ -656,6 +678,16 @@ public function __construct(CollisionInterface $collision)
}
}

class AutowiredInterface
{
public $collision;

public function __construct(CollisionInterface $collision)
{
$this->collision = $collision;
}
}

class Lille
{
}
Expand Down