Skip to content

Commit b4bb39d

Browse files
committed
feature #12420 [EventDispatcher] Document event name aliases (derrabus)
This PR was merged into the 4.4 branch. Discussion ---------- [EventDispatcher] Document event name aliases This PR fixes #12418 by documenting symfony/symfony#33793 Question: On the component's doc page, the `sidebar` block "Registering Event Listeners and Subscribers in the Service Container" has grown a bit larger with my changes. Would it make sense to move that block to a dedicated page now? Commits ------- c87632c [EventDispatcher] Document event name aliases.
2 parents 8b95859 + c87632c commit b4bb39d

File tree

2 files changed

+114
-3
lines changed

2 files changed

+114
-3
lines changed

components/event_dispatcher.rst

Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,11 +220,55 @@ determine which instance is passed.
220220
$containerBuilder->register('subscriber_service_id', \AcmeSubscriber::class)
221221
->addTag('kernel.event_subscriber');
222222

223+
``RegisterListenersPass`` is able to resolve aliased class names which for
224+
instance allows to refer to an event via the fully qualified class name
225+
(FQCN) of the event class. The pass will read the alias mapping from a
226+
dedicated container parameter. This parameter can be extended by registering
227+
another compiler pass, ``AddEventAliasesPass``::
228+
229+
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
230+
use Symfony\Component\DependencyInjection\ContainerBuilder;
231+
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
232+
use Symfony\Component\DependencyInjection\Reference;
233+
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
234+
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
235+
use Symfony\Component\EventDispatcher\EventDispatcher;
236+
237+
$containerBuilder = new ContainerBuilder(new ParameterBag());
238+
$containerBuilder->addCompilerPass(new RegisterListenersPass(), PassConfig::TYPE_BEFORE_REMOVING);
239+
$containerBuilder->addCompilerPass(new AddEventAliasesPass([
240+
\AcmeFooActionEvent::class => 'acme.foo.action',
241+
]));
242+
243+
$containerBuilder->register('event_dispatcher', EventDispatcher::class);
244+
245+
// registers an event listener
246+
$containerBuilder->register('listener_service_id', \AcmeListener::class)
247+
->addTag('kernel.event_listener', [
248+
// will be translated to 'acme.foo.action' by RegisterListenersPass.
249+
'event' => \AcmeFooActionEvent::class,
250+
'method' => 'onFooAction',
251+
]);
252+
253+
.. note::
254+
255+
Note that ``AddEventAliasesPass`` has to be processed before ``RegisterListenersPass``.
256+
223257
By default, the listeners pass assumes that the event dispatcher's service
224258
id is ``event_dispatcher``, that event listeners are tagged with the
225-
``kernel.event_listener`` tag and that event subscribers are tagged
226-
with the ``kernel.event_subscriber`` tag. You can change these default
227-
values by passing custom values to the constructor of ``RegisterListenersPass``.
259+
``kernel.event_listener`` tag, that event subscribers are tagged
260+
with the ``kernel.event_subscriber`` tag and that the alias mapping is
261+
stored as parameter ``event_dispatcher.event_aliases``. You can change these
262+
default values by passing custom values to the constructors of
263+
``RegisterListenersPass`` and ``AddEventAliasesPass``.
264+
265+
.. versionadded:: 4.3
266+
267+
Aliasing event names is possible since Symfony 4.3.
268+
269+
.. versionadded:: 4.4
270+
271+
The ``AddEventAliasesPass`` class was introduced in Symfony 4.4.
228272

229273
.. _event_dispatcher-closures-as-listeners:
230274

event_dispatcher.rst

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,73 @@ there are some minor advantages for each of them:
248248
* **Listeners are more flexible** because bundles can enable or disable each of
249249
them conditionally depending on some configuration value.
250250

251+
.. _event-aliases:
252+
253+
Event Aliases
254+
-------------
255+
256+
When configuring event listeners and subscribers via dependency injection,
257+
Symfony's core events can also be referred to by the fully qualified class
258+
name (FQCN) of the corresponding event class::
259+
260+
// src/EventSubscriber/RequestSubscriber.php
261+
namespace App\EventSubscriber;
262+
263+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
264+
use Symfony\Component\HttpKernel\Event\RequestEvent;
265+
266+
class RequestSubscriber implements EventSubscriberInterface
267+
{
268+
public static function getSubscribedEvents(): array
269+
{
270+
return [
271+
RequestEvent::class => 'onKernelRequest',
272+
];
273+
}
274+
275+
public function onKernelRequest(RequestEvent $event)
276+
{
277+
// ...
278+
}
279+
}
280+
281+
.. versionadded:: 4.3
282+
283+
Referring Symfony's core events via the FQCN of the event class is possible
284+
since Symfony 4.3.
285+
286+
Internally, the event FQCN are treated as aliases for the original event names.
287+
Since the mapping already happens when compiling the service container, event
288+
listeners and subscribers using FQCN instead of event names will appear under
289+
the original event name when inspecting the event dispatcher.
290+
291+
This alias mapping can be extended for custom events by registering the
292+
compiler pass ``AddEventAliasesPass``::
293+
294+
// src/Kernel.php
295+
use App\Event\MyCustomEvent;
296+
use Symfony\Component\DependencyInjection\ContainerBuilder;
297+
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
298+
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
299+
300+
class Kernel extends BaseKernel
301+
{
302+
protected function build(ContainerBuilder $container)
303+
{
304+
$container->addCompilerPass(new AddEventAliasesPass([
305+
MyCustomEvent::class => 'my_custom_event',
306+
]));
307+
}
308+
}
309+
310+
The compiler pass will always extend the existing list of aliases. Because of
311+
that, it is safe to register multiple instances of the pass with different
312+
configurations.
313+
314+
.. versionadded:: 4.4
315+
316+
The ``AddEventAliasesPass`` class was introduced in Symfony 4.4.
317+
251318
Debugging Event Listeners
252319
-------------------------
253320

0 commit comments

Comments
 (0)