Skip to content

Commit ed990f8

Browse files
committed
[EventDispatcher] A compiler pass for aliased userland events.
1 parent 181eda9 commit ed990f8

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
CHANGELOG
22
=========
33

4+
4.4.0
5+
-----
6+
7+
* `AddEventAliasesPass` has been added, allowing applications and bundles to extend the event alias mapping used by `RegisterListenersPass`.
8+
49
4.3.0
510
-----
611

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\EventDispatcher\DependencyInjection;
13+
14+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
17+
/**
18+
* This pass allows bundles to extend the list of event aliases.
19+
*
20+
* @author Alexander M. Turek <[email protected]>
21+
*/
22+
class AddEventAliasesPass implements CompilerPassInterface
23+
{
24+
private $eventAliases;
25+
private $eventAliasesParameter;
26+
27+
public function __construct(array $eventAliases, string $eventAliasesParameter = 'event_dispatcher.event_aliases')
28+
{
29+
$this->eventAliases = $eventAliases;
30+
$this->eventAliasesParameter = $eventAliasesParameter;
31+
}
32+
33+
public function process(ContainerBuilder $container): void
34+
{
35+
$eventAliases = $container->hasParameter($this->eventAliasesParameter) ? $container->getParameter($this->eventAliasesParameter) : [];
36+
37+
$container->setParameter(
38+
$this->eventAliasesParameter,
39+
array_merge($eventAliases, $this->eventAliases)
40+
);
41+
}
42+
}

Tests/DependencyInjection/RegisterListenersPassTest.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\DependencyInjection\Argument\ServiceClosureArgument;
1616
use Symfony\Component\DependencyInjection\ContainerBuilder;
1717
use Symfony\Component\DependencyInjection\Reference;
18+
use Symfony\Component\EventDispatcher\DependencyInjection\AddEventAliasesPass;
1819
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
1920
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
2021

@@ -67,6 +68,9 @@ public function testAliasedEventSubscriber(): void
6768
$builder->register('my_event_subscriber', AliasedSubscriber::class)
6869
->addTag('kernel.event_subscriber');
6970

71+
$eventAliasPass = new AddEventAliasesPass([CustomEvent::class => 'custom_event']);
72+
$eventAliasPass->process($builder);
73+
7074
$registerListenersPass = new RegisterListenersPass();
7175
$registerListenersPass->process($builder);
7276

@@ -79,6 +83,14 @@ public function testAliasedEventSubscriber(): void
7983
0,
8084
],
8185
],
86+
[
87+
'addListener',
88+
[
89+
'custom_event',
90+
[new ServiceClosureArgument(new Reference('my_event_subscriber')), 'onCustomEvent'],
91+
0,
92+
],
93+
],
8294
];
8395
$this->assertEquals($expectedCalls, $builder->getDefinition('event_dispatcher')->getMethodCalls());
8496
}
@@ -202,8 +214,12 @@ public function testAliasedEventListener(): void
202214
$container = new ContainerBuilder();
203215
$container->setParameter('event_dispatcher.event_aliases', [AliasedEvent::class => 'aliased_event']);
204216
$container->register('foo', InvokableListenerService::class)->addTag('kernel.event_listener', ['event' => AliasedEvent::class, 'method' => 'onEvent']);
217+
$container->register('bar', InvokableListenerService::class)->addTag('kernel.event_listener', ['event' => CustomEvent::class, 'method' => 'onEvent']);
205218
$container->register('event_dispatcher');
206219

220+
$eventAliasPass = new AddEventAliasesPass([CustomEvent::class => 'custom_event']);
221+
$eventAliasPass->process($container);
222+
207223
$registerListenersPass = new RegisterListenersPass();
208224
$registerListenersPass->process($container);
209225

@@ -217,6 +233,14 @@ public function testAliasedEventListener(): void
217233
0,
218234
],
219235
],
236+
[
237+
'addListener',
238+
[
239+
'custom_event',
240+
[new ServiceClosureArgument(new Reference('bar')), 'onEvent'],
241+
0,
242+
],
243+
],
220244
];
221245
$this->assertEquals($expectedCalls, $definition->getMethodCalls());
222246
}
@@ -249,10 +273,15 @@ public static function getSubscribedEvents(): array
249273
{
250274
return [
251275
AliasedEvent::class => 'onAliasedEvent',
276+
CustomEvent::class => 'onCustomEvent',
252277
];
253278
}
254279
}
255280

256281
final class AliasedEvent
257282
{
258283
}
284+
285+
final class CustomEvent
286+
{
287+
}

0 commit comments

Comments
 (0)