-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DI] Add "factory" support to named args and autowiring #22277
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 |
---|---|---|
|
@@ -95,9 +95,6 @@ protected function processValue($value, $isRoot = false) | |
if (!$value->isAutowired() || $value->isAbstract() || !$value->getClass()) { | ||
return parent::processValue($value, $isRoot); | ||
} | ||
if ($value->getFactory()) { | ||
throw new RuntimeException(sprintf('Service "%s" can use either autowiring or a factory, not both.', $this->currentId)); | ||
} | ||
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())); | ||
|
||
|
@@ -107,8 +104,8 @@ protected function processValue($value, $isRoot = false) | |
$autowiredMethods = $this->getMethodsToAutowire($reflectionClass); | ||
$methodCalls = $value->getMethodCalls(); | ||
|
||
if ($constructor = $reflectionClass->getConstructor()) { | ||
array_unshift($methodCalls, array($constructor->name, $value->getArguments())); | ||
if ($constructor = $this->getConstructor($value, false)) { | ||
array_unshift($methodCalls, array($constructor, $value->getArguments())); | ||
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. Can't we keep 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. nope we can't, because when a factory is used, a simple string is not enough to reference the target 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. Ok, good point 👍 |
||
} | ||
|
||
$methodCalls = $this->autowireCalls($reflectionClass, $methodCalls, $autowiredMethods); | ||
|
@@ -143,13 +140,13 @@ private function getMethodsToAutowire(\ReflectionClass $reflectionClass) | |
$found = array(); | ||
$methodsToAutowire = array(); | ||
|
||
if ($reflectionMethod = $reflectionClass->getConstructor()) { | ||
$methodsToAutowire[strtolower($reflectionMethod->name)] = $reflectionMethod; | ||
} | ||
|
||
foreach ($reflectionClass->getMethods() as $reflectionMethod) { | ||
$r = $reflectionMethod; | ||
|
||
if ($r->isConstructor()) { | ||
continue; | ||
} | ||
|
||
while (true) { | ||
if (false !== $doc = $r->getDocComment()) { | ||
if (false !== stripos($doc, '@required') && preg_match('#(?:^/\*\*|\n\s*+\*)\s*+@required(?:\s|\*/$)#i', $doc)) { | ||
|
@@ -183,19 +180,13 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC | |
foreach ($methodCalls as $i => $call) { | ||
list($method, $arguments) = $call; | ||
|
||
if (isset($autowiredMethods[$lcMethod = strtolower($method)]) && $autowiredMethods[$lcMethod]->isPublic()) { | ||
if ($method instanceof \ReflectionFunctionAbstract) { | ||
$reflectionMethod = $method; | ||
} elseif (isset($autowiredMethods[$lcMethod = strtolower($method)]) && $autowiredMethods[$lcMethod]->isPublic()) { | ||
$reflectionMethod = $autowiredMethods[$lcMethod]; | ||
unset($autowiredMethods[$lcMethod]); | ||
} else { | ||
if (!$reflectionClass->hasMethod($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()) { | ||
$class = $reflectionClass->name; | ||
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s() must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method)); | ||
} | ||
$reflectionMethod = $this->getReflectionMethod($this->currentDefinition, $method); | ||
} | ||
|
||
$arguments = $this->autowireMethod($reflectionMethod, $arguments); | ||
|
@@ -210,7 +201,7 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC | |
|
||
if (!$reflectionMethod->isPublic()) { | ||
$class = $reflectionClass->name; | ||
throw new RuntimeException(sprintf('Cannot autowire service "%s": method %s() must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method)); | ||
throw new RuntimeException(sprintf('Cannot autowire service "%s": method "%s()" must be public.', $this->currentId, $class !== $this->currentId ? $class.'::'.$method : $method)); | ||
} | ||
$methodCalls[] = array($method, $this->autowireMethod($reflectionMethod, array())); | ||
} | ||
|
@@ -221,16 +212,16 @@ private function autowireCalls(\ReflectionClass $reflectionClass, array $methodC | |
/** | ||
* Autowires the constructor or a method. | ||
* | ||
* @param \ReflectionMethod $reflectionMethod | ||
* @param array $arguments | ||
* @param \ReflectionFunctionAbstract $reflectionMethod | ||
* @param array $arguments | ||
* | ||
* @return array The autowired arguments | ||
* | ||
* @throws RuntimeException | ||
*/ | ||
private function autowireMethod(\ReflectionMethod $reflectionMethod, array $arguments) | ||
private function autowireMethod(\ReflectionFunctionAbstract $reflectionMethod, array $arguments) | ||
{ | ||
$class = $reflectionMethod->class; | ||
$class = $reflectionMethod instanceof \ReflectionMethod ? $reflectionMethod->class : $this->currentId; | ||
$method = $reflectionMethod->name; | ||
$parameters = $reflectionMethod->getParameters(); | ||
if (method_exists('ReflectionMethod', 'isVariadic') && $reflectionMethod->isVariadic()) { | ||
|
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.
Does it really belong to this method? Shouldn't it be in
CheckDefinitionValidityPass
instead?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.
this is required to prevent a potential infinite loop
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.
Ok, good point too :) It should still be added in
CheckDefinitionValidityPass
imo though.