-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Autowiring: add setter injection support #17608
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -110,12 +110,45 @@ private function completeDefinition($id, Definition $definition) | |
$this->container->addResource(static::createResourceForClass($reflectionClass)); | ||
} | ||
|
||
if (!$constructor = $reflectionClass->getConstructor()) { | ||
return; | ||
if ($constructor = $reflectionClass->getConstructor()) { | ||
$this->autowireMethod($id, $definition, $constructor, true); | ||
} | ||
|
||
$methodsCalled = array(); | ||
foreach ($definition->getMethodCalls() as $methodCall) { | ||
$methodsCalled[$methodCall[0]] = true; | ||
} | ||
|
||
foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflectionMethod) { | ||
$name = $reflectionMethod->getName(); | ||
if (isset($methodsCalled[$name]) || $reflectionMethod->isStatic() || 1 !== $reflectionMethod->getNumberOfParameters() || 0 !== strpos($name, 'set')) { | ||
continue; | ||
} | ||
|
||
$this->autowireMethod($id, $definition, $reflectionMethod, false); | ||
} | ||
} | ||
|
||
$arguments = $definition->getArguments(); | ||
foreach ($constructor->getParameters() as $index => $parameter) { | ||
/** | ||
* Autowires the constructor or a setter. | ||
* | ||
* @param string $id | ||
* @param Definition $definition | ||
* @param \ReflectionMethod $reflectionMethod | ||
* @param bool $isConstructor | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
private function autowireMethod($id, Definition $definition, \ReflectionMethod $reflectionMethod, $isConstructor) | ||
{ | ||
if ($isConstructor) { | ||
$arguments = $definition->getArguments(); | ||
} else { | ||
$arguments = array(); | ||
} | ||
|
||
$addMethodCall = false; | ||
foreach ($reflectionMethod->getParameters() as $index => $parameter) { | ||
if (array_key_exists($index, $arguments) && '' !== $arguments[$index]) { | ||
continue; | ||
} | ||
|
@@ -124,7 +157,11 @@ private function completeDefinition($id, Definition $definition) | |
if (!$typeHint = $parameter->getClass()) { | ||
// no default value? Then fail | ||
if (!$parameter->isOptional()) { | ||
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id)); | ||
if ($isConstructor) { | ||
throw new RuntimeException(sprintf('Unable to autowire argument index %d ($%s) for the service "%s". If this is an object, give it a type-hint. Otherwise, specify this argument\'s value explicitly.', $index, $parameter->name, $id)); | ||
} | ||
|
||
return; | ||
} | ||
|
||
// specifically pass the default value | ||
|
@@ -139,24 +176,35 @@ private function completeDefinition($id, Definition $definition) | |
|
||
if (isset($this->types[$typeHint->name])) { | ||
$value = new Reference($this->types[$typeHint->name]); | ||
$addMethodCall = true; | ||
} else { | ||
try { | ||
$value = $this->createAutowiredDefinition($typeHint, $id); | ||
$addMethodCall = true; | ||
} catch (RuntimeException $e) { | ||
if ($parameter->allowsNull()) { | ||
$value = null; | ||
} elseif ($parameter->isDefaultValueAvailable()) { | ||
$value = $parameter->getDefaultValue(); | ||
} else { | ||
throw $e; | ||
// The exception code is set to 1 if the exception must be thrown even if it's a setter | ||
if (1 === $e->getCode() || $isConstructor) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we continue using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was thinking about introducing a |
||
throw $e; | ||
} | ||
|
||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here - should be continue |
||
} | ||
} | ||
} | ||
} catch (\ReflectionException $e) { | ||
// Typehint against a non-existing class | ||
|
||
if (!$parameter->isDefaultValueAvailable()) { | ||
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e); | ||
if ($isConstructor) { | ||
throw new RuntimeException(sprintf('Cannot autowire argument %s for %s because the type-hinted class does not exist (%s).', $index + 1, $definition->getClass(), $e->getMessage()), 0, $e); | ||
} | ||
|
||
return; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should be a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return is the good keyword here IMO. The idea is to stop because this method is not autowirable:
|
||
} | ||
|
||
$value = $parameter->getDefaultValue(); | ||
|
@@ -168,7 +216,12 @@ private function completeDefinition($id, Definition $definition) | |
// it's possible index 1 was set, then index 0, then 2, etc | ||
// make sure that we re-order so they're injected as expected | ||
ksort($arguments); | ||
$definition->setArguments($arguments); | ||
|
||
if ($isConstructor) { | ||
$definition->setArguments($arguments); | ||
} elseif ($addMethodCall) { | ||
$definition->addMethodCall($reflectionMethod->name, $arguments); | ||
} | ||
} | ||
|
||
/** | ||
|
@@ -266,7 +319,7 @@ private function createAutowiredDefinition(\ReflectionClass $typeHint, $id) | |
$classOrInterface = $typeHint->isInterface() ? 'interface' : 'class'; | ||
$matchingServices = implode(', ', $this->ambiguousServiceTypes[$typeHint->name]); | ||
|
||
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices)); | ||
throw new RuntimeException(sprintf('Unable to autowire argument of type "%s" for the service "%s". Multiple services exist for this %s (%s).', $typeHint->name, $id, $classOrInterface, $matchingServices), 1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any other precedent for this in the code? I can see why you used There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fine for me |
||
} | ||
|
||
if (!$typeHint->isInstantiable()) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here -
continue