10000 [DI] Add hints to exceptions thrown by AutowiringPass by nicolas-grekas · Pull Request #22135 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI] Add hints to exceptions thrown by AutowiringPass #22135

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 24, 2017
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
[DI] Add hints to exceptions thrown by AutowiringPass
  • Loading branch information
nicolas-grekas committed Mar 24, 2017
commit d7557cf975728744101111300a2d6c51248f5c57
96 changes: 68 additions & 28 deletions src/Symfony/Component/DependencyInjection/Compiler/AutowirePass.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ public function process(ContainerBuilder $container)
continue;
}

$classOrInterface = class_exists($type) ? 'class' : 'interface';
$matchingServices = implode(', ', $this->ambiguousServiceTypes[$type]);
$this->container = $container;
$classOrInterface = class_exists($type, false) ? 'class' : 'interface';

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));
throw new RuntimeException(sprintf('Cannot autowire service "%s": multiple candidate services exist for %s "%s".%s', $id, $classOrInterface, $type, $this->createTypeAlternatives($type)));
}
} finally {
// Free memory
$this->container = null;
$this->definedTypes = array();
$this->types = null;
$this->ambiguousServiceTypes = array();
Expand Down Expand Up @@ -108,7 +108,12 @@ protected function processValue($value, $isRoot = false)
$this->currentDefinition = $value;

try {
if (!$value->isAutowired() || !$reflectionClass = $this->container->getReflectionClass($value->getClass())) {
if (!$value->isAutowired() || $value->isAbstract() || !$value->getClass()) {
return parent::processValue($value, $isRoot);
}
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);
}

Expand All @@ -117,8 +122,6 @@ protected function processValue($value, $isRoot = false)

if ($constructor = $reflectionClass->getConstructor()) {
array_unshift($methodCalls, array($constructor->name, $value->getArguments()));
} elseif ($value->getArguments()) {
throw new RuntimeException(sprintf('Cannot autowire service "%s": class %s has no constructor but arguments are defined.', $this->currentId, $reflectionClass->name));
}

$methodCalls = $this->autowireCalls($reflectionClass, $methodCalls, $autowiredMethods);
Expand Down Expand Up @@ -203,11 +206,13 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
unset($autowiredMethods[$lcMethod]);
} else {
if (!$reflectionClass->hasMethod($method)) {
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s::%s() does not exist.', $this->currentId, $reflectionClass->name, $method));
$class = $reflectionClass->name;
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s() does not exist.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
}
$reflectionMethod = $reflectionClass->getMethod($method);
if (!$reflectionMethod->isPublic()) {
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s::%s() must be public.', $this->currentId, $reflectionClass->name, $method));
$class = $reflectionClass->name;
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s() must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
}
}

Expand All @@ -222,10 +227,13 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
if (!$reflectionMethod->getNumberOfParameters()) {
continue; // skip getters
}
$method = $reflectionMethod->name;

if (!$reflectionMethod->isPublic()) {
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s::%s() must be public.', $this->currentId, $reflectionClass->name, $reflectionMethod->name));
$class = $reflectionClass->name;
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s() must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
}
$methodCalls[] = array($reflectionMethod->name, $this->autowireMethod($reflectionMethod, array()));
$methodCalls[] = array($method, $this->autowireMethod($reflectionMethod, array()));
}

return $methodCalls;
Expand All @@ -244,9 +252,11 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC
private function autowireMethod(\ReflectionMethod $reflectionMethod, array $arguments)
{
$isConstructor = $reflectionMethod->isConstructor();
$class = $reflectionMethod->class;
$method = $reflectionMethod->name;

if (!$isConstructor && !$arguments && !$reflectionMethod->getNumberOfRequiredParameters()) {
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s::%s() has only optional arguments, thus must be wired explicitly.', $this->currentId, $reflectionMethod->class, $reflectionMethod->name));
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s() has only optional arguments, thus must be wired explicitly.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method));
}

foreach ($reflectionMethod->getParameters() as $index => $parameter) {
Expand All @@ -265,7 +275,7 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
if (!$type) {
// no default value? Then fail
if (!$parameter->isOptional()) {
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s::%s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $reflectionMethod->class, $reflectionMethod->name));
throw new RuntimeException(sprintf('Cannot autowire service "%s": argument $%s of method %s() must have a type-hint or be given a value explicitly.', $this->currentId, $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));
}

if (!array_key_exists($index, $arguments)) {
Expand All @@ -279,7 +289,7 @@ private function autowireMethod(\ReflectionMethod $reflectionMethod, array $argu
if ($value = $this->getAutowiredReference($type)) {
$this->usedTypes[$type] = $this->currentId;
} else {
$failureMessage = $this->createTypeNotFoundMessage($type, 'argument $'.$parameter->name.' of method '.$reflectionMethod->class.'::'.$reflectionMethod->name.'()');
$failureMessage = $this->createTypeNotFoundMessage($type, sprintf('argument $%s of method %s()', $parameter->name, $class !== $this->currentId ? $class.'::'.$method : $method));

if ($parameter->isDefaultValueAvailable()) {
$value = $parameter->getDefaultValue();
Expand Down Expand Up @@ -315,14 +325,17 @@ private function autowireOverridenGetters(array $overridenGetters, array $autowi
if (isset($overridenGetters[$lcMethod]) || $reflectionMethod->getNumberOfParameters() || $reflectionMethod->isConstructor()) {
continue;
}
$class = $reflectionMethod->class;
$method = $reflectionMethod->name;

if (!$type = InheritanceProxyHelper::getTypeHint($reflectionMethod, null, true)) {
$type = InheritanceProxyHelper::getTypeHint($reflectionMethod);

throw new RuntimeException(sprintf('Cannot autowire service "%s": getter %s::%s() must%s have its return value be configured explicitly.', $this->currentId, $reflectionMethod->class, $reflectionMethod->name, $type ? '' : ' have a return-type hint or'));
throw new RuntimeException(sprintf('Cannot autowire service "%s": getter %s() must%s have its return value be configured explicitly.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method, $type ? '' : ' have a return-type hint or'));
}

if (!$typeRef = $this->getAutowiredReference($type)) {
$this->container->log($this, $this->createTypeNotFoundMessage($type, 'return value of method '.$reflectionMethod->class.'::'.$reflectionMethod->name.'()'));
$this->container->log($this, $this->createTypeNotFoundMessage($type, sprintf('return value of method %s()', $class !== $this->currentId ? $class.'::'.$method : $method)));
continue;
}

Expand Down Expand Up @@ -446,15 +459,14 @@ private function set($type, $id)
*/
private function createAutowiredDefinition(\ReflectionClass $typeHint)
{
if (isset($this->ambiguousServiceTypes[$typeHint->name])) {
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class';
$matchingServices = implode(', ', $this->ambiguousServiceTypes[$typeHint->name]);
if (isset($this->ambiguousServiceTypes[$type = $typeHint->name])) {
$classOrInterface = class_exists($type) ? 'class' : 'interface';

throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $this->currentId, $classOrInterface, $matchingServices));
throw new RuntimeException(sprintf('Cannot autowire service "%s": multiple candidate services exist for %s "%s".%s', $this->currentId, $classOrInterface, $type, $this->createTypeAlternatives($type)));
}

if (!$typeHint->isInstantiable()) {
$this->container->log($this, sprintf('Type "%s" is not instantiable thus cannot be auto-registered for service "%s".', $typeHint->name, $this->currentId));
$this->container->log($this, sprintf('Type "%s" is not instantiable thus cannot be auto-registered for service "%s".', $type, $this->currentId));

return;
}
Expand All @@ -463,8 +475,8 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint)
$currentDefinition = $this->currentDefinition;
$definitions = $this->container->getDefinitions();
$currentId = $this->currentId;
$this->currentId = $argumentId = sprintf('autowired.%s', $typeHint->name);
$this->currentDefinition = $argumentDefinition = new Definition($typeHint->name);
$this->currentId = $argumentId = sprintf('autowired.%s', $type);
$this->currentDefinition = $argumentDefinition = new Definition($type);
$argumentDefinition->setPublic(false);
$argumentDefinition->setAutowired(true);

Expand All @@ -475,7 +487,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint)
$this->container->setDefinition($argumentId, $argumentDefinition);
} catch (RuntimeException $e) {
// revert any changes done to our internal state
unset($this->types[$typeHint->name]);
unset($this->types[$type]);
$this->ambiguousServiceTypes = $ambiguousServiceTypes;
$this->container->setDefinitions($definitions);
$this->container->log($this, $e->getMessage());
Expand All @@ -486,19 +498,47 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint)
$this->currentDefinition = $currentDefinition;
}

$this->container->log($this, sprintf('Type "%s" has been auto-registered for service "%s".', $typeHint->name, $this->currentId));
$this->container->log($this, sprintf('Type "%s" has been auto-registered for service "%s".', $type, $this->currentId));

return new Reference($argumentId);
}

private function createTypeNotFoundMessage($type, $label)
{
if (!$classOrInterface = class_exists($type, false) ? 'class' : (interface_exists($type, false) ? 'interface' : null)) {
return sprintf('Cannot autowire %s for service "%s": Class or interface "%s" does not exist.', $label, $this->currentId, $type);
return sprintf('Cannot autowire service "%s": %s has type "%s" but this class does not exist.', $this->currentId, $label, $type);
}
$message = sprintf('no services were found matching the "%s" %s and it cannot be auto-registered for %s.', $type, $classOrInterface, $label);

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

private function createTypeAlternatives($type)
{
$message = ' This type-hint could be aliased to ';

if (isset($this->ambiguousServiceTypes[$type])) {
$message .= sprintf('one of these existing services: "%s"', implode('", "', $this->ambiguousServiceTypes[$type]));
} elseif (isset($this->types[$type])) {
$message .= sprintf('the existing "%s" service', $this->types[$type]);
} else {
return;
}
$aliases = array();

foreach (class_parents($type) + class_implements($type) as $parent) {
if ($this->container->has($parent)) {
$aliases[] = $parent;
}
}

if (1 < count($aliases)) {
$message .= sprintf('; or be updated to one of the following: "%s"', implode('", "', $aliases));
} elseif ($aliases) {
$message .= sprintf('; or be updated to "%s"', $aliases[0]);
}
$message = sprintf('No services were found matching the "%s" %s and it cannot be auto-registered', $type, $classOrInterface);

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

/**
Expand Down
Loading
0