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: liu21st <[email protected]> |
10
|
|
|
// +---------------------------------------------------------------------- |
11
|
|
|
declare (strict_types = 1); |
12
|
|
|
|
13
|
|
|
namespace think; |
14
|
|
|
|
15
|
|
|
use think\exception\ClassNotFoundException; |
16
|
|
|
use think\exception\HttpResponseException; |
17
|
|
|
use think\route\Dispatch; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Web应用管理类 |
21
|
|
|
*/ |
|
|
|
|
22
|
|
|
class Web |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
|
|
|
|
26
|
|
|
* @var App |
27
|
|
|
*/ |
28
|
|
|
protected $app; |
29
|
|
|
|
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* 是否需要使用路由 |
33
|
|
|
* @var bool |
34
|
|
|
*/ |
35
|
|
|
protected $withRoute = true; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* 访问控制器层名称 |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $controllerLayer = 'controller'; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 是否使用控制器类库后缀 |
45
|
|
|
* @var bool |
46
|
|
|
*/ |
47
|
|
|
protected $controllerSuffix = false; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* 空控制器名称 |
51
|
|
|
* @var string |
52
|
|
|
*/ |
53
|
|
|
protected $emptyController = 'Error'; |
54
|
|
|
|
55
|
|
|
public function __construct(App $app) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
$this->app = $app; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* 设置是否使用路由 |
62
|
|
|
* @access public |
63
|
|
|
* @param bool $route |
|
|
|
|
64
|
|
|
* @return $this |
65
|
|
|
*/ |
66
|
|
|
public function withRoute(bool $route) |
67
|
|
|
{ |
68
|
|
|
$this->withRoute = $route; |
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 设置控制器层名称 |
74
|
|
|
* @access public |
75
|
|
|
* @param string $layer 控制器层名称 |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function controllerLayer(string $layer) |
79
|
|
|
{ |
80
|
|
|
$this->controllerLayer = $layer; |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* 设置空控制器名称 |
86
|
|
|
* @access public |
87
|
|
|
* @param string $empty 空控制器名称 |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function emptyController(string $empty) |
91
|
|
|
{ |
92
|
|
|
$this->emptyController = $empty; |
93
|
|
|
return $this; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* 设置是否启用控制器类库后缀 |
98
|
|
|
* @access public |
99
|
|
|
* @param bool $suffix 启用控制器类库后缀 |
100
|
|
|
* @return $this |
101
|
|
|
*/ |
102
|
|
|
public function controllerSuffix(bool $suffix = true) |
103
|
|
|
{ |
104
|
|
|
$this->controllerSuffix = $suffix; |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* 是否启用控制器类库后缀 |
110
|
|
|
* @access public |
111
|
|
|
* @return bool |
112
|
|
|
*/ |
113
|
|
|
public function hasControllerSuffix(): bool |
114
|
|
|
{ |
115
|
|
|
return $this->controllerSuffix; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* 获取控制器层名称 |
120
|
|
|
* @access public |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
|
|
public function getControllerLayer(): string |
124
|
|
|
{ |
125
|
|
|
return $this->controllerLayer; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* 执行应用程序 |
130
|
|
|
* @access public |
131
|
|
|
* @return Response |
132
|
|
|
*/ |
133
|
|
|
public function run(): Response |
134
|
|
|
{ |
135
|
|
|
try { |
136
|
|
|
if ($this->withRoute) { |
137
|
|
|
$dispatch = $this->routeCheck()->init(); |
138
|
|
|
} else { |
139
|
|
|
$dispatch = $this->app->route->url($this->getRealPath())->init(); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
// 监听AppBegin |
143
|
|
|
$this->app->event->trigger('AppBegin'); |
144
|
|
|
|
145
|
|
|
$data = null; |
146
|
|
|
} catch (HttpResponseException $exception) { |
147
|
|
|
$dispatch = null; |
148
|
|
|
$data = $exception->getResponse(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$this->app->middleware->add(function (Request $request, $next) use ($dispatch, $data) { |
|
|
|
|
152
|
|
|
return is_null($data) ? $dispatch->run() : $data; |
153
|
|
|
}); |
|
|
|
|
154
|
|
|
|
155
|
|
|
$response = $this->app->middleware->dispatch($this->app->request); |
156
|
|
|
|
157
|
|
|
// 监听AppEnd |
158
|
|
|
$this->app->event->trigger('AppEnd', $response); |
159
|
|
|
|
160
|
|
|
return $response; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* 实例化访问控制器 |
165
|
|
|
* @access public |
166
|
|
|
* @param string $name 资源地址 |
167
|
|
|
* @return object |
168
|
|
|
* @throws ClassNotFoundException |
169
|
|
|
*/ |
170
|
|
|
public function controller(string $name) |
171
|
|
|
{ |
172
|
|
|
$suffix = $this->controllerSuffix ? 'Controller' : ''; |
173
|
|
|
$class = $this->app->parseClass($this->controllerLayer, $name . $suffix); |
174
|
|
|
|
175
|
|
|
if (class_exists($class)) { |
176
|
|
|
return $this->app->make($class, [], true); |
177
|
|
|
} elseif ($this->emptyController && class_exists($emptyClass = $this->app->parseClass($this->controllerLayer, $this->emptyController . $suffix))) { |
178
|
|
|
return $this->app->make($emptyClass, [], true); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
throw new ClassNotFoundException('class not exists:' . $class, $class); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* 路由初始化(路由规则注册) |
186
|
|
|
* @access protected |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
protected function routeInit(): void |
190
|
|
|
{ |
191
|
|
|
// 加载路由定义 |
192
|
|
|
if (is_dir($this->getRoutePath())) { |
193
|
|
|
$files = glob($this->getRoutePath() . DIRECTORY_SEPARATOR . '*.php'); |
194
|
|
|
foreach ($files as $file) { |
195
|
|
|
include $file; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if ($this->app->route->config('route_annotation')) { |
200
|
|
|
// 自动生成注解路由定义 |
201
|
|
|
if ($this->app->isDebug()) { |
202
|
|
|
$this->app->build->buildRoute(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
$filename = $this->app->getRuntimePath() . 'build_route.php'; |
206
|
|
|
|
207
|
|
|
if (is_file($filename)) { |
208
|
|
|
include $filename; |
209
|
|
|
} |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
/** |
214
|
|
|
* URL路由检测(根据PATH_INFO) |
215
|
|
|
* @access protected |
216
|
|
|
* @return Dispatch |
217
|
|
|
*/ |
218
|
|
|
protected function routeCheck(): Dispatch |
219
|
|
|
{ |
220
|
|
|
// 检测路由缓存 |
221
|
|
|
if (!$this->app->isDebug() && $this->app->route->config('route_check_cache')) { |
222
|
|
|
$routeKey = $this->getRouteCacheKey(); |
223
|
|
|
$option = $this->app->route->config('route_cache_option'); |
224
|
|
|
|
225
|
|
|
if ($option && $this->app->cache->connect($option)->has($routeKey)) { |
226
|
|
|
return $this->app->cache->connect($option)->get($routeKey); |
227
|
|
|
} elseif ($this->app->cache->has($routeKey)) { |
228
|
|
|
return $this->app->cache->get($routeKey); |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
$this->routeInit(); |
233
|
|
|
|
234
|
|
|
// 路由检测 |
235
|
|
|
$dispatch = $this->app->route->check($this->getRealPath()); |
236
|
|
|
|
237
|
|
|
if (!empty($routeKey)) { |
238
|
|
|
try { |
239
|
|
|
if (!empty($option)) { |
240
|
|
|
$this->app->cache->connect($option)->tag('route_cache')->set($routeKey, $dispatch); |
241
|
|
|
} else { |
242
|
|
|
$this->app->cache->tag('route_cache')->set($routeKey, $dispatch); |
243
|
|
|
} |
244
|
|
|
} catch (\Exception $e) { |
245
|
|
|
// 存在闭包的时候缓存无效 |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $dispatch; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* 获取路由缓存Key |
254
|
|
|
* @access protected |
255
|
|
|
* @return string |
256
|
|
|
*/ |
257
|
|
|
protected function getRouteCacheKey(): string |
258
|
|
|
{ |
259
|
|
|
if ($this->app->route->config('route_check_cache_key')) { |
260
|
|
|
$closure = $this->app->route->config('route_check_cache_key'); |
261
|
|
|
$routeKey = $closure($this->app->request); |
262
|
|
|
} else { |
263
|
|
|
$routeKey = md5($this->app->request->baseUrl(true) . ':' . $this->app->request->method()); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
return $routeKey; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* 获取自动多应用模式下的实际URL Path |
272
|
|
|
* @access public |
273
|
|
|
* @return string |
274
|
|
|
*/ |
275
|
|
|
protected function getRealPath(): string |
276
|
|
|
{ |
277
|
|
|
$path = $this->app->request->path(); |
278
|
|
|
|
279
|
|
|
if ($path && $this->app->isAutoMulti()) { |
280
|
|
|
$path = substr_replace($path, '', 0, strpos($path, '/') ? strpos($path, '/') + 1 : strlen($path)); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
return $path; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* 获取路由目录 |
289
|
|
|
* @access public |
290
|
|
|
* @return string |
291
|
|
|
*/ |
292
|
|
|
public function getRoutePath(): string |
293
|
|
|
{ |
294
|
|
|
if ($this->app->isMulti()) { |
295
|
|
|
return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . $this->app->getName() . DIRECTORY_SEPARATOR; |
296
|
|
|
} |
297
|
|
|
return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
|
301
|
|
|
} |
302
|
|
|
|