1
|
|
|
<?php |
2
|
|
|
// +---------------------------------------------------------------------- |
3
|
|
|
// | ThinkPHP [ WE CAN DO IT JUST THINK ] |
4
|
|
|
// +---------------------------------------------------------------------- |
5
|
|
|
// | Copyright (c) 2006~2019 http://thinkphp.cn All rights reserved. |
6
|
|
|
// +---------------------------------------------------------------------- |
7
|
|
|
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 ) |
8
|
|
|
// +---------------------------------------------------------------------- |
9
|
|
|
// | Author: Slince <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think; |
14
|
|
|
|
15
|
|
|
use Closure; |
16
|
|
|
use InvalidArgumentException; |
17
|
|
|
use LogicException; |
18
|
|
|
use think\exception\Handle; |
19
|
|
|
use Throwable; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 中间件管理类 |
23
|
|
|
* @package think |
|
|
|
|
24
|
|
|
*/ |
25
|
|
|
class Middleware |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* 中间件执行队列 |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $queue = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* 应用对象 |
35
|
|
|
* @var App |
36
|
|
|
*/ |
37
|
|
|
protected $app; |
38
|
|
|
|
39
|
|
|
public function __construct(App $app) |
|
|
|
|
40
|
|
|
{ |
41
|
|
|
$this->app = $app; |
42
|
|
|
} |
43
|
7 |
|
|
44
|
|
|
/** |
45
|
7 |
|
* 导入中间件 |
46
|
7 |
|
* @access public |
47
|
|
|
* @param array $middlewares |
|
|
|
|
48
|
7 |
|
* @param string $type 中间件类型 |
|
|
|
|
49
|
|
|
* @return void |
50
|
7 |
|
*/ |
51
|
|
|
public function import(array $middlewares = [], string $type = 'route'): void |
52
|
|
|
{ |
53
|
|
|
foreach ($middlewares as $middleware) { |
54
|
|
|
$this->add($middleware, $type); |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 注册中间件 |
60
|
|
|
* @access public |
61
|
|
|
* @param mixed $middleware |
|
|
|
|
62
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
63
|
|
|
* @return void |
64
|
7 |
|
*/ |
65
|
|
|
public function add($middleware, string $type = 'route'): void |
66
|
7 |
|
{ |
67
|
7 |
|
$middleware = $this->buildMiddleware($middleware, $type); |
68
|
|
|
|
69
|
|
|
if ($middleware) { |
70
|
|
|
$this->queue[$type][] = $middleware; |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* 注册控制器中间件 |
76
|
|
|
* @access public |
77
|
7 |
|
* @param mixed $middleware |
|
|
|
|
78
|
|
|
* @return void |
79
|
7 |
|
*/ |
80
|
|
|
public function controller($middleware): void |
81
|
|
|
{ |
82
|
7 |
|
$this->add($middleware, 'controller'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* 注册中间件到开始位置 |
87
|
|
|
* @access public |
88
|
|
|
* @param mixed $middleware |
|
|
|
|
89
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
90
|
|
|
*/ |
|
|
|
|
91
|
|
|
public function unshift($middleware, string $type = 'route') |
92
|
|
|
{ |
93
|
|
|
$middleware = $this->buildMiddleware($middleware, $type); |
94
|
|
|
|
95
|
|
|
if (!empty($middleware)) { |
96
|
|
|
array_unshift($this->queue[$type], $middleware); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* 获取注册的中间件 |
102
|
|
|
* @access public |
103
|
|
|
* @param string $type 中间件类型 |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function all(string $type = 'route'): array |
107
|
|
|
{ |
108
|
|
|
return $this->queue[$type] ?? []; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* 调度管道 |
113
|
|
|
* @access public |
114
|
|
|
* @param string $type 中间件类型 |
115
|
|
|
* @return Pipeline |
116
|
|
|
*/ |
117
|
|
|
public function pipeline(string $type = 'route') |
118
|
|
|
{ |
119
|
|
|
return (new Pipeline()) |
120
|
|
|
->through(array_map(function ($middleware) { |
|
|
|
|
121
|
|
|
return function ($request, $next) use ($middleware) { |
122
|
|
|
list($call, $param) = $middleware; |
123
|
|
|
if (is_array($call) && is_string($call[0])) { |
124
|
|
|
$call = [$this->app->make($call[0]), $call[1]]; |
125
|
|
|
} |
126
|
|
|
$response = call_user_func($call, $request, $next, $param); |
127
|
|
|
|
128
|
|
|
if (!$response instanceof Response) { |
129
|
|
|
throw new LogicException('The middleware must return Response instance'); |
130
|
|
|
} |
131
|
|
|
return $response; |
132
|
|
|
}; |
133
|
|
|
}, $this->sortMiddleware($this->queue[$type]))) |
|
|
|
|
134
|
|
|
->whenException([$this, 'handleException']); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* 结束调度 |
139
|
|
|
* @param Response $response |
|
|
|
|
140
|
|
|
*/ |
|
|
|
|
141
|
|
|
public function end(Response $response) |
142
|
|
|
{ |
143
|
|
|
foreach ($this->queue as $queue) { |
144
|
|
|
foreach ($queue as $middleware) { |
145
|
|
|
list($call,) = $middleware; |
146
|
|
|
if (is_array($call) && is_string($call[0])) { |
147
|
|
|
$instance = $this->app->make($call[0]); |
148
|
|
|
if (method_exists($instance, 'end')) { |
149
|
|
|
$instance->end($response); |
150
|
|
|
} |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* 异常处理 |
158
|
|
|
* @param Request $passable |
|
|
|
|
159
|
|
|
* @param Throwable $e |
|
|
|
|
160
|
|
|
* @return Response |
161
|
|
|
*/ |
162
|
|
|
public function handleException($passable, Throwable $e) |
163
|
|
|
{ |
164
|
|
|
/** @var Handle $handler */ |
|
|
|
|
165
|
|
|
$handler = $this->app->make(Handle::class); |
166
|
|
|
|
167
|
|
|
$handler->report($e); |
168
|
|
|
|
169
|
|
|
$response = $handler->render($passable, $e); |
170
|
|
|
|
171
|
|
|
return $response; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* 解析中间件 |
176
|
|
|
* @access protected |
177
|
|
|
* @param mixed $middleware |
|
|
|
|
178
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
179
|
|
|
* @return array |
180
|
|
|
*/ |
181
|
|
|
protected function buildMiddleware($middleware, string $type = 'route'): array |
182
|
|
|
{ |
183
|
|
|
if (is_array($middleware)) { |
184
|
|
|
list($middleware, $param) = $middleware; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if ($middleware instanceof Closure) { |
188
|
|
|
return [$middleware, $param ?? null]; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
if (!is_string($middleware)) { |
192
|
|
|
throw new InvalidArgumentException('The middleware is invalid'); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
//中间件别名检查 |
196
|
|
|
$alias = $this->app->config->get('middleware.alias', []); |
197
|
|
|
|
198
|
|
|
if (isset($alias[$middleware])) { |
199
|
|
|
$middleware = $alias[$middleware]; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
if (is_array($middleware)) { |
203
|
|
|
$this->import($middleware, $type); |
204
|
|
|
return []; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return [[$middleware, 'handle'], $param ?? null]; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* 中间件排序 |
212
|
|
|
* @param array $middlewares |
|
|
|
|
213
|
|
|
* @return array |
214
|
|
|
*/ |
215
|
|
|
protected function sortMiddleware(array $middlewares) |
216
|
|
|
{ |
217
|
|
|
$priority = $this->app->config->get('middleware.priority', []); |
218
|
|
|
uasort($middlewares, function ($a, $b) use ($priority) { |
|
|
|
|
219
|
|
|
$aPriority = $this->getMiddlewarePriority($priority, $a); |
220
|
|
|
$bPriority = $this->getMiddlewarePriority($priority, $b); |
221
|
|
|
return $bPriority - $aPriority; |
222
|
|
|
}); |
|
|
|
|
223
|
|
|
|
224
|
|
|
return $middlewares; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
|
|
|
|
228
|
|
|
* 获取中间件优先级 |
229
|
|
|
* @param $priority |
|
|
|
|
230
|
|
|
* @param $middleware |
|
|
|
|
231
|
|
|
* @return int |
232
|
|
|
*/ |
233
|
|
|
protected function getMiddlewarePriority($priority, $middleware) |
234
|
|
|
{ |
235
|
|
|
list($call,) = $middleware; |
236
|
|
|
if (is_array($call) && is_string($call[0])) { |
237
|
|
|
$index = array_search($call[0], array_reverse($priority)); |
238
|
|
|
return $index === false ? -1 : $index; |
|
|
|
|
239
|
|
|
} |
240
|
|
|
return -1; |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
} |
244
|
|
|
|