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 InvalidArgumentException; |
16
|
|
|
use LogicException; |
17
|
|
|
use think\exception\HttpResponseException; |
18
|
|
|
|
19
|
|
|
class Middleware |
|
|
|
|
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* 中间件执行队列 |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
protected $queue = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* 配置 |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $config = [ |
32
|
|
|
'default_namespace' => 'app\\http\\middleware\\', |
33
|
|
|
]; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* 应用对象 |
37
|
|
|
* @var App |
38
|
|
|
*/ |
39
|
|
|
protected $app; |
40
|
|
|
|
41
|
|
|
public function __construct(array $config = []) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->config = array_merge($this->config, $config); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function __make(App $app, Config $config) |
|
|
|
|
47
|
|
|
{ |
48
|
|
|
return (new static($config->get('middleware')))->setApp($app); |
|
|
|
|
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
public function setConfig(array $config): void |
|
|
|
|
52
|
|
|
{ |
53
|
|
|
$this->config = array_merge($this->config, $config); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 设置应用对象 |
58
|
|
|
* @access public |
59
|
|
|
* @param App $app |
|
|
|
|
60
|
|
|
* @return $this |
61
|
|
|
*/ |
62
|
|
|
public function setApp(App $app) |
63
|
|
|
{ |
64
|
|
|
$this->app = $app; |
65
|
|
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* 导入中间件 |
70
|
|
|
* @access public |
71
|
|
|
* @param array $middlewares |
|
|
|
|
72
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
|
|
public function import(array $middlewares = [], string $type = 'route'): void |
76
|
|
|
{ |
77
|
|
|
foreach ($middlewares as $middleware) { |
78
|
|
|
$this->add($middleware, $type); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* 注册中间件 |
84
|
|
|
* @access public |
85
|
|
|
* @param mixed $middleware |
|
|
|
|
86
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
87
|
|
|
* @return void |
88
|
|
|
*/ |
89
|
|
|
public function add($middleware, string $type = 'route'): void |
90
|
|
|
{ |
91
|
|
|
if (is_null($middleware)) { |
92
|
|
|
return; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
$middleware = $this->buildMiddleware($middleware, $type); |
96
|
|
|
|
97
|
|
|
if ($middleware) { |
98
|
|
|
$this->queue[$type][] = $middleware; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 注册控制器中间件 |
104
|
|
|
* @access public |
105
|
|
|
* @param mixed $middleware |
|
|
|
|
106
|
|
|
* @return void |
107
|
|
|
*/ |
108
|
|
|
public function controller($middleware): void |
109
|
|
|
{ |
110
|
|
|
$this->add($middleware, 'controller'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* 移除中间件 |
115
|
|
|
* @access public |
116
|
|
|
* @param mixed $middleware |
|
|
|
|
117
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
118
|
|
|
*/ |
|
|
|
|
119
|
|
|
public function unshift($middleware, string $type = 'route') |
120
|
|
|
{ |
121
|
|
|
if (is_null($middleware)) { |
122
|
|
|
return; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$middleware = $this->buildMiddleware($middleware, $type); |
126
|
|
|
|
127
|
|
|
if (!empty($middleware)) { |
128
|
|
|
array_unshift($this->queue[$type], $middleware); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* 获取注册的中间件 |
134
|
|
|
* @access public |
135
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
136
|
|
|
*/ |
|
|
|
|
137
|
|
|
public function all(string $type = 'route'): array |
138
|
|
|
{ |
139
|
|
|
return $this->queue[$type] ?? []; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* 中间件调度 |
144
|
|
|
* @access public |
145
|
|
|
* @param Request $request |
|
|
|
|
146
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
147
|
|
|
*/ |
|
|
|
|
148
|
|
|
public function dispatch(Request $request, string $type = 'route') |
149
|
|
|
{ |
150
|
|
|
return call_user_func($this->resolve($type), $request); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* 解析中间件 |
155
|
|
|
* @access protected |
156
|
|
|
* @param mixed $middleware |
|
|
|
|
157
|
|
|
* @param string $type 中间件类型 |
|
|
|
|
158
|
|
|
* @return array |
159
|
|
|
*/ |
160
|
|
|
protected function buildMiddleware($middleware, string $type = 'route'): array |
161
|
|
|
{ |
162
|
|
|
if (is_array($middleware)) { |
163
|
|
|
list($middleware, $param) = $middleware; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if ($middleware instanceof \Closure) { |
167
|
|
|
return [$middleware, $param ?? null]; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
if (!is_string($middleware)) { |
171
|
|
|
throw new InvalidArgumentException('The middleware is invalid'); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
if (false === strpos($middleware, '\\')) { |
175
|
|
|
if (isset($this->config[$middleware])) { |
176
|
|
|
$middleware = $this->config[$middleware]; |
177
|
|
|
} else { |
178
|
|
|
$middleware = $this->config['default_namespace'] . $middleware; |
179
|
|
|
} |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (is_array($middleware)) { |
183
|
|
|
$this->import($middleware, $type); |
184
|
|
|
return []; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (strpos($middleware, ':')) { |
188
|
|
|
list($middleware, $param) = explode(':', $middleware, 2); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return [[$this->app->make($middleware), 'handle'], $param ?? null]; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
protected function resolve(string $type = 'route') |
|
|
|
|
195
|
|
|
{ |
196
|
|
|
return function (Request $request) use ($type) { |
197
|
|
|
$middleware = array_shift($this->queue[$type]); |
198
|
|
|
|
199
|
|
|
if (null === $middleware) { |
200
|
|
|
throw new InvalidArgumentException('The queue was exhausted, with no response returned'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
list($call, $param) = $middleware; |
204
|
|
|
|
205
|
|
|
try { |
206
|
|
|
$response = call_user_func_array($call, [$request, $this->resolve($type), $param]); |
207
|
|
|
} catch (HttpResponseException $exception) { |
208
|
|
|
$response = $exception->getResponse(); |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
if (!$response instanceof Response) { |
212
|
|
|
throw new LogicException('The middleware must return Response instance'); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
return $response; |
216
|
|
|
}; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
} |
220
|
|
|
|