[DependencyInjection] don't move locator tag for service subscriber #48093
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. Therouter
service is declared with an argument for the$container
parameter of the constructor, i.e.:Which leads to the binding, as declared in the
RegisterServiceSubscribersPass
pass not being applied. Later on theDecoratorServicePass
pass moves the tags of the decorated service to the decorating service, where it only keeps thecontainer.service_subscriber
tag on the original service, and moves thecontainer.service_subscriber.locator
tag to the decorating service. When afterwards theResolveServiceSubscribersPass
pass is executed it will replace the arguments to theContainerInterface
with the created locator service (as defined in theRegisterServiceSubscribersPass
). But this then fails because thecontainer.service_subscriber.locator
tag isn't applied anymore.So when the
router
isn't decorated theResolveServiceSubscribersPass
pass 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
$container
argument, which would fail (for the binding not being applied, and the tag being missing because of the decorator).