@@ -220,11 +220,53 @@ determine which instance is passed.
220
220
$containerBuilder->register('subscriber_service_id', \AcmeSubscriber::class)
221
221
->addTag('kernel.event_subscriber');
222
222
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 by read the alias mapping from a
226
+ dedicated container parameter. This parameter can be extended by registering
227
+ anthother 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 that ``AddEventAliasesPass `` has to be processed before ``RegisterListenersPass ``.
254
+
223
255
By default, the listeners pass assumes that the event dispatcher's service
224
256
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 ``.
257
+ ``kernel.event_listener `` tag, that event subscribers are tagged
258
+ with the ``kernel.event_subscriber `` tag and that the alias mapping is
259
+ stored as parameter ``event_dispatcher.event_aliases ``. You can change these
260
+ default values by passing custom values to the constructors of
261
+ ``RegisterListenersPass `` and ``AddEventAliasesPass ``.
262
+
263
+ .. versionadded :: 4.3
264
+
265
+ Aliasing event names is possible since Symfony 4.3.
266
+
267
+ .. versionadded :: 4.4
268
+
269
+ The ``AddEventAliasesPass `` class was introduced in Symfony 4.4.
228
270
229
271
.. _event_dispatcher-closures-as-listeners :
230
272
0 commit comments