-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
ContainerAwareEventManager.php
217 lines (176 loc) · 6.46 KB
/
ContainerAwareEventManager.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\Doctrine;
use Doctrine\Common\EventArgs;
use Doctrine\Common\EventManager;
use Doctrine\Common\EventSubscriber;
use Psr\Container\ContainerInterface;
/**
* Allows lazy loading of listener and subscriber services.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ContainerAwareEventManager extends EventManager
{
private array $initialized = [];
private bool $initializedSubscribers = false;
private array $initializedHashMapping = [];
private array $methods = [];
/**
* @param list<array{string[], string|object}> $listeners List of [events, listener] tuples
*/
public function __construct(
private ContainerInterface $container,
private array $listeners = [],
) {
}
public function dispatchEvent(string $eventName, ?EventArgs $eventArgs = null): void
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
if (!isset($this->listeners[$eventName])) {
return;
}
$eventArgs ??= EventArgs::getEmptyInstance();
if (!isset($this->initialized[$eventName])) {
$this->initializeListeners($eventName);
}
foreach ($this->listeners[$eventName] as $hash => $listener) {
$listener->{$this->methods[$eventName][$hash]}($eventArgs);
}
}
public function getListeners(string $event): array
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
if (!isset($this->initialized[$event])) {
$this->initializeListeners($event);
}
return $this->listeners[$event];
}
public function getAllListeners(): array
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
foreach ($this->listeners as $event => $listeners) {
if (!isset($this->initialized[$event])) {
$this->initializeListeners($event);
}
}
return $this->listeners;
}
public function hasListeners(string $event): bool
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
return isset($this->listeners[$event]) && $this->listeners[$event];
}
public function addEventListener(string|array $events, object|string $listener): void
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
$hash = $this->getHash($listener);
foreach ((array) $events as $event) {
// Overrides listener if a previous one was associated already
// Prevents duplicate listeners on same event (same instance only)
$this->listeners[$event][$hash] = $listener;
if (\is_string($listener)) {
unset($this->initialized[$event]);
unset($this->initializedHashMapping[$event][$hash]);
} else {
$this->methods[$event][$hash] = $this->getMethod($listener, $event);
}
}
}
public function removeEventListener(string|array $events, object|string $listener): void
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
$hash = $this->getHash($listener);
foreach ((array) $events as $event) {
if (isset($this->initializedHashMapping[$event][$hash])) {
$hash = $this->initializedHashMapping[$event][$hash];
unset($this->initializedHashMapping[$event][$hash]);
}
// Check if we actually have this listener associated
if (isset($this->listeners[$event][$hash])) {
unset($this->listeners[$event][$hash]);
}
if (isset($this->methods[$event][$hash])) {
unset($this->methods[$event][$hash]);
}
}
}
public function addEventSubscriber(EventSubscriber $subscriber): void
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
parent::addEventSubscriber($subscriber);
}
public function removeEventSubscriber(EventSubscriber $subscriber): void
{
if (!$this->initializedSubscribers) {
$this->initializeSubscribers();
}
parent::removeEventSubscriber($subscriber);
}
private function initializeListeners(string $eventName): void
{
$this->initialized[$eventName] = true;
// We'll refill the whole array in order to keep the same order
$listeners = [];
foreach ($this->listeners[$eventName] as $hash => $listener) {
if (\is_string($listener)) {
$listener = $this->container->get($listener);
$newHash = $this->getHash($listener);
$this->initializedHashMapping[$eventName][$hash] = $newHash;
$listeners[$newHash] = $listener;
$this->methods[$eventName][$newHash] = $this->getMethod($listener, $eventName);
} else {
$listeners[$hash] = $listener;
}
}
$this->listeners[$eventName] = $listeners;
}
private function initializeSubscribers(): void
{
$this->initializedSubscribers = true;
$listeners = $this->listeners;
$this->listeners = [];
foreach ($listeners as $listener) {
if (\is_array($listener)) {
$this->addEventListener(...$listener);
continue;
}
throw new \InvalidArgumentException(\sprintf('Using Doctrine subscriber "%s" is not allowed. Register it as a listener instead, using e.g. the #[AsDoctrineListener] or #[AsDocumentListener] attribute.', \is_object($listener) ? get_debug_type($listener) : $listener));
}
}
private function getHash(string|object $listener): string
{
if (\is_string($listener)) {
return '_service_'.$listener;
}
return spl_object_hash($listener);
}
private function getMethod(object $listener, string $event): string
{
if (!method_exists($listener, $event) && method_exists($listener, '__invoke')) {
return '__invoke';
}
return $event;
}
}