Listen to all events in Symfony 2

Is it possible to set up an event listener (or do something else?) To listen to all the events triggered by the Symfony 2 AppKernel for a specific request?

That is, I know that I can view the application using app_dev.php and use the profiler to view a list of all listeners, but I am interested in capturing a list of all events that have been sent / fired. I know that in some event systems there is a special global / all listener that will allow me to receive every event. I am wondering if Symfony has something similar or if there is another mechanism to get a list of all available events on a particular page.

I also know that I can add a temporary debug code to one of the event manager classes

 Symfony/Component/EventDispatcher/EventDispatcher.php Symfony/Component/HttpKernel/Debug/ContainerAwareTraceableEventDispatcher.php Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php 

but I'm looking for something that is less harmful / less destructive.

New to Symfony, but not new to programming. Sorry if this is a naive question, but googling about has not shown what I need.

+6
source share
1 answer

A clean way would be to create your own EventDispatcher that will do your registration or whatever you are trying to do if an event occurs. Take a look at default one to see how this works.

Now create a class first

 use Symfony\Component\EventDispatcher\EventDispatcher; class MyDispatcher extends EventDispatcher { // sadly those properties aren't protected in EventDispatcher private $listeners = array(); private $sorted = array(); public function dispatch($eventName, Event $event = null) { if (null === $event) { $event = new Event(); } $event->setDispatcher($this); $event->setName($eventName); // do something with the event here ... ie log it if (!isset($this->listeners[$eventName])) { return $event; } $this->doDispatch($this->getListeners($eventName), $eventName, $event); return $event; } 

... then register your MyDispatcher as symfony by default.

(by overwriting the original event_dispatcher service)

application /Config/config.yml

 services: event_dispatcher: class: Vendor\YourBundle\MyDispatcher arguments: [@service_container] 

... or even easier to just override the parameter that symfony uses when creating the service.

 parameters: event_dispatcher.class: Vendor\YourBundle\MyDispatcher 
+6
source

Source: https://habr.com/ru/post/948082/


All Articles