|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace suda\framework; |
|
4
|
|
|
|
|
5
|
|
|
use ArrayIterator; |
|
6
|
|
|
use Iterator; |
|
7
|
|
|
use suda\framework\runnable\Runnable; |
|
8
|
|
|
|
|
9
|
|
|
class Event |
|
10
|
|
|
{ |
|
11
|
|
|
protected $queue = []; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* 加载事件处理 |
|
15
|
|
|
* |
|
16
|
|
|
* @param array $arrays |
|
17
|
|
|
* @return void |
|
18
|
|
|
*/ |
|
19
|
|
|
public function load(array $arrays) |
|
20
|
|
|
{ |
|
21
|
|
|
$this->queue = array_merge_recursive($this->queue, $arrays); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* 注册一条命令 |
|
26
|
|
|
* |
|
27
|
|
|
* @param string $name |
|
28
|
|
|
* @param mixed $command |
|
29
|
|
|
* @return void |
|
30
|
|
|
*/ |
|
31
|
|
|
public function listen(string $name, $command) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->add($name, $command); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* 注册一条命令 |
|
38
|
|
|
* |
|
39
|
|
|
* @param string $name |
|
40
|
|
|
* @param mixed $command |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function register(string $name, $command) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->add($name, $command); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* 添加命令到底部 |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param mixed $command |
|
53
|
|
|
* @return void |
|
54
|
|
|
*/ |
|
55
|
|
|
public function add(string $name, $command) |
|
56
|
|
|
{ |
|
57
|
|
|
if (!in_array($command, $this->queue[$name] ?? [])) { |
|
58
|
|
|
$this->queue[$name][] = $command; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* 添加命令到顶部 |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $name |
|
66
|
|
|
* @param mixed $command |
|
67
|
|
|
* @return void |
|
68
|
|
|
*/ |
|
69
|
|
|
public function addTop(string $name, $command) |
|
70
|
|
|
{ |
|
71
|
|
|
if (array_key_exists($name, $this->queue) && is_array($this->queue[$name])) { |
|
72
|
|
|
if (!in_array($command, $this->queue[$name])) { |
|
73
|
|
|
array_unshift($this->queue[$name], $command); |
|
74
|
|
|
} |
|
75
|
|
|
} else { |
|
76
|
|
|
$this->add($name, $command); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* 移除一条命令 |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $name |
|
84
|
|
|
* @param mixed $remove |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
public function remove(string $name, $remove) |
|
88
|
|
|
{ |
|
89
|
|
|
if (array_key_exists($name, $this->queue) && is_array($this->queue[$name])) { |
|
90
|
|
|
foreach ($this->queue[$name] as $key => $command) { |
|
91
|
|
|
if ($command === $remove) { |
|
92
|
|
|
unset($this->queue[$name][$key]); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
} |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* 运行所有命令 |
|
100
|
|
|
* |
|
101
|
|
|
* @param string $name |
|
102
|
|
|
* @param array $args |
|
103
|
|
|
* @param bool $reverse |
|
104
|
|
|
* @return void |
|
105
|
|
|
*/ |
|
106
|
|
|
public function exec(string $name, array $args = [], bool $reverse = false) |
|
107
|
|
|
{ |
|
108
|
|
|
if ($this->hasListenEvent($name)) { |
|
109
|
|
|
$iterator = $this->getCallbackIterator($name, $reverse); |
|
110
|
|
|
foreach ($iterator as $command) { |
|
111
|
|
|
$this->call($name, $command, $args); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* 获取迭代器 |
|
118
|
|
|
* @param string $name |
|
119
|
|
|
* @param bool $reverse |
|
120
|
|
|
* @return Iterator |
|
121
|
|
|
*/ |
|
122
|
|
|
protected function getCallbackIterator(string $name, bool $reverse = false): Iterator { |
|
123
|
|
|
return $reverse?new ArrayIterator(array_reverse($this->queue[$name])):new ArrayIterator($this->queue[$name]); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
|
|
/** |
|
127
|
|
|
* 继续执行 |
|
128
|
|
|
* |
|
129
|
|
|
* @param string $name |
|
130
|
|
|
* @param mixed $value |
|
131
|
|
|
* @param array $args |
|
132
|
|
|
* @param bool $reverse |
|
133
|
|
|
* @return mixed |
|
134
|
|
|
*/ |
|
135
|
|
|
public function process(string $name, $value, array $args = [], bool $reverse = false) |
|
136
|
|
|
{ |
|
137
|
|
|
if ($this->hasListenEvent($name)) { |
|
138
|
|
|
array_unshift($args, $value); |
|
139
|
|
|
$iterator = $this->getCallbackIterator($name, $reverse); |
|
140
|
|
|
foreach ($iterator as $command) { |
|
141
|
|
|
$args[0] = $value; |
|
142
|
|
|
$value = $this->call($name, $command, $args); |
|
143
|
|
|
} |
|
144
|
|
|
} |
|
145
|
|
|
return $value; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* 继续执行 |
|
150
|
|
|
* |
|
151
|
|
|
* @param string $name |
|
152
|
|
|
* @param array $args |
|
153
|
|
|
* @param mixed $condition |
|
154
|
|
|
* @param bool $reverse |
|
155
|
|
|
* @return boolean |
|
156
|
|
|
*/ |
|
157
|
|
|
public function next(string $name, array $args = [], $condition = true, bool $reverse = false): bool |
|
158
|
|
|
{ |
|
159
|
|
|
if ($this->hasListenEvent($name)) { |
|
160
|
|
|
$iterator = $this->getCallbackIterator($name, $reverse); |
|
161
|
|
|
foreach ($iterator as $command) { |
|
162
|
|
|
if ($this->call($name, $command, $args) === $condition) { |
|
163
|
|
|
continue; |
|
164
|
|
|
} else { |
|
165
|
|
|
return false; |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
return true; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* 运行最先注入的命令 |
|
174
|
|
|
* |
|
175
|
|
|
* @param string $name |
|
176
|
|
|
* @param array $args |
|
177
|
|
|
* @return mixed |
|
178
|
|
|
*/ |
|
179
|
|
|
public function execFirst(string $name, array $args = []) |
|
180
|
|
|
{ |
|
181
|
|
|
if ($this->hasListenEvent($name)) { |
|
182
|
|
|
return $this->call($name, $this->queue[$name][0], $args); |
|
183
|
|
|
} |
|
184
|
|
|
return null; |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* 运行最后一个注入的命令 |
|
189
|
|
|
* |
|
190
|
|
|
* @param string $name |
|
191
|
|
|
* @param array $args |
|
192
|
|
|
* @return mixed |
|
193
|
|
|
*/ |
|
194
|
|
|
public function execLast(string $name, array $args = []) |
|
195
|
|
|
{ |
|
196
|
|
|
if ($this->hasListenEvent($name)) { |
|
197
|
|
|
$last = count($this->queue[$name]) - 1; |
|
198
|
|
|
return $this->call($name, $this->queue[$name][$last], $args); |
|
199
|
|
|
} |
|
200
|
|
|
return null; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* 调用对象 |
|
205
|
|
|
* |
|
206
|
|
|
* @param string $event |
|
207
|
|
|
* @param mixed $command |
|
208
|
|
|
* @param array $args |
|
209
|
|
|
* @return mixed |
|
210
|
|
|
*/ |
|
211
|
|
|
protected function call(string $event, $command, array &$args) |
|
212
|
|
|
{ |
|
213
|
|
|
return (new Runnable($command))->apply($args); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* 判断是监控事件 |
|
218
|
|
|
* |
|
219
|
|
|
* @param string $name |
|
220
|
|
|
* @return boolean |
|
221
|
|
|
*/ |
|
222
|
|
|
public function hasListenEvent(string $name): bool |
|
223
|
|
|
{ |
|
224
|
|
|
if (array_key_exists($name, $this->queue) && is_array($this->queue[$name])) { |
|
225
|
|
|
return count($this->queue[$name]) > 0; |
|
226
|
|
|
} |
|
227
|
|
|
return false; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|