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

Commit ec01b4d

Browse filesBrowse files
committed
[DependencyInjection] Fix autowiring collisions detection
1 parent 6f578ee commit ec01b4d
Copy full SHA for ec01b4d

File tree

1 file changed

+20
-4
lines changed
Filter options

1 file changed

+20
-4
lines changed

‎src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
+20-4Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ class AutowirePass implements CompilerPassInterface
2828
private $definedTypes = array();
2929
private $types;
3030
private $notGuessableTypes = array();
31+
private $usedTypes = array();
3132

3233
/**
3334
* {@inheritdoc}
@@ -56,6 +57,7 @@ public function process(ContainerBuilder $container)
5657
$this->definedTypes = array();
5758
$this->types = null;
5859
$this->notGuessableTypes = array();
60+
$this->usedTypes = array();
5961

6062
if (isset($e)) {
6163
throw $e;
@@ -109,9 +111,11 @@ private function completeDefinition($id, Definition $definition)
109111

110112
if (isset($this->types[$typeHint->name]) && !isset($this->notGuessableTypes[$typeHint->name])) {
111113
$value = new Reference($this->types[$typeHint->name]);
114+
$this->usedTypes[$typeHint->name] = $id;
112115
} else {
113116
try {
114117
$value = $this->createAutowiredDefinition($typeHint, $id);
118+
$this->usedTypes[$typeHint->name] = $id;
115119
} catch (RuntimeException $e) {
116120
if ($parameter->allowsNull()) {
117121
$value = null;
@@ -158,6 +162,8 @@ private function populateAvailableTypes()
158162
*
159163
* @param string $id
160164
* @param Definition $definition
165+
*
166+
* @return array
161167
*/
162168
private function populateAvailableType($id, Definition $definition)
163169
{
@@ -172,16 +178,19 @@ private function populateAvailableType($id, Definition $definition)
172178
}
173179

174180
if (!$reflectionClass = $this->getReflectionClass($id, $definition)) {
175-
return;
181+
return array();
176182
}
177183

184+
$types = array();
178185
foreach ($reflectionClass->getInterfaces() as $reflectionInterface) {
179-
$this->set($reflectionInterface->name, $id);
186+
$this->set($types[] = $reflectionInterface->name, $id);
180187
}
181188

182189
do {
183-
$this->set($reflectionClass->name, $id);
190+
$this->set($types[] = $reflectionClass->name, $id);
184191
} while ($reflectionClass = $reflectionClass->getParentClass());
192+
193+
return $types;
185194
}
186195

187196
/**
@@ -243,7 +252,14 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id)
243252
$argumentDefinition = $this->container->register($argumentId, $typeHint->name);
244253
$argumentDefinition->setPublic(false);
245254

246-
$this->populateAvailableType($argumentId, $argumentDefinition);
255+
foreach ($this->populateAvailableType($argumentId, $argumentDefinition) as $type) {
256+
if (isset($this->usedTypes[$type]) && isset($this->notGuessableTypes[$type])) {
257+
$classOrInterface = interface_exists($type) ? 'interface' : 'class';
258+
$matchingServices = implode(', ', $this->types[$type]);
259+
260+
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $type, $this->usedTypes[$type], $classOrInterface, $matchingServices), 1);
261+
}
262+
}
247263

248264
try {
249265
$this->completeDefinition($argumentId, $argumentDefinition);

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.