|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Event; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\EventManager\EventManagerInterface; |
|
6
|
|
|
use Psr\EventManager\EventInterface; |
|
7
|
|
|
use Event\Event; |
|
8
|
|
|
use Event\ListenerQueue; |
|
9
|
|
|
|
|
10
|
|
|
class EventManager implements EventManagerInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $listenersHeap = []; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Attaches a listener to an event |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $event the event to attach too |
|
18
|
|
|
* @param callable $callback a callable function |
|
19
|
|
|
* @param int $priority the priority at which the $callback executed |
|
20
|
|
|
* @return bool true on success false on failure |
|
21
|
|
|
*/ |
|
22
|
4 |
|
public function attach($event, $callback, $priority = 0) |
|
23
|
|
|
{ |
|
24
|
4 |
|
if (is_string($event) |
|
25
|
4 |
|
&& is_callable($callback) |
|
26
|
4 |
|
&& is_integer($priority)) { |
|
27
|
3 |
|
if (!array_key_exists($event, $this->listenersHeap)) { |
|
28
|
3 |
|
$this->listenersHeap[$event] = new ListenerQueue; |
|
29
|
3 |
|
} |
|
30
|
|
|
|
|
31
|
3 |
|
$this->listenersHeap[$event]->add($callback, $priority); |
|
32
|
|
|
|
|
33
|
3 |
|
return true; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
1 |
|
return false; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Detaches a listener from an event |
|
41
|
|
|
* |
|
42
|
|
|
* @param string $event the event to attach too |
|
43
|
|
|
* @param callable $callback a callable function |
|
44
|
|
|
* @return bool true on success false on failure |
|
45
|
|
|
*/ |
|
46
|
1 |
|
public function detach($event, $callback) |
|
47
|
|
|
{ |
|
48
|
1 |
|
if (is_string($event) && is_callable($callback)) { |
|
49
|
1 |
|
if (array_key_exists($event, $this->listenersHeap)) { |
|
50
|
1 |
|
$this->listenersHeap[$event]->eject($callback); |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
return true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Clear all listeners for a given event |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $event |
|
63
|
|
|
* @return bool true on success false on failure |
|
64
|
|
|
*/ |
|
65
|
1 |
|
public function clearListeners($event) |
|
66
|
|
|
{ |
|
67
|
1 |
|
if (is_string($event)) { |
|
68
|
1 |
|
$this->listenersHeap[$event] = null; |
|
69
|
|
|
|
|
70
|
1 |
|
return true; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return false; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Trigger an event |
|
78
|
|
|
* |
|
79
|
|
|
* Can accept an EventInterface or will create one if not passed |
|
80
|
|
|
* |
|
81
|
|
|
* @param string|EventInterface $event |
|
82
|
|
|
* @param object|string $target |
|
83
|
|
|
* @param array|object $argv - arguments for listener callback |
|
84
|
|
|
* @return mixed |
|
85
|
|
|
*/ |
|
86
|
|
|
public function trigger($event, $target = null, $argv = []) |
|
87
|
|
|
{ |
|
88
|
|
|
$result = false; |
|
89
|
|
|
|
|
90
|
|
|
if (is_string($event) || (is_object($event) && $event instanceof EventInterface) |
|
91
|
|
|
&& (is_string($target) || is_object($target)) |
|
92
|
|
|
&& (is_array($argv) || is_object($argv))) { |
|
93
|
|
|
if (is_string($event)) { |
|
94
|
|
|
$result = $event = new Event($event); |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($event->isPropagationStopped()) { |
|
99
|
|
|
return; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$event = $event->getName(); |
|
103
|
|
|
|
|
104
|
|
|
$listeners = $this->listenersHeap[$event]->get(); |
|
105
|
|
|
|
|
106
|
|
|
foreach ($listeners as $listener) { |
|
107
|
|
|
call_user_func_array( |
|
108
|
|
|
$listener['callback'], |
|
109
|
|
|
$argv |
|
110
|
|
|
); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return $result; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Custom method for check event listeners |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $event |
|
120
|
|
|
* @return bool true on success false on failure |
|
121
|
|
|
*/ |
|
122
|
1 |
|
public function isExistListeners($event) |
|
123
|
|
|
{ |
|
124
|
1 |
|
if (is_string($event)) { |
|
125
|
1 |
|
if (is_null($this->listenersHeap[$event])) { |
|
126
|
1 |
|
return false; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
return true; |
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|