简体   繁体   中英

Change subscribed events during runtime (Symfony/Doctrine ED)

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?

Eg

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM