Observable.php
7.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
<?php
/*
* This file is part of the overtrue/wechat.
*
* (c) overtrue <i@overtrue.me>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace EasyWeChat\Kernel\Traits;
use EasyWeChat\Kernel\Clauses\Clause;
use EasyWeChat\Kernel\Contracts\EventHandlerInterface;
use EasyWeChat\Kernel\Decorators\FinallyResult;
use EasyWeChat\Kernel\Decorators\TerminateResult;
use EasyWeChat\Kernel\Exceptions\InvalidArgumentException;
use EasyWeChat\Kernel\ServiceContainer;
/**
* Trait Observable.
*
* @author overtrue <i@overtrue.me>
*/
trait Observable
{
/**
* @var array
*/
protected $handlers = [];
/**
* @var array
*/
protected $clauses = [];
/**
* @param \Closure|EventHandlerInterface|callable|string $handler
* @param \Closure|EventHandlerInterface|callable|string $condition
*
* @return \EasyWeChat\Kernel\Clauses\Clause
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \ReflectionException
*/
public function push($handler, $condition = '*')
{
list($handler, $condition) = $this->resolveHandlerAndCondition($handler, $condition);
if (!isset($this->handlers[$condition])) {
$this->handlers[$condition] = [];
}
array_push($this->handlers[$condition], $handler);
return $this->newClause($handler);
}
/**
* @param \Closure|EventHandlerInterface|string $handler
* @param \Closure|EventHandlerInterface|string $condition
*
* @return \EasyWeChat\Kernel\Clauses\Clause
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \ReflectionException
*/
public function unshift($handler, $condition = '*')
{
list($handler, $condition) = $this->resolveHandlerAndCondition($handler, $condition);
if (!isset($this->handlers[$condition])) {
$this->handlers[$condition] = [];
}
array_unshift($this->handlers[$condition], $handler);
return $this->newClause($handler);
}
/**
* @param string $condition
* @param \Closure|EventHandlerInterface|string $handler
*
* @return \EasyWeChat\Kernel\Clauses\Clause
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \ReflectionException
*/
public function observe($condition, $handler)
{
return $this->push($handler, $condition);
}
/**
* @param string $condition
* @param \Closure|EventHandlerInterface|string $handler
*
* @return \EasyWeChat\Kernel\Clauses\Clause
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \ReflectionException
*/
public function on($condition, $handler)
{
return $this->push($handler, $condition);
}
/**
* @param string|int $event
* @param mixed ...$payload
*
* @return mixed|null
*/
public function dispatch($event, $payload)
{
return $this->notify($event, $payload);
}
/**
* @param string|int $event
* @param mixed ...$payload
*
* @return mixed|null
*/
public function notify($event, $payload)
{
$result = null;
foreach ($this->handlers as $condition => $handlers) {
if ('*' === $condition || ($condition & $event) === $event) {
foreach ($handlers as $handler) {
if ($clause = $this->clauses[$this->getHandlerHash($handler)] ?? null) {
if ($clause->intercepted($payload)) {
continue;
}
}
$response = $this->callHandler($handler, $payload);
switch (true) {
case $response instanceof TerminateResult:
return $response->content;
case true === $response:
continue 2;
case false === $response:
break 2;
case !empty($response) && !($result instanceof FinallyResult):
$result = $response;
}
}
}
}
return $result instanceof FinallyResult ? $result->content : $result;
}
/**
* @return array
*/
public function getHandlers()
{
return $this->handlers;
}
/**
* @param mixed $handler
*
* @return \EasyWeChat\Kernel\Clauses\Clause
*/
protected function newClause($handler): Clause
{
return $this->clauses[$this->getHandlerHash($handler)] = new Clause();
}
/**
* @param mixed $handler
*
* @return string
*/
protected function getHandlerHash($handler)
{
if (is_string($handler)) {
return $handler;
}
if (is_array($handler)) {
return is_string($handler[0])
? $handler[0].'::'.$handler[1]
: get_class($handler[0]).$handler[1];
}
return spl_object_hash($handler);
}
/**
* @param callable $handler
* @param mixed $payload
*
* @return mixed
*/
protected function callHandler(callable $handler, $payload)
{
try {
return call_user_func_array($handler, [$payload]);
} catch (\Exception $e) {
if (property_exists($this, 'app') && $this->app instanceof ServiceContainer) {
$this->app['logger']->error($e->getCode().': '.$e->getMessage(), [
'code' => $e->getCode(),
'message' => $e->getMessage(),
'file' => $e->getFile(),
'line' => $e->getLine(),
]);
}
}
}
/**
* @param mixed $handler
*
* @return \Closure
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \ReflectionException
*/
protected function makeClosure($handler)
{
if (is_callable($handler)) {
return $handler;
}
if (is_string($handler) && '*' !== $handler) {
if (!class_exists($handler)) {
throw new InvalidArgumentException(sprintf('Class "%s" not exists.', $handler));
}
if (!in_array(EventHandlerInterface::class, (new \ReflectionClass($handler))->getInterfaceNames(), true)) {
throw new InvalidArgumentException(sprintf('Class "%s" not an instance of "%s".', $handler, EventHandlerInterface::class));
}
return function ($payload) use ($handler) {
return (new $handler($this->app ?? null))->handle($payload);
};
}
if ($handler instanceof EventHandlerInterface) {
return function () use ($handler) {
return $handler->handle(...func_get_args());
};
}
throw new InvalidArgumentException('No valid handler is found in arguments.');
}
/**
* @param mixed $handler
* @param mixed $condition
*
* @return array
*
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \ReflectionException
*/
protected function resolveHandlerAndCondition($handler, $condition): array
{
if (is_int($handler) || (is_string($handler) && !class_exists($handler))) {
list($handler, $condition) = [$condition, $handler];
}
return [$this->makeClosure($handler), $condition];
}
}