Skip to content

Commit 760f963

Browse files
committed
Merge branch '5.0'
* 5.0: Remove old versionadded directives Minor #12420 [EventDispatcher] Document event name aliases.
2 parents a69aece + d381770 commit 760f963

File tree

2 files changed

+95
-3
lines changed

2 files changed

+95
-3
lines changed

components/event_dispatcher.rst

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

223+
``RegisterListenersPass`` resolves aliased class names which for instance
224+
allows to refer to an event via the fully qualified class name (FQCN) of the
225+
event class. The pass will read the alias mapping from a dedicated container
226+
parameter. This parameter can be extended by registering another compiler pass,
227+
``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``.
228264

229265
.. _event_dispatcher-closures-as-listeners:
230266

event_dispatcher.rst

+56
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,62 @@ there are some minor advantages for each of them:
242242
* **Listeners are more flexible** because bundles can enable or disable each of
243243
them conditionally depending on some configuration value.
244244

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

0 commit comments

Comments
 (0)