-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[RFC][DI] tagging system like event subscribers (autoconfigure) #25327
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
Comments
I'm -1 on adding TagSubscriberInterface in the core. Making classes exposing their DI tags themselves introduces a coupling to the config component again (as the concept of tags is purely a DI one). On a side note, you can implement TagSubscriberInterface in your own code if you really want it in your project:
|
Indeed, that's why it's a really nice thing to start with. However, I still have to define the priority (and other attributes) somewhere, which beats the purpose of the resource loading and makes my service config huge.
It's optional and not any different from how the
I would like to solve this problem in the core (if possible) with a generic solution to prevent a wild spread of similar implementations and therefore bugs or code duplication. Is there currently a neat way of collecting this static information from a service definition? The
Yes, I can certainly add it myself, that's not really the problem, but I'm pretty sure I'm not the only one running against this particular issue. I can always create a bundle to provide this feature for more so that's not really a problem either. So based on your feedback, what about something that allows people to write attribute targeting interfaces? class FooBundle
{
public function load(ContainerBuilder $builder)
{
// just and example
$container->mapInterfaceToTagAttribute(PriorityInterface::class, 'priority', [
'app.option', 'app.selection'
]);
$container->mapInterfaceToTagAttribute(FeatureConfigKeyInterface::class, 'config-key', [
'features.resolver'
]);
$container->mapInterfaceToTagAttribute(FeatureTagKeyInterface::class, 'tag', [
'features.tag'
]);
}
}
class Foo implements PriorityInterface, FeatureConfigKeyInterface, FeatureTagKeyInterface
{
public static function getPriority(): int { return 10; }
public static function getConfigKey(): string { return 'environment'; }
public static function getFeatureTag(): string { return 'some_feature_name'; }
} |
For the record, TagSubscriberInterface was rejected in #22649 |
There is a big difference with EventSubscriberInterface. The API exposed by EventSubscriberInterface is related to the feature of the event dispatcher component (btw, it is even usable without using DI at all).
if you need custom attributes on your tags, you already need a custom compiler pass to process them ( |
Alright, so the best course is to create something myself and make this publicly available in case others will need it as well. |
And then, if it’s widely used, it would have a better chance in core. Your use-case makes sense to me, but I don’t how common it is :). |
👎 also: this inlines some important logic/knowledge in a DI extension, instead of a compiler pass. This means the logic is not reusable anymore. That's a serious regression as far as design is concerned. |
how about reducing the verbosity by adding "service template parameters": services:
#...
_instanceof:
App\OptionInterface:
tags:
- { name: app.option, priority: "%var(app.option.priority:10)%" }
#App\Option\One: # default value of 10 applies
App\Option\Two:
vars:
app.option.priority: 15
App\Option\Three:
vars:
app.option.priority: 100
App\Option\Four:
vars:
app.option.priority: -10
#... |
@backbone87 then I'd still have to define each and every dependency, while I would want to utilize autoconfigure for this |
@iltar sadly I must close this proposal as "won't fix" because of the downvotes from the Core Team, but I hope you keep sending proposals to improve Symfony. Thanks! |
Still being on 3.3, my first preparation in my application is to change everything to autowiring. Now that we have autoconfigure and resource loading, it's a lot easier to get things started with ~1900 services in my app.
However, there are some obstacles I'm hitting that still require me to define a lot of services in my config. While we can currently already use
!tagged
to avoid using compiler passes in 3.4, I'm still required to configure a priority when this is needed. In some cases, I have ~20 service definitions that I need to sort and inject into another.Now my autowire.yml looks something like this:
Ideally I'd only define the service definition that collects these options, so I've been wondering if a tag subscriber would be the solution. Similar to event listeners vs event subscribers, this would allow a class to subscribe to specific tags, which can be picked up by autoconfigure:
WDYT?
@Simperfit 😄

The text was updated successfully, but these errors were encountered: