|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ConfigToken; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class EventManager |
|
7
|
|
|
{ |
|
8
|
|
|
/** @var EventManager */ |
|
9
|
|
|
private static $instance; |
|
10
|
|
|
/** @var EventListenerInterface[] */ |
|
11
|
|
|
protected $listeners; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Get the event manager singleton instance. |
|
15
|
|
|
* |
|
16
|
|
|
* @return EventManager The event manager singleton. |
|
17
|
|
|
*/ |
|
18
|
1 |
|
public static function getInstance() |
|
19
|
|
|
{ |
|
20
|
1 |
|
if (null === self::$instance) { |
|
21
|
1 |
|
self::$instance = new static(); |
|
22
|
1 |
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
return self::$instance; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
protected function __construct() |
|
28
|
|
|
{ |
|
29
|
1 |
|
$this->clear(); |
|
30
|
1 |
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @codeCoverageIgnore |
|
34
|
|
|
*/ |
|
35
|
|
|
private function __clone() |
|
36
|
|
|
{ |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Dispatch the given event to all registered listeners. |
|
41
|
|
|
* |
|
42
|
|
|
* @param Event $event |
|
43
|
|
|
*/ |
|
44
|
1 |
|
public function dispatch(Event $event) |
|
45
|
|
|
{ |
|
46
|
1 |
|
foreach ($this->listeners as $listener) { |
|
47
|
1 |
|
if (!$listener->handleEvent($event)) { |
|
48
|
1 |
|
break; |
|
49
|
|
|
} |
|
50
|
1 |
|
} |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Register an event listener. |
|
55
|
|
|
* |
|
56
|
|
|
* @param EventListenerInterface $listener The event listener. |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function register(EventListenerInterface $listener) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->listeners[spl_object_hash($listener)] = $listener; |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Un-register an event listener. |
|
65
|
|
|
* |
|
66
|
|
|
* @param EventListenerInterface $listener The event listener. |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function remove(EventListenerInterface $listener) |
|
69
|
|
|
{ |
|
70
|
1 |
|
if ($this->isRegistered($listener)) { |
|
71
|
1 |
|
unset($this->listeners[spl_object_hash($listener)]); |
|
72
|
1 |
|
} |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Un-register all event listeners |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function clear() |
|
79
|
|
|
{ |
|
80
|
1 |
|
$this->listeners = array(); |
|
81
|
1 |
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Check if the given event listener is registered. |
|
85
|
|
|
* |
|
86
|
|
|
* @param EventListenerInterface $listener |
|
87
|
|
|
* @return boolean |
|
88
|
|
|
*/ |
|
89
|
1 |
|
public function isRegistered(EventListenerInterface $listener) |
|
90
|
|
|
{ |
|
91
|
1 |
|
return isset($this->listeners[spl_object_hash($listener)]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Check if any event listeners are registered. |
|
96
|
|
|
* |
|
97
|
|
|
* @return boolean |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function hasListeners() |
|
100
|
|
|
{ |
|
101
|
1 |
|
return count($this->listeners) > 0; |
|
102
|
|
|
} |
|
103
|
|
|
} |