Total Complexity | 60 |
Total Lines | 373 |
Duplicated Lines | 0 % |
Coverage | 92.06% |
Changes | 0 |
Complex classes like Http often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Http, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class Http |
||
24 | { |
||
25 | |||
26 | /** |
||
27 | * @var App |
||
28 | */ |
||
29 | protected $app; |
||
30 | |||
31 | /** |
||
32 | * 应用路径 |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $path; |
||
36 | |||
37 | /** |
||
38 | * 是否多应用模式 |
||
39 | * @var bool |
||
40 | */ |
||
41 | protected $multi = false; |
||
42 | |||
43 | /** |
||
44 | * 是否域名绑定应用 |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $bindDomain = false; |
||
48 | |||
49 | /** |
||
50 | * 应用名称 |
||
51 | * @var string |
||
52 | */ |
||
53 | protected $name; |
||
54 | |||
55 | 9 | public function __construct(App $app) |
|
56 | { |
||
57 | 9 | $this->app = $app; |
|
58 | 9 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
|
59 | 9 | } |
|
60 | |||
61 | /** |
||
62 | * 是否域名绑定应用 |
||
63 | * @access public |
||
64 | * @return bool |
||
65 | */ |
||
66 | 2 | public function isBindDomain(): bool |
|
67 | { |
||
68 | 2 | return $this->bindDomain; |
|
69 | } |
||
70 | |||
71 | /** |
||
72 | * 设置应用模式 |
||
73 | * @access public |
||
74 | * @param bool $multi |
||
1 ignored issue
–
show
|
|||
75 | * @return $this |
||
76 | */ |
||
77 | 1 | public function multi(bool $multi) |
|
78 | { |
||
79 | 1 | $this->multi = $multi; |
|
80 | 1 | return $this; |
|
81 | } |
||
82 | |||
83 | /** |
||
84 | * 是否为多应用模式 |
||
85 | * @access public |
||
86 | * @return bool |
||
87 | */ |
||
88 | 7 | public function isMulti(): bool |
|
89 | { |
||
90 | 7 | return $this->multi; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * 设置应用名称 |
||
95 | * @access public |
||
96 | * @param string $name 应用名称 |
||
1 ignored issue
–
show
|
|||
97 | * @return $this |
||
98 | */ |
||
99 | 1 | public function name(string $name) |
|
100 | { |
||
101 | 1 | $this->name = $name; |
|
102 | 1 | return $this; |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * 获取应用名称 |
||
107 | * @access public |
||
108 | * @return string |
||
109 | */ |
||
110 | 5 | public function getName(): string |
|
111 | { |
||
112 | 5 | return $this->name ?: ''; |
|
113 | } |
||
114 | |||
115 | /** |
||
116 | * 设置应用目录 |
||
117 | * @access public |
||
118 | * @param string $path 应用目录 |
||
1 ignored issue
–
show
|
|||
119 | * @return $this |
||
120 | */ |
||
121 | 1 | public function path(string $path) |
|
122 | { |
||
123 | 1 | $this->path = $path; |
|
124 | 1 | return $this; |
|
125 | } |
||
126 | |||
127 | /** |
||
128 | * 执行应用程序 |
||
129 | * @access public |
||
130 | * @param Request|null $request |
||
1 ignored issue
–
show
|
|||
131 | * @return Response |
||
132 | */ |
||
133 | 8 | public function run(Request $request = null): Response |
|
134 | { |
||
135 | //自动创建request对象 |
||
136 | 8 | $request = $request ?? $this->app->make('request', [], true); |
|
137 | 8 | $this->app->instance('request', $request); |
|
138 | |||
139 | try { |
||
140 | 8 | $response = $this->runWithRequest($request); |
|
141 | 2 | } catch (Throwable $e) { |
|
142 | 2 | $this->reportException($e); |
|
143 | |||
144 | 2 | $response = $this->renderException($request, $e); |
|
145 | } |
||
146 | |||
147 | 8 | return $response->setCookie($this->app->cookie); |
|
148 | } |
||
149 | |||
150 | /** |
||
151 | * 初始化 |
||
152 | */ |
||
153 | 7 | protected function initialize() |
|
154 | { |
||
155 | 7 | if (!$this->app->initialized()) { |
|
156 | 7 | $this->app->initialize(); |
|
157 | } |
||
158 | 7 | } |
|
159 | |||
160 | /** |
||
161 | * 执行应用程序 |
||
162 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
163 | * @return mixed |
||
164 | */ |
||
165 | 7 | protected function runWithRequest(Request $request) |
|
166 | { |
||
167 | 7 | $this->initialize(); |
|
168 | |||
169 | // 加载全局中间件 |
||
170 | 7 | if (is_file($this->app->getBasePath() . 'middleware.php')) { |
|
171 | 7 | $this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php'); |
|
172 | } |
||
173 | |||
174 | 7 | if ($this->multi) { |
|
175 | 6 | $this->parseMultiApp(); |
|
176 | } |
||
177 | |||
178 | // 设置开启事件机制 |
||
179 | 6 | $this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
|
180 | |||
181 | // 监听HttpRun |
||
182 | 6 | $this->app->event->trigger('HttpRun'); |
|
183 | |||
184 | $withRoute = $this->app->config->get('app.with_route', true) ? function () { |
||
185 | 6 | $this->loadRoutes(); |
|
186 | 6 | } : null; |
|
187 | |||
188 | 6 | return $this->app->route->dispatch($request, $withRoute); |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * 加载路由 |
||
193 | * @access protected |
||
194 | * @return void |
||
195 | */ |
||
196 | 6 | protected function loadRoutes(): void |
|
216 | } |
||
217 | } |
||
218 | 6 | } |
|
219 | |||
220 | /** |
||
221 | * 获取路由目录 |
||
222 | * @access protected |
||
223 | * @return string |
||
224 | */ |
||
225 | 6 | protected function getRoutePath(): string |
|
226 | { |
||
227 | 6 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
|
228 | } |
||
229 | |||
230 | /** |
||
231 | * Report the exception to the exception handler. |
||
232 | * |
||
233 | * @param Throwable $e |
||
1 ignored issue
–
show
|
|||
234 | * @return void |
||
235 | */ |
||
236 | 1 | protected function reportException(Throwable $e) |
|
237 | { |
||
238 | 1 | $this->app->make(Handle::class)->report($e); |
|
239 | 1 | } |
|
240 | |||
241 | /** |
||
242 | * Render the exception to a response. |
||
243 | * |
||
244 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
245 | * @param Throwable $e |
||
1 ignored issue
–
show
|
|||
246 | * @return Response |
||
247 | */ |
||
248 | 1 | protected function renderException($request, Throwable $e) |
|
251 | } |
||
252 | |||
253 | /** |
||
254 | * 获取当前运行入口名称 |
||
255 | * @access protected |
||
1 ignored issue
–
show
|
|||
256 | * @codeCoverageIgnore |
||
257 | * @return string |
||
1 ignored issue
–
show
|
|||
258 | */ |
||
259 | protected function getScriptName(): string |
||
260 | { |
||
268 | } |
||
269 | |||
270 | /** |
||
271 | * 解析多应用 |
||
272 | */ |
||
273 | 6 | protected function parseMultiApp(): void |
|
326 | 5 | } |
|
327 | |||
328 | /** |
||
329 | * 加载应用文件 |
||
330 | * @param string $appName 应用名 |
||
1 ignored issue
–
show
|
|||
331 | * @return void |
||
332 | */ |
||
333 | 5 | protected function loadApp(string $appName): void |
|
381 | 5 | } |
|
382 | |||
383 | /** |
||
384 | * HttpEnd |
||
385 | * @param Response $response |
||
1 ignored issue
–
show
|
|||
386 | * @return void |
||
387 | */ |
||
388 | 1 | public function end(Response $response): void |
|
398 |