Passed
Pull Request — 6.0 (#2238)
by yun
06:03
created

Http::dispatchToRoute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 4
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 2
rs 10
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
0 ignored issues
show
Coding Style introduced by
Package name "think" is not valid; consider "Think" instead
Loading history...
24
 */
25
class Http
26
{
27
28
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
52
    {
53 3
        $this->app = $app;
54
55 3
        $this->routePath = $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR;
0 ignored issues
show
Bug Best Practice introduced by
The property routePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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;
0 ignored issues
show
Bug Best Practice introduced by
The property routePath does not exist. Although not strictly required by PHP, it is generally a best practice to declare properties explicitly.
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
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;
170
    }
171
172
    /**
173
     * 初始化
174
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
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
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
185
     * @return mixed
186
     */
187 1
    protected function runWithRequest(Request $request)
188
    {
189 1
        $this->initialize();
190
191
        // 加载全局中间件
192 1
        $this->loadMiddleware();
193
194
        // 监听HttpRun
195 1
        $this->app->event->trigger(HttpRun::class);
196
197 1
        return $this->app->middleware->pipeline()
198 1
            ->send($request)
199
            ->then(function ($request) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
200 1
                return $this->dispatchToRoute($request);
201 1
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
202
    }
203
204 1
    protected function dispatchToRoute($request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function dispatchToRoute()
Loading history...
205
    {
206
        $withRoute = $this->app->config->get('app.with_route', true) ? function () {
207 1
            $this->loadRoutes();
208 1
        }
209 1
        : null;
210
211 1
        return $this->app->route->dispatch($request, $withRoute);
212
    }
213
214
    /**
215
     * 加载全局中间件
216
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
217 1
    protected function loadMiddleware(): void
218
    {
219 1
        if (is_file($this->app->getBasePath() . 'middleware.php')) {
220 1
            $this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php');
221
        }
222 1
    }
223
224
    /**
225
     * 加载路由
226
     * @access protected
227
     * @return void
228
     */
229 1
    protected function loadRoutes(): void
230
    {
231
        // 加载路由定义
232 1
        $routePath = $this->getRoutePath();
233
234 1
        if (is_dir($routePath)) {
235
            $files = glob($routePath . '*.php');
236
            foreach ($files as $file) {
237
                include $file;
238
            }
239
        }
240
241 1
        $this->app->event->trigger(RouteLoaded::class);
242 1
    }
243
244
    /**
245
     * Report the exception to the exception handler.
246
     *
247
     * @param Throwable $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
248
     * @return void
249
     */
250 1
    protected function reportException(Throwable $e)
251
    {
252 1
        $this->app->make(Handle::class)->report($e);
253 1
    }
254
255
    /**
256
     * Render the exception to a response.
257
     *
258
     * @param Request   $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
259
     * @param Throwable $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
260
     * @return Response
261
     */
262 1
    protected function renderException($request, Throwable $e)
263
    {
264 1
        return $this->app->make(Handle::class)->render($request, $e);
265
    }
266
267
    /**
268
     * HttpEnd
269
     * @param Response $response
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
270
     * @return void
271
     */
272 1
    public function end(Response $response): void
273
    {
274 1
        $this->app->event->trigger(HttpEnd::class, $response);
275
276
        //执行中间件
277 1
        $this->app->middleware->end($response);
278
279
        // 写入日志
280 1
        $this->app->log->save();
281 1
    }
282
}
283