|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace suda\application; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* 系统事件 |
|
8
|
|
|
*/ |
|
9
|
|
|
class Event |
|
10
|
|
|
{ |
|
11
|
|
|
protected $eventListener=[]; |
|
12
|
|
|
|
|
13
|
|
|
public function loadConfig(string $path, array $extra = []) |
|
|
|
|
|
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
public function load(array $arrays) |
|
|
|
|
|
|
19
|
|
|
{ |
|
20
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* 注册一条命令 |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $name |
|
27
|
|
|
* @param mixed $command |
|
28
|
|
|
* @return void |
|
29
|
|
|
*/ |
|
30
|
|
|
public function listen(string $name, $command) |
|
31
|
|
|
{ |
|
32
|
|
|
self::add($name, $command); |
|
|
|
|
|
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* 注册一条命令 |
|
37
|
|
|
* |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @param mixed $command |
|
40
|
|
|
* @return void |
|
41
|
|
|
*/ |
|
42
|
|
|
public function register(string $name, $command) |
|
43
|
|
|
{ |
|
44
|
|
|
self::add($name, $command); |
|
|
|
|
|
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* 添加命令到底部 |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $name |
|
51
|
|
|
* @param mixed $command |
|
52
|
|
|
* @return void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function add(string $name, $command) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->eventListener[$name][]=$command; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* 添加监控到顶部 |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $name |
|
63
|
|
|
* @param mixed $command |
|
64
|
|
|
* @return void |
|
65
|
|
|
*/ |
|
66
|
|
|
public function addTop(string $name, $command) |
|
67
|
|
|
{ |
|
68
|
|
|
if (\array_key_exists($name, $this->eventListener[$name]) && is_array($this->eventListener[$name])) { |
|
69
|
|
|
array_unshift($this->eventListener[$name], $command); |
|
70
|
|
|
} else { |
|
71
|
|
|
$this->add($name, $command); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* 移除一条监控信息 |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $name |
|
79
|
|
|
* @param mixed $remove |
|
80
|
|
|
* @return void |
|
81
|
|
|
*/ |
|
82
|
|
|
public function remove(string $name, $remove) |
|
83
|
|
|
{ |
|
84
|
|
|
if (\array_key_exists($name, $this->eventListener[$name]) && is_array($this->eventListener[$name])) { |
|
85
|
|
|
foreach ($this->eventListener[$name] as $key=>$command) { |
|
86
|
|
|
if ($command === $remove) { |
|
87
|
|
|
unset($this->eventListener[$name][$key]); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
#=================================================================== |
|
94
|
|
|
# 命令运行 |
|
95
|
|
|
#=================================================================== |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* 运行所有命令 |
|
99
|
|
|
* |
|
100
|
|
|
* @param string $name |
|
101
|
|
|
* @param array $args |
|
102
|
|
|
* @return void |
|
103
|
|
|
*/ |
|
104
|
|
|
public function exec(string $name, array $args=[]) |
|
105
|
|
|
{ |
|
106
|
|
|
if ($this->exist($name)) { |
|
107
|
|
|
foreach ($this->eventListener[$name] as $command) { |
|
108
|
|
|
$this->call($command, $args); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* 运行,遇到返回指定条件则停止并返回true |
|
115
|
|
|
* |
|
116
|
|
|
* @param string $name |
|
117
|
|
|
* @param array $args |
|
118
|
|
|
* @param boolean $condition |
|
119
|
|
|
* @return boolean |
|
120
|
|
|
*/ |
|
121
|
|
|
public function execIf(string $name, array $args=[], $condition = true):bool |
|
122
|
|
|
{ |
|
123
|
|
|
if ($this->exist($name)) { |
|
124
|
|
|
foreach ($this->eventListener[$name] as $command) { |
|
125
|
|
|
if ($this->call($command, $args)===$condition) { |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
return false; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* 运行所有命令返回第一个非空值 |
|
135
|
|
|
* |
|
136
|
|
|
* @param string $name |
|
137
|
|
|
* @param array $args |
|
138
|
|
|
* @return mixed|null |
|
139
|
|
|
*/ |
|
140
|
|
|
public function execNotNull(string $name, array $args=[]) |
|
141
|
|
|
{ |
|
142
|
|
|
if ($this->exist($name)) { |
|
143
|
|
|
foreach ($this->eventListener[$name] as $command) { |
|
144
|
|
|
if (!is_null($value=$this->call($command, $args))) { |
|
145
|
|
|
return $value; |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
} |
|
149
|
|
|
return null; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* 运行最先注入的命令 |
|
154
|
|
|
* |
|
155
|
|
|
* @param string $name |
|
156
|
|
|
* @param array $args |
|
157
|
|
|
* @return mixed|null |
|
158
|
|
|
*/ |
|
159
|
|
|
public function execTop(string $name, array $args=[]) |
|
160
|
|
|
{ |
|
161
|
|
|
debug()->trace($name); |
|
|
|
|
|
|
162
|
|
|
if ($this->exist($name)) { |
|
163
|
|
|
return $this->call(array_shift($this->eventListener[$name]), $args); |
|
164
|
|
|
} |
|
165
|
|
|
return null; |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
/** |
|
169
|
|
|
* 运行最后一个注入的命令 |
|
170
|
|
|
* |
|
171
|
|
|
* @param string $name |
|
172
|
|
|
* @param array $args |
|
173
|
|
|
* @return mixed|null |
|
174
|
|
|
*/ |
|
175
|
|
|
public function execTail(string $name, array $args=[]) |
|
176
|
|
|
{ |
|
177
|
|
|
debug()->trace($name); |
|
178
|
|
|
if ($this->exist($name)) { |
|
179
|
|
|
return $this->call(array_pop($this->eventListener[$name]), $args); |
|
180
|
|
|
} |
|
181
|
|
|
return null; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
protected function call($command, array &$args) |
|
185
|
|
|
{ |
|
186
|
|
|
return (new EventRunner($command))->call($args); |
|
187
|
|
|
} |
|
188
|
|
|
|
|
189
|
|
|
protected function exist(string $name): bool { |
|
190
|
|
|
return \array_key_exists($name, $this->eventListener[$name]) && is_array($this->eventListener[$name]); |
|
191
|
|
|
} |
|
192
|
|
|
} |
|
193
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.