8000 Fixing autowiring collision failure by weaverryan · Pull Request #18039 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

Fixing autowiring collision failure #18039

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

Merged
merged 1 commit into from
Mar 10, 2016
Merged
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 @@ -181,19 +181,21 @@ private function set($type, $id)
return;
}

// is this already a type/class that is known to match multiple services?
if (isset($this->ambiguousServiceTypes[$type])) {
$this->addServiceToAmbiguousType($id, $type);

return;
}

// check to make sure the type doesn't match multiple services
if (isset($this->types[$type])) {
if ($this->types[$type] === $id) {
return;
}

// keep an array of all services matching this type
if (!isset($this->ambiguousServiceTypes[$type])) {
$this->ambiguousServiceTypes[$type] = array(
$this->types[$type],
);
}
$this->ambiguousServiceTypes[$type][] = $id;
$this->addServiceToAmbiguousType($id, $type);

unset($this->types[$type]);

Expand Down Expand Up @@ -265,4 +267,15 @@ private function getReflectionClass($id, Definition $definition)
// return null
}
}

private function addServiceToAmbiguousType($id, $type)
{
// keep an array of all services matching this type
if (!isset($this->ambiguousServiceTypes[$type])) {
$this->ambiguousServiceTypes[$type] = array(
$this->types[$type],
Copy link
Member

Choose a reason for hiding this comment

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

Are we sure that there always is an entry in $this->types for $type? In set(), we later on explicitly do check for isset($this->types[$type]).

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

In that case, the call to $this->types[$type] is always skipped because of if (!isset($this->ambiguousServiceTypes[$type])) { (the line you pointed out is in a isset($this->ambiguousServiceTypes[$type]) block).

);
}
$this->ambiguousServiceTypes[$type][] = $id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,15 @@ public function testCompleteExistingDefinitionWithNotDefinedArguments()

/**
* @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 (c1, c2).
* @expectedExceptionMessage Unable to autowire argument of type "Symfony\Component\DependencyInjection\Tests\Compiler\CollisionInterface" for the service "a". Multiple services exist for this interface (c1, c2, c3).
*/
public function testTypeCollision()
{
$container = new ContainerBuilder();

$container->register('c1', __NAMESPACE__.'\CollisionA');
$container->register('c2', __NAMESPACE__.'\CollisionB');
$container->register('c3', __NAMESPACE__.'\CollisionB');
$aDefinition = $container->register('a', __NAMESPACE__.'\CannotBeAutowired');
$aDefinition->setAutowired(true);

Expand Down
0