Description
Symfony version(s) affected: all from 3.2.0
Description
#20953 changed the way event subscribers are written to the container at compile time. Before this change it wrote calls to addSubscriberService
to the container. Now it resolves the listeners and writes calls to addListener
.
Logic in getSubscribedEvents
that depends on the current state at runtime will no longer be called on every run. It will be executed only once at compile time.
To make it obvious how this can break existing code is an EventSubscriber that listens to userRegistered
events and wants to send an e-mail, when a user registered at night. It achieves this by returning the events it wants to listen to depending on the time, so it only will register for the event if it is past 10pm and before 8am.
After the update, the subscriber will either always send an e-mail or never, depending on the time the container was compiled at.
While this is a very constructed example I think it makes obvious why this is kind of serious, as it will change how the code behaves depending on seemingly arbitrary things.
How to reproduce
Create an EventSubscriber
that has runtime dependent logic inside getSubscribedEvents
. It will only run this logic at compile time and the events will not be handled depending on that logic at runtime anymore.
Possible Solution
As this change is from 2017 I don't think documenting the bc break in a changelog makes much sense at this time. But I think it should be documented in the interface doc block of the getSubscribedEvents
that classes implementing this method must not depend on runtime state in their implementation.