Taking the example from https://symfony.com/doc/current/event_dispatcher.html
class ExceptionSubscriber implements EventSubscriberInterface
{
public static function getSubscribedEvents()
{
// return the subscribed events, their methods and priorities
return [
KernelEvents::EXCEPTION => [
['processException', 10],
['logException', 0],
['notifyException', -10],
],
];
}
}
Is it correct to assume that this list can be changed during runtime?
E.g.
class ExceptionSubscriber implements EventSubscriberInterface
{
protected $someToggle = false;
public static function getSubscribedEvents()
{
if ($this->someToggle) {
return [KernelEvents::EXCEPTION => ['processException']]
}
return [
KernelEvents::EXCEPTION => [
['processException', 10],
['logException', 0],
['notifyException', -10],
],
]
}
}
Is this legit and unsubscribes logException
and notifyException
when I set $someToggle
during runtime?
No, you cannot change dynamically what events a subscriber listen to by adding logic to the getSubscribedEvents():array
method.
That method is run only during a compiler pass when the container is being built, so it will only be executed after cache is cleared.
Trying to change this at runtime will have no effect.
The practical way of doing this is to put this logic into the "work" part of the listener/subscriber:
public function processException(ExceptionEvent $event)
{
if (!$this->shouldProcessException()) {
return;
}
}
The performance hit would be very small or negligible, unless getting the value for shouldProcessException()
was otherwise expensive.