Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Autowiring] Add interface FQCN as a valid service name to avoid collisions #21132

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 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -338,11 +338,15 @@ private function set($type, $id)
*/
private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
{
if (isset($this->ambiguousServiceTypes[$typeHint->name])) {
if (isset($this->ambiguousServiceTypes[$typeHintName = $typeHint->name])) {
Copy link
Member

Choose a reason for hiding this comment

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

Why is necessary to create the $typeHintName variable? It makes the code harder to read for no benefit over using the prop directly.

if ($this->container->has($typeHintName)) {
return new Reference($typeHintName);
}

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

throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices), 1);
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHintName, $id, $classOrInterface, $matchingServices));
}

if (!$typeHint->isInstantiable()) {
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', __NAMESPACE__.'\CollisionA');
Copy link
Member

Choose a reason for hiding this comment

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

You can use CollisionA::class for Symfony 3+. Same of lines below.

$container->register('c2', __NAMESPACE__.'\CollisionB');
$container->register('c3', __NAMESPACE__.'\CollisionB');

$container->register(__NAMESPACE__.'\CollisionInterface', 'c3');

$aDefinition = $container->register('a', __NAMESPACE__.'\AutowiredInterface');
$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
Morty Proxy This is a proxified and sanitized view of the page, visit original site.