Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Listen for All Events in Symfony 2

Is it possible to setup an event listener (or do — something else?) to listen for all the events fired by a Symfony 2 AppKernel application for a particular request?

That is, I know I can browse an application with app_dev.php and use the profiler to view a list of all the listeners, but I'm interested in grabbing a list of every event that's been dispatched/fired. I know some event systems have a special global/all listener what would let me receive every event. I'm wondering if Symfony has something similar, or if there's another mechanism to get a list of all the available events on a particular page.

I also know I could add some temporary debugging code to one of the event dispatcher 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 of a hack/less-destructive.

New to Symfony, but not new to programming. Apologies if this is a naive question, but googling about hasn't revealed what I'm after.

like image 539
Alan Storm Avatar asked Jun 25 '13 16:06

Alan Storm


1 Answers

The clean way would be creating your own EventDispatcher which executes your logging or whatever you're trying to do if an event occurs. Have a look at the default one to get an idea of how it works.

Now first create the class

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 ... i.e. log it

        if (!isset($this->listeners[$eventName])) {
            return $event;
        }

        $this->doDispatch($this->getListeners($eventName), $eventName, $event);

        return $event;
    }

... then register your MyDispatcher as symfony's default one.

( by overwriting the original event_dispatcher service )

app/config/config.yml

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

... or even simpler just override the class parameter being used by symfony when creating the service.

parameters:
    event_dispatcher.class: Vendor\YourBundle\MyDispatcher
like image 66
Nicolai Fröhlich Avatar answered Sep 21 '22 20:09

Nicolai Fröhlich