|
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\event\HttpEnd; |
|
16
|
|
|
use think\event\HttpRun; |
|
17
|
|
|
use think\event\RouteLoaded; |
|
18
|
|
|
use think\exception\Handle; |
|
19
|
|
|
use Throwable; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Web应用管理类 |
|
23
|
|
|
* @package think |
|
|
|
|
|
|
24
|
|
|
*/ |
|
25
|
|
|
class Http |
|
26
|
|
|
{ |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
|
|
|
|
|
29
|
|
|
* @var App |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $app; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* 应用名称 |
|
35
|
|
|
* @var string |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $name; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* 应用路径 |
|
41
|
|
|
* @var string |
|
42
|
|
|
*/ |
|
43
|
|
|
protected $path; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* 是否绑定应用 |
|
47
|
|
|
* @var bool |
|
48
|
|
|
*/ |
|
49
|
|
|
protected $isBind = false; |
|
50
|
|
|
|
|
51
|
3 |
|
public function __construct(App $app) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
3 |
|
$this->app = $app; |
|
54
|
|
|
|
|
55
|
3 |
|
$this->routePath = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR; |
|
|
|
|
|
|
56
|
3 |
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* 设置应用名称 |
|
60
|
|
|
* @access public |
|
61
|
|
|
* @param string $name 应用名称 |
|
62
|
|
|
* @return $this |
|
63
|
|
|
*/ |
|
64
|
|
|
public function name(string $name) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->name = $name; |
|
67
|
|
|
return $this; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* 获取应用名称 |
|
72
|
|
|
* @access public |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getName(): string |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->name ?: ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* 设置应用目录 |
|
82
|
|
|
* @access public |
|
83
|
|
|
* @param string $path 应用目录 |
|
84
|
|
|
* @return $this |
|
85
|
|
|
*/ |
|
86
|
|
|
public function path(string $path) |
|
87
|
|
|
{ |
|
88
|
|
|
if (substr($path, -1) != DIRECTORY_SEPARATOR) { |
|
89
|
|
|
$path .= DIRECTORY_SEPARATOR; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$this->path = $path; |
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* 获取应用路径 |
|
98
|
|
|
* @access public |
|
99
|
|
|
* @return string |
|
100
|
|
|
*/ |
|
101
|
|
|
public function getPath(): string |
|
102
|
|
|
{ |
|
103
|
|
|
return $this->path ?: ''; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* 获取路由目录 |
|
108
|
|
|
* @access public |
|
109
|
|
|
* @return string |
|
110
|
|
|
*/ |
|
111
|
1 |
|
public function getRoutePath(): string |
|
112
|
|
|
{ |
|
113
|
1 |
|
return $this->routePath; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* 设置路由目录 |
|
118
|
|
|
* @access public |
|
119
|
|
|
* @param string $path 路由定义目录 |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
public function setRoutePath(string $path): void |
|
123
|
|
|
{ |
|
124
|
|
|
$this->routePath = $path; |
|
|
|
|
|
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* 设置应用绑定 |
|
129
|
|
|
* @access public |
|
130
|
|
|
* @param bool $bind 是否绑定 |
|
131
|
|
|
* @return $this |
|
132
|
|
|
*/ |
|
133
|
|
|
public function setBind(bool $bind = true) |
|
134
|
|
|
{ |
|
135
|
|
|
$this->isBind = $bind; |
|
136
|
|
|
return $this; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* 是否绑定应用 |
|
141
|
|
|
* @access public |
|
142
|
|
|
* @return bool |
|
143
|
|
|
*/ |
|
144
|
|
|
public function isBind(): bool |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->isBind; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* 执行应用程序 |
|
151
|
|
|
* @access public |
|
152
|
|
|
* @param Request|null $request |
|
|
|
|
|
|
153
|
|
|
* @return Response |
|
154
|
|
|
*/ |
|
155
|
2 |
|
public function run(Request $request = null): Response |
|
156
|
|
|
{ |
|
157
|
|
|
//自动创建request对象 |
|
158
|
2 |
|
$request = $request ?? $this->app->make('request', [], true); |
|
159
|
2 |
|
$this->app->instance('request', $request); |
|
160
|
|
|
|
|
161
|
|
|
try { |
|
162
|
2 |
|
$response = $this->runWithRequest($request); |
|
163
|
1 |
|
} catch (Throwable $e) { |
|
164
|
1 |
|
$this->reportException($e); |
|
165
|
|
|
|
|
166
|
1 |
|
$response = $this->renderException($request, $e); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
2 |
|
return $response->setCookie($this->app->cookie); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* 初始化 |
|
174
|
|
|
*/ |
|
|
|
|
|
|
175
|
1 |
|
protected function initialize() |
|
176
|
|
|
{ |
|
177
|
1 |
|
if (!$this->app->initialized()) { |
|
178
|
1 |
|
$this->app->initialize(); |
|
179
|
|
|
} |
|
180
|
1 |
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* 执行应用程序 |
|
184
|
|
|
* @param Request $request |
|
|
|
|
|
|
185
|
|
|
* @return mixed |
|
186
|
|
|
*/ |
|
187
|
1 |
|
protected function runWithRequest(Request $request) |
|
188
|
|
|
{ |
|
189
|
1 |
|
$this->initialize(); |
|
190
|
|
|
|
|
191
|
|
|
// 加载全局中间件 |
|
192
|
1 |
|
$this->loadMiddleware(); |
|
193
|
|
|
|
|
194
|
|
|
// 设置开启事件机制 |
|
195
|
1 |
|
$this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
|
|
|
|
|
|
196
|
|
|
|
|
197
|
|
|
// 监听HttpRun |
|
198
|
1 |
|
$this->app->event->trigger(HttpRun::class); |
|
199
|
|
|
|
|
200
|
1 |
|
return $this->app->middleware->pipeline() |
|
201
|
1 |
|
->send($request) |
|
202
|
|
|
->then(function ($request) { |
|
|
|
|
|
|
203
|
1 |
|
return $this->dispatchToRoute($request); |
|
204
|
1 |
|
}); |
|
|
|
|
|
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
protected function dispatchToRoute($request) |
|
|
|
|
|
|
208
|
|
|
{ |
|
209
|
|
|
$withRoute = $this->app->config->get('app.with_route', true) ? function () { |
|
210
|
1 |
|
$this->loadRoutes(); |
|
211
|
1 |
|
} : null; |
|
212
|
|
|
|
|
213
|
1 |
|
return $this->app->route->dispatch($request, $withRoute); |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* 加载全局中间件 |
|
218
|
|
|
*/ |
|
|
|
|
|
|
219
|
1 |
|
protected function loadMiddleware(): void |
|
220
|
|
|
{ |
|
221
|
1 |
|
if (is_file($this->app->getBasePath() . 'middleware.php')) { |
|
222
|
1 |
|
$this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php'); |
|
223
|
|
|
} |
|
224
|
1 |
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* 加载路由 |
|
228
|
|
|
* @access protected |
|
229
|
|
|
* @return void |
|
230
|
|
|
*/ |
|
231
|
1 |
|
protected function loadRoutes(): void |
|
232
|
|
|
{ |
|
233
|
|
|
// 加载路由定义 |
|
234
|
1 |
|
$routePath = $this->getRoutePath(); |
|
235
|
|
|
|
|
236
|
1 |
|
if (is_dir($routePath)) { |
|
237
|
|
|
$files = glob($routePath . '*.php'); |
|
238
|
|
|
foreach ($files as $file) { |
|
239
|
|
|
include $file; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
1 |
|
$this->app->event->trigger(RouteLoaded::class); |
|
244
|
1 |
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* Report the exception to the exception handler. |
|
248
|
|
|
* |
|
249
|
|
|
* @param Throwable $e |
|
|
|
|
|
|
250
|
|
|
* @return void |
|
251
|
|
|
*/ |
|
252
|
1 |
|
protected function reportException(Throwable $e) |
|
253
|
|
|
{ |
|
254
|
1 |
|
$this->app->make(Handle::class)->report($e); |
|
255
|
1 |
|
} |
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* Render the exception to a response. |
|
259
|
|
|
* |
|
260
|
|
|
* @param Request $request |
|
|
|
|
|
|
261
|
|
|
* @param Throwable $e |
|
|
|
|
|
|
262
|
|
|
* @return Response |
|
263
|
|
|
*/ |
|
264
|
1 |
|
protected function renderException($request, Throwable $e) |
|
265
|
|
|
{ |
|
266
|
1 |
|
return $this->app->make(Handle::class)->render($request, $e); |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* HttpEnd |
|
271
|
|
|
* @param Response $response |
|
|
|
|
|
|
272
|
|
|
* @return void |
|
273
|
|
|
*/ |
|
274
|
1 |
|
public function end(Response $response): void |
|
275
|
|
|
{ |
|
276
|
1 |
|
$this->app->event->trigger(HttpEnd::class, $response); |
|
277
|
|
|
|
|
278
|
|
|
//执行中间件 |
|
279
|
1 |
|
$this->app->middleware->end($response); |
|
280
|
|
|
|
|
281
|
|
|
// 写入日志 |
|
282
|
1 |
|
$this->app->log->save(); |
|
283
|
1 |
|
} |
|
284
|
|
|
|
|
285
|
|
|
} |
|
286
|
|
|
|