Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see list of available events in Symfony2

Tags:

symfony

How can I see all available events in Symfony2?

I found a command on google

php app\console container:debug --show-private

But it does not show all available events. Like event named "security.interactive_login" is not listed in it. Is there a way to see available events?

like image 219
user1906383 Avatar asked Nov 02 '13 15:11

user1906383


2 Answers

Console command

You can run:

app/console debug:event-dispatcher

This will show you a detailed summary of every subscriber, in order of priority per event. Unfortunately this won't show you all possible events, since it's infeasible to query the container for any events that could be registered due to the inherently dynamic nature of the events system.

To understand events you'll need to refer to docs and code of each component and bundle.

Documentation is the best place to start

Symfony standard ships with a multitude of events. Each Symfony component and bundle may or may not define events — your best bet is to look at each component or bundle's documentation for references to events.

Some very common events can be found in the docs:

  • HTTP Kernel Events
  • Console Events
  • Form Events

Code analysis

I used PhpStorm to look for all subclasses of Symfony's base Event class (Symfony\Component\EventDispatcher\Event).

I generated an inheritance tree each child is a subclass of it's parent.

* note: prepend Symfony\Component\ to find the FQN

  • EventDispatcher\Event
    • EventDispatcher\GenericEvent
    • Console\Event\ConsoleEvent
      • Console\Event\ConsoleCommandEvent
      • Console\Event\ConsoleExceptionEvent
      • Console\Event\ConsoleTerminateEvent
    • Form\FormEvent
    • HttpKernel\Event\KernelEvent
      • HttpKernel\Event\FilterResponseEvent
      • HttpKernel\Event\FilterControllerEvent
      • HttpKernel\Event\FinishRequestEvent
      • HttpKernel\Event\GetResponseEvent
        • HttpKernel\Event\GetResponseForControllerResultEvent
        • HttpKernel\Event\GetResponseForExceptionEvent
      • HttpKernel\Event\PostResponseEvent
    • Security\Http\Event\SwitchUserEvent
    • Security\Http\Event\InteractiveLoginEvent

I make no claim that these are all public events you can/should hook into — this is just one way to programmatic examine 3rd party code and get a sense for potential idioms.

For instance I noticed that both the HttpKernel, Security, and Console components use namespaced constants to expose their keys, see:

  • Symfony\Component\HttpKernel\KernelEvents
  • Symfony\Component\Security\Http\SecurityEvents
  • Symfony\Component\Console\ConsoleEvents
like image 118
Mark Fox Avatar answered Oct 06 '22 07:10

Mark Fox


The container:debug command shows all services that are registered to the dependency injection container. With the parameter show-private it will also show services that are flagged with public=false.

So as the most events might not be services the command you are using will not give you a list of available events. But to give you a possibility to search for available events you could try the following command:

php app/console container:debug --show-private | grep -i "listener"

As the most event handlers might have the word listener in their definitions you will find many of them. If you then want to get a more detailed information about the events which are handled by those listeners just call the command with specifying the service ID. For example if you are working with the FOSUserBundle this will give you a description for the interactive login listener:

php app/console container:debug fos_user.security.interactive_login_listener
like image 44
sebbo Avatar answered Oct 06 '22 06:10

sebbo