-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Fix some edge cases with autowiring #16464
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
Conversation
@@ -147,6 +148,11 @@ private function populateAvailableType($id, Definition $definition) | |||
$this->types[$type] = $id; | |||
} | |||
|
|||
// Cannot use reflection if the class isn't set |
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 can be moved above the foreach-loop as it doesn't depend on the loop.
And improves performance a little.
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.
It depends of the class of the definition. It cannot be moved outside of the 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.
I mean like this:
private function populateAvailableType($id, Definition $definition)
{
// Never use abstract services
if ($definition->isAbstract()) {
return;
}
// Cannot use reflection if the class isn't set
if (!$definition->getClass()) {
return;
}
foreach ($definition->getAutowiringTypes() as $type) {
$this->definedTypes[$type] = true;
$this->types[$type] = $id;
}
if ($reflectionClass = $this->getReflectionClass($id, $definition)) {
$this->extractInterfaces($id, $reflectionClass);
$this->extractAncestors($id, $reflectionClass);
}
}
Check if a class is set, if not return before executing the 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.
I've done that on purpose. Associating a service with a type using the autowiring_types
key doesn't require reflection, so it doesn't require the class
key to exist.
It allows to do things like associating types with a service definition where the class is associated in a compiler pass executed after the autowiring.
ac456ee
to
52f5936
Compare
Rebased. |
52f5936
to
faefc60
Compare
Thank you @dunglas. |
…ing (dunglas) This PR was merged into the 2.8 branch. Discussion ---------- [DependencyInjection] Fix some edge cases with autowiring | Q | A | ------------- | --- | Bug fix? | no | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | n/a | License | MIT | Doc PR | n/a Enhance the autowiring system: - Works with parent definitions and decorator - Always exclude parent definitions It allows to autowire major services of the standard edition (tested with Swift Mailer, Monolog, Doctrine and Twig). Commits ------- faefc60 [DependencyInjection] Autowing: exclu 7A5C de abstract definitons 71d502a [DependencyInjection] Autowiring: support parent/decorators
Enhance the autowiring system:
It allows to autowire major services of the standard edition (tested with Swift Mailer, Monolog, Doctrine and Twig).