[DependencyInjection] Fix autowiring collisions detection

This commit is contained in:
Guilhem Niot 2017-02-18 18:58:12 +01:00
parent 6f578ee514
commit bb70472dce
No known key found for this signature in database
GPG Key ID: 7A655BEB5BDC1CA6

View File

@ -28,6 +28,7 @@ class AutowirePass implements CompilerPassInterface
private $definedTypes = array(); private $definedTypes = array();
private $types; private $types;
private $notGuessableTypes = array(); private $notGuessableTypes = array();
private $usedTypes = array();
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -44,6 +45,15 @@ class AutowirePass implements CompilerPassInterface
$this->completeDefinition($id, $definition); $this->completeDefinition($id, $definition);
} }
} }
foreach ($this->usedTypes as $type => $id) {
if (isset($this->usedTypes[$type]) && isset($this->notGuessableTypes[$type])) {
$classOrInterface = class_exists($type) ? 'class' : 'interface';
$matchingServices = implode(', ', $this->types[$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));
}
}
} catch (\Exception $e) { } catch (\Exception $e) {
} catch (\Throwable $e) { } catch (\Throwable $e) {
} }
@ -56,6 +66,7 @@ class AutowirePass implements CompilerPassInterface
$this->definedTypes = array(); $this->definedTypes = array();
$this->types = null; $this->types = null;
$this->notGuessableTypes = array(); $this->notGuessableTypes = array();
$this->usedTypes = array();
if (isset($e)) { if (isset($e)) {
throw $e; throw $e;
@ -109,9 +120,11 @@ class AutowirePass implements CompilerPassInterface
if (isset($this->types[$typeHint->name]) && !isset($this->notGuessableTypes[$typeHint->name])) { if (isset($this->types[$typeHint->name]) && !isset($this->notGuessableTypes[$typeHint->name])) {
$value = new Reference($this->types[$typeHint->name]); $value = new Reference($this->types[$typeHint->name]);
$this->usedTypes[$typeHint->name] = $id;
} else { } else {
try { try {
$value = $this->createAutowiredDefinition($typeHint, $id); $value = $this->createAutowiredDefinition($typeHint, $id);
$this->usedTypes[$typeHint->name] = $id;
} catch (RuntimeException $e) { } catch (RuntimeException $e) {
if ($parameter->allowsNull()) { if ($parameter->allowsNull()) {
$value = null; $value = null;