1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EventyClassic; |
4
|
|
|
|
5
|
|
|
abstract class Event |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Holds the event listeners. |
9
|
|
|
* |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
protected $listeners = null; |
13
|
|
|
|
14
|
17 |
|
public function __construct() |
15
|
|
|
{ |
16
|
17 |
|
$this->listeners = []; |
17
|
17 |
|
} |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Adds a listener. |
21
|
|
|
* |
22
|
|
|
* @param string $hook Hook name. |
23
|
|
|
* @param mixed $callback Function to execute. |
24
|
|
|
* @param integer $priority Priority of the action. |
25
|
|
|
* @param integer $arguments Number of arguments to accept. |
26
|
|
|
* |
27
|
|
|
* @return Event |
28
|
|
|
*/ |
29
|
17 |
|
public function listen($hook, $callback, $priority = 20, $arguments = 1) |
30
|
|
|
{ |
31
|
17 |
|
$this->listeners[] = [ |
32
|
17 |
|
'hook' => $hook, |
33
|
17 |
|
'callback' => $callback, |
34
|
17 |
|
'priority' => $priority, |
35
|
17 |
|
'arguments' => $arguments, |
36
|
|
|
]; |
37
|
|
|
|
38
|
17 |
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Removes a listener. |
43
|
|
|
* |
44
|
|
|
* @param string $hook Hook name. |
45
|
|
|
* @param mixed $callback Function to execute. |
46
|
|
|
* @param int $priority Priority of the action. |
47
|
|
|
*/ |
48
|
2 |
|
public function remove($hook, $callback, $priority = 20) |
49
|
|
|
{ |
50
|
2 |
|
if ($this->listeners) { |
|
|
|
|
51
|
2 |
|
$listeners = $this->listeners; |
52
|
2 |
|
foreach ($this->listeners as $key => $value) { |
53
|
2 |
|
if ($value['hook'] == $hook && |
54
|
2 |
|
$value['callback'] == $callback && |
55
|
2 |
|
$value['priority'] == $priority |
56
|
|
|
) { |
57
|
2 |
|
unset($listeners[$key]); |
58
|
|
|
} |
59
|
|
|
} |
60
|
2 |
|
$this->listeners = $listeners; |
61
|
|
|
} |
62
|
2 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Remove all listeners with given hook in collection. If no hook, clear all listeners. |
66
|
|
|
* |
67
|
|
|
* @param string $hook Hook name |
68
|
|
|
*/ |
69
|
4 |
|
public function removeAll($hook = null) |
70
|
|
|
{ |
71
|
4 |
|
if ($this->listeners) { |
|
|
|
|
72
|
4 |
|
if ($hook) { |
73
|
2 |
|
$listeners = $this->listeners; |
74
|
2 |
|
foreach ($this->listeners as $key => $value) { |
75
|
2 |
|
if ($value['hook'] == $hook) { |
76
|
2 |
|
unset($listeners[$key]); |
77
|
|
|
} |
78
|
|
|
} |
79
|
2 |
|
$this->listeners = $listeners; |
80
|
|
|
} else { |
81
|
2 |
|
$this->listeners = []; |
82
|
|
|
} |
83
|
|
|
} |
84
|
4 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Gets a sorted list of all listeners. |
88
|
|
|
* |
89
|
|
|
* @return array |
90
|
|
|
*/ |
91
|
17 |
|
public function getListeners() |
92
|
|
|
{ |
93
|
17 |
|
$listeners = $this->listeners; |
94
|
|
|
|
95
|
17 |
|
if (count($listeners) == 0) { |
96
|
3 |
|
return []; |
97
|
|
|
} |
98
|
|
|
|
99
|
17 |
|
reset($listeners); |
100
|
17 |
|
$key = key($listeners); |
101
|
|
|
|
102
|
17 |
|
$same = true; |
103
|
17 |
|
$previous = $listeners[$key]; |
104
|
17 |
|
foreach ($listeners as $listener) { |
105
|
17 |
|
if ($previous['priority'] != $listener['priority']) { |
106
|
3 |
|
$same = false; |
107
|
3 |
|
break; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
111
|
17 |
|
if ($same) { |
112
|
15 |
|
return $listeners; |
113
|
|
|
} |
114
|
|
|
|
115
|
3 |
|
usort( |
116
|
3 |
|
$listeners, |
117
|
|
|
function ($a, $b) { |
118
|
3 |
|
return ($a['priority'] - $b['priority']); |
119
|
3 |
|
} |
120
|
|
|
); |
121
|
|
|
|
122
|
3 |
|
return $listeners; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Gets the function. |
127
|
|
|
* |
128
|
|
|
* @param mixed $callback Callback |
129
|
|
|
* |
130
|
|
|
* @return callable A closure |
131
|
|
|
* |
132
|
|
|
* @throws \Exception |
133
|
|
|
*/ |
134
|
9 |
|
protected function getFunction($callback) |
135
|
|
|
{ |
136
|
9 |
|
if (is_callable($callback)) { |
137
|
7 |
|
return $callback; |
138
|
|
|
} |
139
|
|
|
|
140
|
2 |
|
throw new \Exception('$callback is not a Callable', 1); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* Fires a new action. |
145
|
|
|
* |
146
|
|
|
* @param string $action Name of action |
147
|
|
|
* @param array $args Arguments passed to the action |
148
|
|
|
*/ |
149
|
|
|
abstract public function fire($action, $args); |
150
|
|
|
} |
151
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.