-1

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?

Rossko_DCA
  • 51
  • 1
  • 9
  • 1
    Why do you need that exactly? As your subscriber is a usual service: why not add a flag to it, inject the service into your command, and set that flag from that command? – Nico Haase Aug 12 '20 at 06:52
  • @NicoHaase it is cause I need trigger this subscriber every time any entity is updated / persisted / deleted but just in this specific operation (command call updateService which is calling multiple other classes which are manipulating updating object) I do not want to trigger this subscriber – Rossko_DCA Aug 12 '20 at 07:37
  • 1
    Then what have you tried to resolve this? Does my first comment help you? – Nico Haase Aug 12 '20 at 07:48
  • 1
    I understand, I tried it and it works perfectly, thank you for your help, I did not realize, that subscriber is still just a service – Rossko_DCA Aug 12 '20 at 08:04

1 Answers1

1

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

Nico Haase
  • 11,420
  • 35
  • 43
  • 69