Skip to content

Commit

Permalink
[EventDispatcher] Allow to omit the event name when registering liste…
Browse files Browse the repository at this point in the history
…ners
  • Loading branch information
alamirault authored and javiereguiluz committed Jan 10, 2023
1 parent 5790b77 commit 62f5165
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ The most common way to listen to an event is to register an **event listener**::

class ExceptionListener
{
public function onKernelException(ExceptionEvent $event)
public function __invoke(ExceptionEvent $event): void
{
// You get the exception object from the received event
$exception = $event->getThrowable();
Expand Down Expand Up @@ -60,16 +60,8 @@ The most common way to listen to an event is to register an **event listener**::
}
}

.. tip::

Each event receives a slightly different type of ``$event`` object. For
the ``kernel.exception`` event, it is :class:`Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent`.
Check out the :doc:`Symfony events reference </reference/events>` to see
what type of object each event provides.

Now that the class is created, you need to register it as a service and
notify Symfony that it is a "listener" on the ``kernel.exception`` event by
using a special "tag":
notify Symfony that it is a event listener by using a special "tag":

.. configuration-block::

Expand All @@ -78,8 +70,7 @@ using a special "tag":
# config/services.yaml
services:
App\EventListener\ExceptionListener:
tags:
- { name: kernel.event_listener, event: kernel.exception }
tags: [kernel.event_listener]
.. code-block:: xml
Expand All @@ -92,7 +83,7 @@ using a special "tag":
<services>
<service id="App\EventListener\ExceptionListener">
<tag name="kernel.event_listener" event="kernel.exception"/>
<tag name="kernel.event_listener"/>
</service>
</services>
</container>
Expand All @@ -108,7 +99,7 @@ using a special "tag":
$services = $containerConfigurator->services();
$services->set(ExceptionListener::class)
->tag('kernel.event_listener', ['event' => 'kernel.exception'])
->tag('kernel.event_listener')
;
};
Expand All @@ -117,10 +108,7 @@ listener class:

#. If the ``kernel.event_listener`` tag defines the ``method`` attribute, that's
the name of the method to be called;
#. If no ``method`` attribute is defined, try to call the method whose name
is ``on`` + "PascalCased event name" (e.g. ``onKernelException()`` method for
the ``kernel.exception`` event);
#. If that method is not defined either, try to call the ``__invoke()`` magic
#. If no ``method`` attribute is defined, try to call the ``__invoke()`` magic
method (which makes event listeners invokable);
#. If the ``__invoke()`` method is not defined either, throw an exception.

Expand All @@ -134,6 +122,27 @@ listener class:
internal Symfony listeners usually range from ``-256`` to ``256`` but your
own listeners can use any positive or negative integer.

.. note::

There is an optional attribute for the ``kernel.event_listener`` tag called
``event`` which is useful when listener ``$event`` argument is not typed.
If you configure it, it will change type of ``$event`` object.
For the ``kernel.exception`` event, it is :class:`Symfony\\Component\\HttpKernel\\Event\\ExceptionEvent`.
Check out the :doc:`Symfony events reference </reference/events>` to see
what type of object each event provides.

With this attribute, Symfony follows this logic to decide which method to call
inside the event listener class:

#. If the ``kernel.event_listener`` tag defines the ``method`` attribute, that's
the name of the method to be called;
#. If no ``method`` attribute is defined, try to call the method whose name
is ``on`` + "PascalCased event name" (e.g. ``onKernelException()`` method for
the ``kernel.exception`` event);
#. If that method is not defined either, try to call the ``__invoke()`` magic
method (which makes event listeners invokable);
#. If the ``__invoke()`` method is not defined either, throw an exception.

Defining Event Listeners with PHP Attributes
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down

0 comments on commit 62f5165

Please sign in to comment.