[DI] Dont tell about autoregistration in strict autowiring mode

This commit is contained in:
Nicolas Grekas 2018-03-22 11:04:59 +01:00
parent 7af20bceae
commit 5e922db4de

View File

@ -30,7 +30,7 @@ class AutowirePass extends AbstractRecursivePass
{ {
private $definedTypes = array(); private $definedTypes = array();
private $types; private $types;
private $ambiguousServiceTypes = array(); private $ambiguousServiceTypes;
private $autowired = array(); private $autowired = array();
private $lastFailure; private $lastFailure;
private $throwOnAutowiringException; private $throwOnAutowiringException;
@ -71,7 +71,7 @@ class AutowirePass extends AbstractRecursivePass
} finally { } finally {
$this->definedTypes = array(); $this->definedTypes = array();
$this->types = null; $this->types = null;
$this->ambiguousServiceTypes = array(); $this->ambiguousServiceTypes = null;
$this->autowired = array(); $this->autowired = array();
} }
} }
@ -286,7 +286,7 @@ class AutowirePass extends AbstractRecursivePass
} }
if (null === $this->types) { if (null === $this->types) {
$this->populateAvailableTypes(); $this->populateAvailableTypes($this->strictMode);
} }
if (isset($this->definedTypes[$type])) { if (isset($this->definedTypes[$type])) {
@ -322,12 +322,15 @@ class AutowirePass extends AbstractRecursivePass
/** /**
* Populates the list of available types. * Populates the list of available types.
*/ */
private function populateAvailableTypes() private function populateAvailableTypes($onlyAutowiringTypes = false)
{ {
$this->types = array(); $this->types = array();
if (!$onlyAutowiringTypes) {
$this->ambiguousServiceTypes = array();
}
foreach ($this->container->getDefinitions() as $id => $definition) { foreach ($this->container->getDefinitions() as $id => $definition) {
$this->populateAvailableType($id, $definition); $this->populateAvailableType($id, $definition, $onlyAutowiringTypes);
} }
} }
@ -337,7 +340,7 @@ class AutowirePass extends AbstractRecursivePass
* @param string $id * @param string $id
* @param Definition $definition * @param Definition $definition
*/ */
private function populateAvailableType($id, Definition $definition) private function populateAvailableType($id, Definition $definition, $onlyAutowiringTypes)
{ {
// Never use abstract services // Never use abstract services
if ($definition->isAbstract()) { if ($definition->isAbstract()) {
@ -350,6 +353,10 @@ class AutowirePass extends AbstractRecursivePass
unset($this->ambiguousServiceTypes[$type]); unset($this->ambiguousServiceTypes[$type]);
} }
if ($onlyAutowiringTypes) {
return;
}
if (preg_match('/^\d+_[^~]++~[._a-zA-Z\d]{7}$/', $id) || $definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) { if (preg_match('/^\d+_[^~]++~[._a-zA-Z\d]{7}$/', $id) || $definition->isDeprecated() || !$reflectionClass = $this->container->getReflectionClass($definition->getClass(), false)) {
return; return;
} }
@ -479,12 +486,15 @@ class AutowirePass extends AbstractRecursivePass
if ($message = $this->getAliasesSuggestionForType($type = $reference->getType())) { if ($message = $this->getAliasesSuggestionForType($type = $reference->getType())) {
return ' '.$message; return ' '.$message;
} }
if (null === $this->ambiguousServiceTypes) {
$this->populateAvailableTypes();
}
if (isset($this->ambiguousServiceTypes[$type])) { if (isset($this->ambiguousServiceTypes[$type])) {
$message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type])); $message = sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
} elseif (isset($this->types[$type])) { } elseif (isset($this->types[$type])) {
$message = sprintf('the existing "%s" service', $this->types[$type]); $message = sprintf('the existing "%s" service', $this->types[$type]);
} elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered()) { } elseif ($reference->getRequiringClass() && !$reference->canBeAutoregistered() && !$this->strictMode) {
return ' It cannot be auto-registered because it is from a different root namespace.'; return ' It cannot be auto-registered because it is from a different root namespace.';
} else { } else {
return; return;