Completed
Push — 6.0 ( 677f21...00c571 )
by liu
05:27
created

Http::renderException()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 2
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 3
    public function __construct(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
34
    {
35 3
        $this->app = $app;
36 3
    }
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
    }
59
60
    /**
61
     * 设置应用目录
62
     * @access public
63
     * @param string $path 应用目录
64
     * @return $this
65
     */
66
    public function path(string $path)
67
    {
68
        $this->app->path($path);
69
        return $this;
70
    }
71
72
    /**
73
     * 执行应用程序
74
     * @access public
75
     * @param Request|null $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
76
     * @return Response
77
     */
78 2
    public function run(Request $request = null): Response
79
    {
80
        //自动创建request对象
81 2
        $request = $request ?? $this->app->make('request', [], true);
82 2
        $this->app->instance('request', $request);
83
84
        try {
85 2
            $response = $this->runWithRequest($request);
86 1
        } catch (Throwable $e) {
87 1
            $this->reportException($e);
88
89 1
            $response = $this->renderException($request, $e);
90
        }
91
92 2
        return $response->setCookie($this->app->cookie);
93
    }
94
95
    /**
96
     * 初始化
97
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
98 1
    protected function initialize()
99
    {
100 1
        if (!$this->app->initialized()) {
101 1
            $this->app->initialize();
102
        }
103 1
    }
104
105
    /**
106
     * 执行应用程序
107
     * @param Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
108
     * @return mixed
109
     */
110 1
    protected function runWithRequest(Request $request)
111
    {
112 1
        $this->initialize();
113
114
        // 加载全局中间件
115 1
        $this->loadMiddleware();
116
117
        // 设置开启事件机制
118 1
        $this->app->event->withEvent($this->app->config->get('app.with_event', true));
0 ignored issues
show
Bug introduced by
It seems like $this->app->config->get('app.with_event', true) can also be of type array; however, parameter $event of think\Event::withEvent() does only seem to accept boolean, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

118
        $this->app->event->withEvent(/** @scrutinizer ignore-type */ $this->app->config->get('app.with_event', true));
Loading history...
119
120
        // 监听HttpRun
121 1
        $this->app->event->trigger(HttpRun::class);
122
123 1
        return $this->app->middleware->pipeline()
124 1
            ->send($request)
125
            ->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...
126 1
                return $this->dispatchToRoute($request);
127 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...
128
    }
129
130 1
    protected function dispatchToRoute($request)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function dispatchToRoute()
Loading history...
131
    {
132
        $withRoute = $this->app->config->get('app.with_route', true) ? function () {
133 1
            $this->loadRoutes();
134 1
        } : null;
135
136 1
        return $this->app->route->dispatch($request, $withRoute);
137
    }
138
139
    /**
140
     * 加载全局中间件
141
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
142 1
    protected function loadMiddleware(): void
143
    {
144 1
        if (is_file($this->app->getBasePath() . 'middleware.php')) {
145 1
            $this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php');
146
        }
147 1
    }
148
149
    /**
150
     * 加载路由
151
     * @access protected
152
     * @return void
153
     */
154 1
    protected function loadRoutes(): void
155
    {
156
        // 加载路由定义
157 1
        $routePath = $this->app->getRoutePath();
158
159 1
        if (is_dir($routePath)) {
160
            $files = glob($routePath . '*.php');
161
            foreach ($files as $file) {
162
                include $file;
163
            }
164
        }
165
166 1
        $this->app->event->trigger(RouteLoaded::class);
167 1
    }
168
169
    /**
170
     * Report the exception to the exception handler.
171
     *
172
     * @param Throwable $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
173
     * @return void
174
     */
175 1
    protected function reportException(Throwable $e)
176
    {
177 1
        $this->app->make(Handle::class)->report($e);
178 1
    }
179
180
    /**
181
     * Render the exception to a response.
182
     *
183
     * @param Request   $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
184
     * @param Throwable $e
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
185
     * @return Response
186
     */
187 1
    protected function renderException($request, Throwable $e)
188
    {
189 1
        return $this->app->make(Handle::class)->render($request, $e);
190
    }
191
192
    /**
193
     * HttpEnd
194
     * @param Response $response
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
195
     * @return void
196
     */
197 1
    public function end(Response $response): void
198
    {
199 1
        $this->app->event->trigger(HttpEnd::class, $response);
200
201
        //执行中间件
202 1
        $this->app->middleware->end($response);
203
204
        // 写入日志
205 1
        $this->app->log->save();
206 1
    }
207
208
}
209