Search code examples
symfonydoctrine-ormsubscriber

Get the context of call in symfony event subscriber


I have event subscriber, which is listening on doctrines events:

return [
    Events::postPersist,
    Events::postUpdate,
    Events::postRemove,
];

It is working ok, but what I need is in specific application operation (called from command) disable this event to trigger subscriber. My idea is somehow detect from which scope was called flush(), is there a way like debug_backrace() (which I don't want use obv.) to detect context of application?


Solution

  • One thing you can try: add a flag to your subscriber such that you can enable or disable your subscriber, like:

    private $isEnabled = true;
    
    public function disableSubscriber(): void {
        $this->isEnabled = false;
    }
    

    In each of your methods handling stuff, you check for that flag - is it false, then don't do whatever you would do otherwise.

    Afterwards, you can inject the subscriber into other services, just like any other service. In the command where you want to disable handling through that subscriber, call $subscriber->disableSubscriber(); and you're done