Dispatcher.php
10.3 KB
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
487
488
489
490
491
492
493
494
495
496
497
498
499
<?php
/**
* Part of the Joomla Framework Event Package
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE
*/
namespace Joomla\Event;
use Closure;
use InvalidArgumentException;
/**
* Implementation of a DispatcherInterface supporting
* prioritized listeners.
*
* @since 1.0
*/
class Dispatcher implements DispatcherInterface
{
/**
* An array of registered events indexed by
* the event names.
*
* @var EventInterface[]
*
* @since 1.0
*/
protected $events = array();
/**
* A regular expression that will filter listener method names.
*
* @var string
* @since 1.0
* @deprecated 1.1.0
*/
protected $listenerFilter;
/**
* An array of ListenersPriorityQueue indexed
* by the event names.
*
* @var ListenersPriorityQueue[]
*
* @since 1.0
*/
protected $listeners = array();
/**
* Set an event to the dispatcher.
* It will replace any event with the same name.
*
* @param EventInterface $event The event.
*
* @return Dispatcher This method is chainable.
*
* @since 1.0
*/
public function setEvent(EventInterface $event)
{
$this->events[$event->getName()] = $event;
return $this;
}
/**
* Sets a regular expression to filter the class methods when adding a listener.
*
* @param string $regex A regular expression (for example '^on' will only register methods starting with "on").
*
* @return Dispatcher This method is chainable.
*
* @since 1.0
* @deprecated 1.1.0 Incorporate a method in your listener object such as `getEvents` to feed into the `setListener` method.
*/
public function setListenerFilter($regex)
{
$this->listenerFilter = $regex;
return $this;
}
/**
* Add an event to this dispatcher, only if it is not existing.
*
* @param EventInterface $event The event.
*
* @return Dispatcher This method is chainable.
*
* @since 1.0
*/
public function addEvent(EventInterface $event)
{
if (!isset($this->events[$event->getName()]))
{
$this->events[$event->getName()] = $event;
}
return $this;
}
/**
* Tell if the given event has been added to this dispatcher.
*
* @param EventInterface|string $event The event object or name.
*
* @return boolean True if the listener has the given event, false otherwise.
*
* @since 1.0
*/
public function hasEvent($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
return isset($this->events[$event]);
}
/**
* Get the event object identified by the given name.
*
* @param string $name The event name.
* @param mixed $default The default value if the event was not registered.
*
* @return EventInterface|mixed The event of the default value.
*
* @since 1.0
*/
public function getEvent($name, $default = null)
{
if (isset($this->events[$name]))
{
return $this->events[$name];
}
return $default;
}
/**
* Remove an event from this dispatcher.
* The registered listeners will remain.
*
* @param EventInterface|string $event The event object or name.
*
* @return Dispatcher This method is chainable.
*
* @since 1.0
*/
public function removeEvent($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
if (isset($this->events[$event]))
{
unset($this->events[$event]);
}
return $this;
}
/**
* Get the registered events.
*
* @return EventInterface[] The registered event.
*
* @since 1.0
*/
public function getEvents()
{
return $this->events;
}
/**
* Clear all events.
*
* @return EventInterface[] The old events.
*
* @since 1.0
*/
public function clearEvents()
{
$events = $this->events;
$this->events = array();
return $events;
}
/**
* Count the number of registered event.
*
* @return integer The numer of registered events.
*
* @since 1.0
*/
public function countEvents()
{
return \count($this->events);
}
/**
* Add a listener to this dispatcher, only if not already registered to these events.
* If no events are specified, it will be registered to all events matching it's methods name.
* In the case of a closure, you must specify at least one event name.
*
* @param object|Closure $listener The listener
* @param array $events An associative array of event names as keys
* and the corresponding listener priority as values.
*
* @return Dispatcher This method is chainable.
*
* @throws InvalidArgumentException
*
* @since 1.0
*/
public function addListener($listener, array $events = array())
{
if (!\is_object($listener))
{
throw new InvalidArgumentException('The given listener is not an object.');
}
// We deal with a closure.
if ($listener instanceof Closure)
{
if (empty($events))
{
throw new InvalidArgumentException('No event name(s) and priority specified for the Closure listener.');
}
foreach ($events as $name => $priority)
{
if (!isset($this->listeners[$name]))
{
$this->listeners[$name] = new ListenersPriorityQueue;
}
$this->listeners[$name]->add($listener, $priority);
}
return $this;
}
// We deal with a "normal" object.
$methods = get_class_methods($listener);
if (!empty($events))
{
$methods = array_intersect($methods, array_keys($events));
}
// @deprecated
$regex = $this->listenerFilter ?: '.*';
foreach ($methods as $event)
{
// @deprecated - this outer `if` is deprecated.
if (preg_match("#$regex#", $event))
{
// Retain this inner code after removal of the outer `if`.
if (!isset($this->listeners[$event]))
{
$this->listeners[$event] = new ListenersPriorityQueue;
}
$priority = isset($events[$event]) ? $events[$event] : Priority::NORMAL;
$this->listeners[$event]->add($listener, $priority);
}
}
return $this;
}
/**
* Get the priority of the given listener for the given event.
*
* @param object|Closure $listener The listener.
* @param EventInterface|string $event The event object or name.
*
* @return mixed The listener priority or null if the listener doesn't exist.
*
* @since 1.0
*/
public function getListenerPriority($listener, $event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
if (isset($this->listeners[$event]))
{
return $this->listeners[$event]->getPriority($listener);
}
}
/**
* Get the listeners registered to the given event.
*
* @param EventInterface|string $event The event object or name.
*
* @return object[] An array of registered listeners sorted according to their priorities.
*
* @since 1.0
*/
public function getListeners($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
if (isset($this->listeners[$event]))
{
return $this->listeners[$event]->getAll();
}
return array();
}
/**
* Tell if the given listener has been added.
* If an event is specified, it will tell if the listener is registered for that event.
*
* @param object|Closure $listener The listener.
* @param EventInterface|string $event The event object or name.
*
* @return boolean True if the listener is registered, false otherwise.
*
* @since 1.0
*/
public function hasListener($listener, $event = null)
{
if ($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
if (isset($this->listeners[$event]))
{
return $this->listeners[$event]->has($listener);
}
}
else
{
foreach ($this->listeners as $queue)
{
if ($queue->has($listener))
{
return true;
}
}
}
return false;
}
/**
* Remove the given listener from this dispatcher.
* If no event is specified, it will be removed from all events it is listening to.
*
* @param object|Closure $listener The listener to remove.
* @param EventInterface|string $event The event object or name.
*
* @return Dispatcher This method is chainable.
*
* @since 1.0
*/
public function removeListener($listener, $event = null)
{
if ($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
if (isset($this->listeners[$event]))
{
$this->listeners[$event]->remove($listener);
}
}
else
{
foreach ($this->listeners as $queue)
{
$queue->remove($listener);
}
}
return $this;
}
/**
* Clear the listeners in this dispatcher.
* If an event is specified, the listeners will be cleared only for that event.
*
* @param EventInterface|string $event The event object or name.
*
* @return Dispatcher This method is chainable.
*
* @since 1.0
*/
public function clearListeners($event = null)
{
if ($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
if (isset($this->listeners[$event]))
{
unset($this->listeners[$event]);
}
}
else
{
$this->listeners = array();
}
return $this;
}
/**
* Count the number of registered listeners for the given event.
*
* @param EventInterface|string $event The event object or name.
*
* @return integer The number of registered listeners for the given event.
*
* @since 1.0
*/
public function countListeners($event)
{
if ($event instanceof EventInterface)
{
$event = $event->getName();
}
return isset($this->listeners[$event]) ? \count($this->listeners[$event]) : 0;
}
/**
* Trigger an event.
*
* @param EventInterface|string $event The event object or name.
*
* @return EventInterface The event after being passed through all listeners.
*
* @since 1.0
*/
public function triggerEvent($event)
{
if (!($event instanceof EventInterface))
{
if (isset($this->events[$event]))
{
$event = $this->events[$event];
}
else
{
$event = new Event($event);
}
}
if (isset($this->listeners[$event->getName()]))
{
foreach ($this->listeners[$event->getName()] as $listener)
{
if ($event->isStopped())
{
return $event;
}
if ($listener instanceof Closure)
{
\call_user_func($listener, $event);
}
else
{
\call_user_func(array($listener, $event->getName()), $event);
}
}
}
return $event;
}
}