Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork9.7k
[DependencyInjection] don't move locator tag for service subscriber#48093
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
nicolas-grekas merged 1 commit intosymfony:4.4fromRobertMe:replace-container-of-decorated-service-subscriber-with-argumentNov 4, 2022
Merged
[DependencyInjection] don't move locator tag for service subscriber#48093
nicolas-grekas merged 1 commit intosymfony:4.4fromRobertMe:replace-container-of-decorated-service-subscriber-with-argumentNov 4, 2022
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Decorators move tags applied to the decorated service to the decoratingservice. But this (sometimes) breaks when the decorated service is aservice subscriber, which has the argument for the container explicitlyset.This mostly works because the locator for the service subscriber isapplied twice. The RegisterServiceSubscriberPass which creates thelocator also sets a binding on the service. TheResolveServiceSubscriberPass replaces the arguments referencing theContainerInterface or ServiceProviderInterface for those services taggedwith the container.service_subscriber.locator tag. So when the argumentisn't provided in the service definition it will automatically be setusing the binding. And in case the argument is set, it will be replacedby the Resolver pass based on the tag.But this thus breaks in case a service explicitly sets theargument (which means the binding isn't applied) and the service beingdecorated (meaning the locator tag is "lost"). So add the locator tagto the list of tags to keep on the original service.
c4b0e31 to3c7dbb4CompareMember
nicolas-grekas commentedNov 4, 2022
Thank you@RobertMe. |
Merged
This was referencedNov 28, 2022
Merged
Merged
Merged
Merged
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
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.
From the commit message:
Decorators move tags applied to the decorated service to the decorating service. But this (sometimes) breaks when the decorated service is a service subscriber, which has the argument for the container explicitly set.
This mostly works because the locator for the service subscriber is applied twice. The RegisterServiceSubscriberPass which creates the locator also sets a binding on the service. The
ResolveServiceSubscriberPass replaces the arguments referencing the ContainerInterface or ServiceProviderInterface for those services tagged with the container.service_subscriber.locator tag. So when the argument isn't provided in the service definition it will automatically be set using the binding. And in case the argument is set, it will be replaced by the Resolver pass based on the tag.
But this thus breaks in case a service explicitly sets the argument (which means the binding isn't applied) and the service being decorated (meaning the locator tag is "lost"). So add the locator tag to the list of tags to keep on the original service.
Explanation:
I found this issue when decorating the
Router. TheRouter(inFrameworkBundle) uses a service subscriber, but this lead to a deprecation message for autowiringPsr\...\ContainerInterface. Debugging also showed that the full container was injected, and not the extracted service locator (service locator service actually was logged as removed for being unused). After investigation the issue was found to be as described above. Therouterservice is declared with an argument for the$containerparameter of the constructor, i.e.:Which leads to the binding, as declared in the
RegisterServiceSubscribersPasspass not being applied. Later on theDecoratorServicePasspass moves the tags of the decorated service to the decorating service, where it only keeps thecontainer.service_subscribertag on the original service, and moves thecontainer.service_subscriber.locatortag to the decorating service. When afterwards theResolveServiceSubscribersPasspass is executed it will replace the arguments to theContainerInterfacewith the created locator service (as defined in theRegisterServiceSubscribersPass). But this then fails because thecontainer.service_subscriber.locatortag isn't applied anymore.So when the
routerisn't decorated theResolveServiceSubscribersPasspass is the one which makes the service subscriber work, replacing the original argument as defined in the service definition. But when it is decorated this breaks because the tag is missing.The unit tests didn't detect this because: 1. the container isn't injected (and thus not validated); 2. even with validation of the container it would work as the binding would be applied. This is why I also kept the original unit test (but expanding the test with validating the container), which would still pass (based on the binding), and adding the additional test which explicitly sets the
$containerargument, which would fail (for the binding not being applied, and the tag being missing because of the decorator).