|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the LdapToolsBundle package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Chad Sikorra <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace LdapTools\Bundle\LdapToolsBundle\DependencyInjection\Compiler; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
|
14
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
16
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Add any tagged listeners/subscribers to the LdapTools event dispatcher. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Chad Sikorra <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class EventRegisterPass implements CompilerPassInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* The event subscriber tag name. |
|
27
|
|
|
*/ |
|
28
|
|
|
const SUBSCRIBER_TAG = 'ldap_tools.event_subscriber'; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* The event listener tag name. |
|
32
|
|
|
*/ |
|
33
|
|
|
const LISTENER_TAG = 'ldap_tools.event_listener'; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The event dispatcher service name. |
|
37
|
|
|
*/ |
|
38
|
|
|
const DISPATCHER = 'ldap_tools.event_dispatcher'; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @inheritdoc |
|
42
|
|
|
*/ |
|
43
|
|
|
public function process(ContainerBuilder $container) |
|
44
|
|
|
{ |
|
45
|
|
|
$subscribers = $container->findTaggedServiceIds(self::SUBSCRIBER_TAG); |
|
46
|
|
|
$listeners = $container->findTaggedServiceIds(self::LISTENER_TAG); |
|
47
|
|
|
|
|
48
|
|
|
if (empty($subscribers) && empty($listeners)) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
$dispatcher = $container->findDefinition(self::DISPATCHER); |
|
52
|
|
|
|
|
53
|
|
|
if (!empty($subscribers)) { |
|
54
|
|
|
$this->addSubscribersToEventDispatcher($container, $subscribers, $dispatcher); |
|
55
|
|
|
} |
|
56
|
|
|
if (!empty($listeners)) { |
|
57
|
|
|
$this->addListenersToEventDispatcher($container, $listeners, $dispatcher); |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* @param ContainerBuilder $container |
|
63
|
|
|
* @param array $events |
|
64
|
|
|
* @param Definition $dispatcher |
|
65
|
|
|
*/ |
|
66
|
|
|
protected function addSubscribersToEventDispatcher(ContainerBuilder $container, array $events, Definition $dispatcher) |
|
67
|
|
|
{ |
|
68
|
|
|
foreach ($events as $id => $event) { |
|
69
|
|
|
$this->validateTaggedService($container, $id); |
|
70
|
|
|
$dispatcher->addMethodCall('addSubscriber', [new Reference($id)]); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param ContainerBuilder $container |
|
76
|
|
|
* @param array $events |
|
77
|
|
|
* @param Definition $dispatcher |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function addListenersToEventDispatcher(ContainerBuilder $container, array $events, Definition $dispatcher) |
|
80
|
|
|
{ |
|
81
|
|
|
foreach ($events as $id => $event) { |
|
82
|
|
|
$this->validateTaggedService($container, $id); |
|
83
|
|
View Code Duplication |
if (!isset($event[0]['event'])) { |
|
|
|
|
|
|
84
|
|
|
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, self::LISTENER_TAG)); |
|
85
|
|
|
} |
|
86
|
|
View Code Duplication |
if (!isset($event[0]['method'])) { |
|
|
|
|
|
|
87
|
|
|
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "method" attribute on "%s" tags.', $id, self::LISTENER_TAG)); |
|
88
|
|
|
} |
|
89
|
|
|
$dispatcher->addMethodCall('addListener', [$event[0]['event'], [new Reference($id), $event[0]['method']]]); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param ContainerBuilder $container |
|
95
|
|
|
* @param string $id |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function validateTaggedService(ContainerBuilder $container, $id) |
|
98
|
|
|
{ |
|
99
|
|
|
if ($container->getDefinition($id)->isAbstract()) { |
|
100
|
|
|
throw new \InvalidArgumentException(sprintf( |
|
101
|
|
|
'The abstract service "%s" cannot be tagged as a LdapTools subscriber/listener.', |
|
102
|
|
|
$id |
|
103
|
|
|
)); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.