-
Notifications
You must be signed in to change notification settings - Fork 6
/
EventManager.php
486 lines (424 loc) · 14.2 KB
/
EventManager.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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @since 2.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Event;
use Cake\Core\Exception\CakeException;
/**
* The event manager is responsible for keeping track of event listeners, passing the correct
* data to them, and firing them in the correct order, when associated events are triggered. You
* can create multiple instances of this object to manage local events or keep a single instance
* and pass it around to manage all events in your app.
*/
class EventManager implements EventManagerInterface
{
/**
* The default priority queue value for new, attached listeners
*
* @var int
*/
public static $defaultPriority = 10;
/**
* The globally available instance, used for dispatching events attached from any scope
*
* @var \Cake\Event\EventManager|null
*/
protected static $_generalManager;
/**
* List of listener callbacks associated to
*
* @var array
*/
protected $_listeners = [];
/**
* Internal flag to distinguish a common manager from the singleton
*
* @var bool
*/
protected $_isGlobal = false;
/**
* The event list object.
*
* @var \Cake\Event\EventList|null
*/
protected $_eventList;
/**
* Enables automatic adding of events to the event list object if it is present.
*
* @var bool
*/
protected $_trackEvents = false;
/**
* Returns the globally available instance of a Cake\Event\EventManager
* this is used for dispatching events attached from outside the scope
* other managers were created. Usually for creating hook systems or inter-class
* communication
*
* If called with the first parameter, it will be set as the globally available instance
*
* @param \Cake\Event\EventManager|null $manager Event manager instance.
* @return \Cake\Event\EventManager The global event manager
*/
public static function instance(?EventManager $manager = null)
{
if ($manager instanceof EventManager) {
static::$_generalManager = $manager;
}
if (empty(static::$_generalManager)) {
static::$_generalManager = new static();
}
static::$_generalManager->_isGlobal = true;
return static::$_generalManager;
}
/**
* @inheritDoc
*/
public function on($eventKey, $options = [], ?callable $callable = null)
{
if ($eventKey instanceof EventListenerInterface) {
$this->_attachSubscriber($eventKey);
return $this;
}
$argCount = func_num_args();
if ($argCount === 2) {
$this->_listeners[$eventKey][static::$defaultPriority][] = [
'callable' => $options,
];
return $this;
}
$priority = $options['priority'] ?? static::$defaultPriority;
$this->_listeners[$eventKey][$priority][] = [
'callable' => $callable,
];
return $this;
}
/**
* Auxiliary function to attach all implemented callbacks of a Cake\Event\EventListenerInterface class instance
* as individual methods on this manager
*
* @param \Cake\Event\EventListenerInterface $subscriber Event listener.
* @return void
*/
protected function _attachSubscriber(EventListenerInterface $subscriber): void
{
foreach ($subscriber->implementedEvents() as $eventKey => $function) {
$options = [];
$method = $function;
if (is_array($function) && isset($function['callable'])) {
[$method, $options] = $this->_extractCallable($function, $subscriber);
} elseif (is_array($function) && is_numeric(key($function))) {
foreach ($function as $f) {
[$method, $options] = $this->_extractCallable($f, $subscriber);
$this->on($eventKey, $options, $method);
}
continue;
}
if (is_string($method)) {
$method = [$subscriber, $function];
}
$this->on($eventKey, $options, $method);
}
}
/**
* Auxiliary function to extract and return a PHP callback type out of the callable definition
* from the return value of the `implementedEvents` method on a Cake\Event\EventListenerInterface
*
* @param array $function the array taken from a handler definition for an event
* @param \Cake\Event\EventListenerInterface $object The handler object
* @return array
*/
protected function _extractCallable(array $function, EventListenerInterface $object): array
{
/** @var callable $method */
$method = $function['callable'];
$options = $function;
unset($options['callable']);
if (is_string($method)) {
/** @var callable $method */
$method = [$object, $method];
}
return [$method, $options];
}
/**
* @inheritDoc
*/
public function off($eventKey, $callable = null)
{
if ($eventKey instanceof EventListenerInterface) {
$this->_detachSubscriber($eventKey);
return $this;
}
if (!is_string($eventKey)) {
if (!is_callable($eventKey)) {
throw new CakeException(
'First argument of EventManager::off() must be ' .
' string or EventListenerInterface instance or callable.'
);
}
foreach (array_keys($this->_listeners) as $name) {
$this->off($name, $eventKey);
}
return $this;
}
if ($callable instanceof EventListenerInterface) {
$this->_detachSubscriber($callable, $eventKey);
return $this;
}
if ($callable === null) {
unset($this->_listeners[$eventKey]);
return $this;
}
if (empty($this->_listeners[$eventKey])) {
return $this;
}
foreach ($this->_listeners[$eventKey] as $priority => $callables) {
foreach ($callables as $k => $callback) {
if ($callback['callable'] === $callable) {
unset($this->_listeners[$eventKey][$priority][$k]);
break;
}
}
}
return $this;
}
/**
* Auxiliary function to help detach all listeners provided by an object implementing EventListenerInterface
*
* @param \Cake\Event\EventListenerInterface $subscriber the subscriber to be detached
* @param string|null $eventKey optional event key name to unsubscribe the listener from
* @return void
*/
protected function _detachSubscriber(EventListenerInterface $subscriber, ?string $eventKey = null): void
{
$events = $subscriber->implementedEvents();
if (!empty($eventKey) && empty($events[$eventKey])) {
return;
}
if (!empty($eventKey)) {
$events = [$eventKey => $events[$eventKey]];
}
foreach ($events as $key => $function) {
if (is_array($function)) {
if (is_numeric(key($function))) {
foreach ($function as $handler) {
$handler = $handler['callable'] ?? $handler;
$this->off($key, [$subscriber, $handler]);
}
continue;
}
$function = $function['callable'];
}
$this->off($key, [$subscriber, $function]);
}
}
/**
* @inheritDoc
*/
public function dispatch($event): EventInterface
{
if (is_string($event)) {
$event = new Event($event);
}
$listeners = $this->listeners($event->getName());
if ($this->_trackEvents) {
$this->addEventToList($event);
}
if (!$this->_isGlobal && static::instance()->isTrackingEvents()) {
static::instance()->addEventToList($event);
}
if (empty($listeners)) {
return $event;
}
foreach ($listeners as $listener) {
if ($event->isStopped()) {
break;
}
$result = $this->_callListener($listener['callable'], $event);
if ($result === false) {
$event->stopPropagation();
}
if ($result !== null) {
$event->setResult($result);
}
}
return $event;
}
/**
* Calls a listener.
*
* @param callable $listener The listener to trigger.
* @param \Cake\Event\EventInterface $event Event instance.
* @return mixed The result of the $listener function.
*/
protected function _callListener(callable $listener, EventInterface $event)
{
$data = (array)$event->getData();
return $listener($event, ...array_values($data));
}
/**
* @inheritDoc
*/
public function listeners(string $eventKey): array
{
$localListeners = [];
if (!$this->_isGlobal) {
$localListeners = $this->prioritisedListeners($eventKey);
$localListeners = empty($localListeners) ? [] : $localListeners;
}
$globalListeners = static::instance()->prioritisedListeners($eventKey);
$globalListeners = empty($globalListeners) ? [] : $globalListeners;
$priorities = array_merge(array_keys($globalListeners), array_keys($localListeners));
$priorities = array_unique($priorities);
asort($priorities);
$result = [];
foreach ($priorities as $priority) {
if (isset($globalListeners[$priority])) {
$result = array_merge($result, $globalListeners[$priority]);
}
if (isset($localListeners[$priority])) {
$result = array_merge($result, $localListeners[$priority]);
}
}
return $result;
}
/**
* Returns the listeners for the specified event key indexed by priority
*
* @param string $eventKey Event key.
* @return array
*/
public function prioritisedListeners(string $eventKey): array
{
if (empty($this->_listeners[$eventKey])) {
return [];
}
return $this->_listeners[$eventKey];
}
/**
* Returns the listeners matching a specified pattern
*
* @param string $eventKeyPattern Pattern to match.
* @return array
*/
public function matchingListeners(string $eventKeyPattern): array
{
$matchPattern = '/' . preg_quote($eventKeyPattern, '/') . '/';
$matches = array_intersect_key(
$this->_listeners,
array_flip(
preg_grep($matchPattern, array_keys($this->_listeners), 0)
)
);
return $matches;
}
/**
* Returns the event list.
*
* @return \Cake\Event\EventList|null
*/
public function getEventList(): ?EventList
{
return $this->_eventList;
}
/**
* Adds an event to the list if the event list object is present.
*
* @param \Cake\Event\EventInterface $event An event to add to the list.
* @return $this
*/
public function addEventToList(EventInterface $event)
{
if ($this->_eventList) {
$this->_eventList->add($event);
}
return $this;
}
/**
* Enables / disables event tracking at runtime.
*
* @param bool $enabled True or false to enable / disable it.
* @return $this
*/
public function trackEvents(bool $enabled)
{
$this->_trackEvents = $enabled;
return $this;
}
/**
* Returns whether this manager is set up to track events
*
* @return bool
*/
public function isTrackingEvents(): bool
{
return $this->_trackEvents && $this->_eventList;
}
/**
* Enables the listing of dispatched events.
*
* @param \Cake\Event\EventList $eventList The event list object to use.
* @return $this
*/
public function setEventList(EventList $eventList)
{
$this->_eventList = $eventList;
$this->_trackEvents = true;
return $this;
}
/**
* Disables the listing of dispatched events.
*
* @return $this
*/
public function unsetEventList()
{
$this->_eventList = null;
$this->_trackEvents = false;
return $this;
}
/**
* Debug friendly object properties.
*
* @return array
*/
public function __debugInfo(): array
{
$properties = get_object_vars($this);
$properties['_generalManager'] = '(object) EventManager';
$properties['_listeners'] = [];
foreach ($this->_listeners as $key => $priorities) {
$listenerCount = 0;
foreach ($priorities as $listeners) {
$listenerCount += count($listeners);
}
$properties['_listeners'][$key] = $listenerCount . ' listener(s)';
}
if ($this->_eventList) {
$count = count($this->_eventList);
for ($i = 0; $i < $count; $i++) {
$event = $this->_eventList[$i];
try {
$subject = $event->getSubject();
$properties['_dispatchedEvents'][] = $event->getName() . ' with subject ' . get_class($subject);
} catch (CakeException $e) {
$properties['_dispatchedEvents'][] = $event->getName() . ' with no subject';
}
}
} else {
$properties['_dispatchedEvents'] = null;
}
unset($properties['_eventList']);
return $properties;
}
}