-
-
Notifications
You must be signed in to change notification settings - Fork 9.8k
[DependencyInjection] Allow registering attributes on classes that cannot be instantiated #53605
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
…nnot be instanciated Fixes symfony#48392
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 think I had more something like below in mind.
Of course without any changes to AbstractRecursivePass.
If you want to process container.excluded services, you'd first need to extract them from the container and remove the tag on them.
--- a/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
+++ b/src/Symfony/Component/DependencyInjection/Loader/FileLoader.php
@@ -162,6 +162,11 @@ abstract class FileLoader extends BaseFileLoader
if (interface_exists($class, false)) {
$this->interfaces[] = $class;
} else {
+ $r = $this->container->getReflectionClass($class);
+ if (!$r->isInstantiable()) {
+ $this->addContainerExcludedTag($class, $source);
+ continue;
+ }
$this->setDefinition($class, $definition = $getPrototype());
if (null !== $errorMessage) {
$definition->addError($errorMessage);
@@ -179,7 +184,6 @@ abstract class FileLoader extends BaseFileLoader
if (!$autoconfigureAttributes) {
continue;
}
- $r = $this->container->getReflectionClass($class);
$defaultAlias = 1 === \count($interfaces) ? $interfaces[0] : null;
foreach ($r->getAttributes(AsAlias::class) as $attr) {|
(Loosely related: this makes me think we could add a |
Hmm, but then I have to reflect on all of those excluded services, to find attributes on the class again. Basically, doing everything from scratch that Symfony already handles. Does that make sense? We can rely on |
not really: you can build a side container with only the service that are of interest to you, remove that tag from them, and run the compiler passes you want on it by compiling the container
this is the critical part - we cannot change this behavior but let me know if you have another idea that would fit this constraint |
|
What if we do the following:
|
|
I like this issue because it's near from what I attempt to do: read attributes at compile time on non-services classes. Yet, near is not enough, because this probably would allow to deal with excluded services, but I'm not sure extending usage of this to, in my use case, messenger messages, would be accepted by core contributors. @nicolas-grekas what is your opinion about this? |
|
I just reopened the topic and submitted #59712 to fill the gap. |
This is a proof of concept for #48392
Needs some tests.