Skip to content

[DI] Report cascades of autowiring error messages #22323

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
Apr 6, 2017
Merged
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
39 changes: 23 additions & 16 deletions src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class AutowirePass extends AbstractRecursivePass
private $types;
private $ambiguousServiceTypes = array();
private $autowired = array();
private $lastFailure;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -77,21 +78,19 @@ protected function processValue($value, $isRoot = false)
{
if ($value instanceof TypedReference) {
if ($ref = $this->getAutowiredReference($value)) {
$value = $ref;
} else {
$this->container->log($this, $this->createTypeNotFoundMessage($value->getType(), 'typed reference'));
return $ref;
}
$this->container->log($this, $this->createTypeNotFoundMessage($value->getType(), 'it'));
}
if (!$value instanceof Definition) {
return parent::processValue($value, $isRoot);
}
if (!$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
return parent::processValue($value, $isRoot);
$value = parent::processValue($value, $isRoot);

if (!$value instanceof Definition || !$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
return $value;
}
if (!$reflectionClass = $this->container->getReflectionClass($value->getClass())) {
$this->container->log($this, sprintf('Skipping service "%s": Class or interface "%s" does not exist.', $this->currentId, $value->getClass()));

return parent::processValue($value, $isRoot);
return $value;
}

$autowiredMethods = $this->getMethodsToAutowire($reflectionClass);
Expand All @@ -115,7 +114,7 @@ protected function processValue($value, $isRoot = false)
$value->setMethodCalls($methodCalls);
}

return parent::processValue($value, $isRoot);
return $value;
}

/**
Expand Down Expand Up @@ -246,9 +245,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a

if ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
} elseif ($parameter->allowsNull()) {
$value = null;
} else {
} elseif (!$parameter->allowsNull()) {
throw new RuntimeException($failureMessage);
}
$this->container->log($this, $failureMessage);
Expand Down Expand Up @@ -279,6 +276,7 @@ private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, a
*/
private function getAutowiredReference(TypedReference $reference)
{
$this->lastFailure = null;
$type = $reference->getType();

if ($type !== (string) $reference || ($this->container->has($type) && !$this->container->findDefinition($type)->isAbstract())) {
Expand Down Expand Up @@ -396,7 +394,8 @@ private function createAutowiredDefinition($type)
}

$currentId = $this->currentId;
$this->currentId = $this->autowired[$type] = $argumentId = sprintf('autowired.%s', $type);
$this->currentId = $type;
$this->autowired[$type] = $argumentId = sprintf('autowired.%s', $type);
$argumentDefinition = new Definition($type);
$argumentDefinition->setPublic(false);
$argumentDefinition->setAutowired(true);
Expand All @@ -406,7 +405,8 @@ private function createAutowiredDefinition($type)
$this->container->setDefinition($argumentId, $argumentDefinition);
} catch (RuntimeException $e) {
$this->autowired[$type] = false;
$this->container->log($this, $e->getMessage());
$this->lastFailure = $e->getMessage();
$this->container->log($this, $this->lastFailure);

return;
} finally {
Expand All @@ -427,7 +427,14 @@ private function createTypeNotFoundMessage($type, $label)
$message = sprintf('references %s "%s" but %s.%s', $r->isInterface() ? 'interface' : 'class', $type, $message, $this->createTypeAlternatives($type));
}

return sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message);
$message = sprintf('Cannot autowire service "%s": %s %s', $this->currentId, $label, $message);

if (null !== $this->lastFailure) {
$message = $this->lastFailure."\n".$message;
$this->lastFailure = null;
}

return $message;
}

private function createTypeAlternatives($type)
Expand Down