Events, Subscribers & Listeners
Under the hood the Symfony Event Dispatcher is used, however there is a custom implementation on the Event Dispatcher. This is in order to provide future stability and to enable the system for adding functionality in to the event system.
#
Subscribing or listening to eventsYou can choose to subscribe to (multiple) events by using an EventSubscriber. An EventSubscriber has to return an array of the events it desires to listen (subscribe) to. An EventListener on the other hand can mark a public method as a Listener for a specific event by annotating it with the ListenTo attribute (example below).
#
Subscribing to eventsWhen you want to do something when a given event occurs (like logging, or for example add a Route variable_type) you can subscribe to those events using a EventSubscriber instance. In contrary to Symfony, in this system Event Subscriber do support Dependency Injection. It is recommended to only use subscribers to 'catch' the event and use a service to execute the actual logic (and if applicable apply the result to the event). Quite the same as you would do in a Controller or a command. This makes the logic in the service reusable for different occasions and keeps the subscriber clean.
#
Listening to an eventListen to an event by marking the class an EventListener by implement the Interface and marking a method as a listener. Provide the event as argument in the attribute. The event will be passed to the method as an argument.
#
How to create your own eventsEvents are classes which can be dispatched using the EventDispatcher. You can easily create your own like the example.
#
Dispatching eventsEvents are dispatched using the EventDispatcher. You will need to inject the EventDispatcher (Swift\Events\EventDispatcher
) into your class.
Below an example from the Authentication Manager dispatching an event for the created token.
#
Default system eventsAll events and their description can be listed by running the command php bin/console events:list:all
. This will result in something as such.