/
RegisterEventListenersAndSubscribersPass.php
62 lines (52 loc) · 2.08 KB
/
RegisterEventListenersAndSubscribersPass.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php
namespace Symfony\Bundle\DoctrineBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
class RegisterEventListenersAndSubscribersPass implements CompilerPassInterface
{
protected $container;
public function process(ContainerBuilder $container)
{
$this->container = $container;
foreach ($container->getDefinitions() as $id => $definition) {
if ('%doctrine.dbal.event_manager_class%' !== $definition->getClass()) {
continue;
}
$prefix = substr($id, 0, -1 * strlen('_connection.event_manager'));
$this->registerListeners($prefix, $definition);
$this->registerSubscribers($prefix, $definition);
}
}
protected function registerSubscribers($prefix, $definition)
{
$subscribers = array_merge(
$this->container->findTaggedServiceIds('doctrine.common.event_subscriber'),
$this->container->findTaggedServiceIds($prefix.'_event_subscriber')
);
foreach ($subscribers as $id => $instances) {
$definition->addMethodCall('addEventSubscriber', array(new Reference($id)));
}
}
protected function registerListeners($prefix, $definition)
{
$listeners = array_merge(
$this->container->findTaggedServiceIds('doctrine.common.event_listener'),
$this->container->findTaggedServiceIds($prefix.'_event_listener')
);
foreach ($listeners as $listenerId => $instances) {
$events = array();
foreach ($instances as $attributes) {
if (isset($attributes['event'])) {
$events[] = $attributes['event'];
}
}
if (0 < count($events)) {
$definition->addMethodCall('addListener', array(
$events,
new Reference($listenerId),
));
}
}
}
}