Templated ServiceLocator to be used for tagged services #248
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
With service locator anti-pattern being deprecated long ago, we can now use
ServiceLocator
class for tagged services. If you check compiler passes, it is only used for that anyway.Use case:
Tagged services must have interface. So fictional exporter:
Simple implementation:
Real usage with autowiring (other cases also work in 100% same way):
So in the above case, site visitors would pick name via some dropdown or button, and that value sent to
Exporter
class.Potential problem:
None found for last 6+ months. The only issue I can think of is if someone uses
ServiceLocator
class in undocumented ways.I.e. Symfony still allows public services but they are discouraged, and all documentation is using
ContainerInterface
; something thatServiceLocator
extends.But after testing it for long time, I think it is safe to stub it.
Updated:
This is the feature I mentioned, if someone is not familiar with it: symfony/symfony#39924