From 62f516596b9e5efc595bbdb0672400231a3810c7 Mon Sep 17 00:00:00 2001 From: Antoine Lamirault Date: Sat, 7 Jan 2023 18:02:00 +0100 Subject: [PATCH] [EventDispatcher] Allow to omit the event name when registering listeners --- event_dispatcher.rst | 45 ++++++++++++++++++++++++++------------------ 1 file changed, 27 insertions(+), 18 deletions(-) diff --git a/event_dispatcher.rst b/event_dispatcher.rst index f10a93bc90f..17e4e02019c 100644 --- a/event_dispatcher.rst +++ b/event_dispatcher.rst @@ -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(); @@ -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 ` 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:: @@ -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 @@ -92,7 +83,7 @@ using a special "tag": - + @@ -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') ; }; @@ -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. @@ -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 ` 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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~