|
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
|
|
|
public function __construct(App $app) |
|
|
|
|
|
|
34
|
|
|
{ |
|
35
|
|
|
$this->app = $app; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* 设置应用名称 |
|
40
|
|
|
* @access public |
|
41
|
|
|
* @param string $name 应用名称 |
|
42
|
|
|
* @return $this |
|
43
|
|
|
*/ |
|
44
|
|
|
public function name(string $name) |
|
45
|
|
|
{ |
|
46
|
|
|
$this->app->name($name); |
|
47
|
|
|
return $this; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* 获取应用名称 |
|
52
|
|
|
* @access public |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function getName(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->app->getName(); |
|
58
|
9 |
|
} |
|
59
|
|
|
|
|
60
|
9 |
|
/** |
|
61
|
9 |
|
* 设置应用目录 |
|
62
|
9 |
|
* @access public |
|
63
|
|
|
* @param string $path 应用目录 |
|
64
|
|
|
* @return $this |
|
65
|
|
|
*/ |
|
66
|
|
|
public function path(string $path) |
|
67
|
|
|
{ |
|
68
|
|
|
$this->app->path($path); |
|
69
|
2 |
|
return $this; |
|
70
|
|
|
} |
|
71
|
2 |
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* 执行应用程序 |
|
74
|
|
|
* @access public |
|
75
|
|
|
* @param Request|null $request |
|
|
|
|
|
|
76
|
|
|
* @return Response |
|
77
|
|
|
*/ |
|
78
|
|
|
public function run(Request $request = null): Response |
|
79
|
|
|
{ |
|
80
|
7 |
|
//自动创建request对象 |
|
81
|
|
|
$request = $request ?? $this->app->make('request', [], true); |
|
82
|
7 |
|
$this->app->instance('request', $request); |
|
83
|
7 |
|
|
|
84
|
|
|
try { |
|
85
|
|
|
$response = $this->runWithRequest($request); |
|
86
|
|
|
} catch (Throwable $e) { |
|
87
|
|
|
$this->reportException($e); |
|
88
|
|
|
|
|
89
|
|
|
$response = $this->renderException($request, $e); |
|
90
|
|
|
} |
|
91
|
7 |
|
|
|
92
|
|
|
return $response->setCookie($this->app->cookie); |
|
93
|
7 |
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* 初始化 |
|
97
|
|
|
*/ |
|
|
|
|
|
|
98
|
|
|
protected function initialize() |
|
99
|
|
|
{ |
|
100
|
|
|
if (!$this->app->initialized()) { |
|
101
|
|
|
$this->app->initialize(); |
|
102
|
1 |
|
} |
|
103
|
|
|
} |
|
104
|
1 |
|
|
|
105
|
1 |
|
/** |
|
106
|
|
|
* 执行应用程序 |
|
107
|
|
|
* @param Request $request |
|
|
|
|
|
|
108
|
|
|
* @return mixed |
|
109
|
|
|
*/ |
|
110
|
|
|
protected function runWithRequest(Request $request) |
|
111
|
|
|
{ |
|
112
|
|
|
$this->initialize(); |
|
113
|
5 |
|
|
|
114
|
|
|
// 加载全局中间件 |
|
115
|
5 |
|
$this->loadMiddleware(); |
|
116
|
|
|
|
|
117
|
|
|
// 设置开启事件机制 |
|
118
|
|
|
$this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
|
|
|
|
|
|
119
|
|
|
|
|
120
|
|
|
// 监听HttpRun |
|
121
|
|
|
$this->app->event->trigger(HttpRun::class); |
|
122
|
|
|
|
|
123
|
|
|
return $this->app->middleware->pipeline() |
|
124
|
1 |
|
->send($request) |
|
125
|
|
|
->then(function ($request) { |
|
|
|
|
|
|
126
|
1 |
|
return $this->dispatchToRoute($request); |
|
127
|
1 |
|
}); |
|
|
|
|
|
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
1 |
|
protected function dispatchToRoute($request) |
|
|
|
|
|
|
131
|
1 |
|
{ |
|
132
|
|
|
$withRoute = $this->app->config->get('app.with_route', true) ? function () { |
|
133
|
|
|
$this->loadRoutes(); |
|
134
|
|
|
} : null; |
|
135
|
|
|
|
|
136
|
|
|
return $this->app->route->dispatch($request, $withRoute); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
8 |
|
* 加载全局中间件 |
|
141
|
|
|
*/ |
|
|
|
|
|
|
142
|
|
|
protected function loadMiddleware(): void |
|
143
|
8 |
|
{ |
|
144
|
8 |
|
if (is_file($this->app->getBasePath() . 'middleware.php')) { |
|
145
|
|
|
$this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php'); |
|
146
|
|
|
} |
|
147
|
8 |
|
} |
|
148
|
2 |
|
|
|
149
|
2 |
|
/** |
|
150
|
|
|
* 加载路由 |
|
151
|
2 |
|
* @access protected |
|
152
|
|
|
* @return void |
|
153
|
|
|
*/ |
|
154
|
8 |
|
protected function loadRoutes(): void |
|
155
|
|
|
{ |
|
156
|
|
|
// 加载路由定义 |
|
157
|
|
|
$routePath = $this->app->getRoutePath(); |
|
158
|
|
|
|
|
159
|
|
|
if (is_dir($routePath)) { |
|
160
|
7 |
|
$files = glob($routePath . '*.php'); |
|
161
|
|
|
foreach ($files as $file) { |
|
162
|
7 |
|
include $file; |
|
163
|
7 |
|
} |
|
164
|
|
|
} |
|
165
|
7 |
|
|
|
166
|
|
|
$this->app->event->trigger(RouteLoaded::class); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Report the exception to the exception handler. |
|
171
|
|
|
* |
|
172
|
7 |
|
* @param Throwable $e |
|
|
|
|
|
|
173
|
|
|
* @return void |
|
174
|
7 |
|
*/ |
|
175
|
|
|
protected function reportException(Throwable $e) |
|
176
|
|
|
{ |
|
177
|
7 |
|
$this->app->make(Handle::class)->report($e); |
|
178
|
|
|
} |
|
179
|
7 |
|
|
|
180
|
|
|
/** |
|
181
|
7 |
|
* Render the exception to a response. |
|
182
|
6 |
|
* |
|
183
|
6 |
|
* @param Request $request |
|
|
|
|
|
|
184
|
|
|
* @param Throwable $e |
|
|
|
|
|
|
185
|
|
|
* @return Response |
|
186
|
|
|
*/ |
|
187
|
6 |
|
protected function renderException($request, Throwable $e) |
|
188
|
|
|
{ |
|
189
|
|
|
return $this->app->make(Handle::class)->render($request, $e); |
|
190
|
6 |
|
} |
|
191
|
|
|
|
|
192
|
6 |
|
/** |
|
193
|
6 |
|
* HttpEnd |
|
194
|
|
|
* @param Response $response |
|
|
|
|
|
|
195
|
6 |
|
* @return void |
|
196
|
6 |
|
*/ |
|
197
|
|
|
public function end(Response $response): void |
|
198
|
|
|
{ |
|
199
|
6 |
|
$this->app->event->trigger(HttpEnd::class, $response); |
|
200
|
|
|
|
|
201
|
|
|
//执行中间件 |
|
202
|
6 |
|
$this->app->middleware->end($response); |
|
203
|
6 |
|
|
|
204
|
|
|
// 写入日志 |
|
205
|
6 |
|
$this->app->log->save(); |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
} |
|
209
|
|
|
|