-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[DependencyInjection] Service subscriber without autowire #60272
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
I'd like to understand more what issue you face. I'm using ServiceSubscriberInterface on a non-autowired service since years: https://github.com/FriendsOfSymfony/FOSUserBundle/blob/901069c01e20ef4d6c4e0d638428656ae5bedcdf/src/Resources/config/change_password.xml#L24-L27 You need to configure your |
Oh. Didn't know this and din't figured out either. Let me try it out and maybe open a PR on documentation. If it works on my side I will close this PR. Thank you @stof |
@stof : indeed it works. thank you for your insight. Should I close this Pr then ? |
Yes and maybe provide something to the documentation to make this more discoverable? |
It would indeed be great to document the way to configure service subscribers when not using autowiring. |
opened an issue to track this : symfony/symfony-docs#20959 and will get on it ASAP. |
TL;DR
We needed to use the ServiceSubscriber feature in a reusable Bundle.
The best practices for Bundles is to not use the Autowire feature.
But the way service subscriber are registered / resolved only works with autowired services at the moment.
The goal of this PR is to provide a way to easily extract the Locator for a service subscriber class / definition and use it in a custom compiler pass for example.
More details
RegisterServiceSubscribersPass
if the service definition has thecontainer.service_subscriber
getSubscribedServices
static method to cerate a$serviceMap
container.service_subscriber.locator
with the id of the service locatorPsrContainerInterface
ResolveServiceSubscribersPass
which clears tags + inject the locatorThere is mo way to insert a compiler pass in between those two, they have the same priority in the same PassConfig type.
Apart from re-doing all the logic ourselves, there are no easy way to create a service locator solely based on the interface.
Why do I need to use a ServiceSubscriber in my Bundle ?
Our bundle provides extension points, allowing developers to create their own services that integrate seamlessly with the bundle's internal logic. This improves the developer experience (DX) by simplifying service wiring.
Since we can't predict in advance which services these extensions will depend on, using a ServiceSubscriber is ideal. It allows us to support dynamic dependencies without requiring developers to write a compiler pass.
For example, if a developer adds a trait like TwigAwareTrait to a class, the twig service becomes a dependency. We want to give them the flexibility to inject such additional services—beyond those provided by default in the service locator—in a clean and declarative way.