Total Complexity | 57 |
Total Lines | 365 |
Duplicated Lines | 0 % |
Coverage | 95% |
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 |
||
22 | class Http |
||
23 | { |
||
24 | |||
25 | /** |
||
26 | * @var App |
||
27 | */ |
||
28 | protected $app; |
||
29 | |||
30 | /** |
||
31 | * 应用路径 |
||
32 | * @var string |
||
33 | */ |
||
34 | protected $path; |
||
35 | |||
36 | /** |
||
37 | * 是否多应用模式 |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $multi = false; |
||
41 | |||
42 | /** |
||
43 | * 是否域名绑定应用 |
||
44 | * @var bool |
||
45 | */ |
||
46 | protected $bindDomain = false; |
||
47 | |||
48 | /** |
||
49 | * 应用名称 |
||
50 | * @var string |
||
51 | */ |
||
52 | protected $name; |
||
53 | |||
54 | 9 | public function __construct(App $app) |
|
55 | { |
||
56 | 9 | $this->app = $app; |
|
57 | 9 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
|
58 | 9 | } |
|
59 | |||
60 | /** |
||
61 | * 是否域名绑定应用 |
||
62 | * @access public |
||
63 | * @return bool |
||
64 | */ |
||
65 | 2 | public function isBindDomain(): bool |
|
66 | { |
||
67 | 2 | return $this->bindDomain; |
|
68 | } |
||
69 | |||
70 | /** |
||
71 | * 设置应用模式 |
||
72 | * @access public |
||
73 | * @param bool $multi |
||
1 ignored issue
–
show
|
|||
74 | * @return $this |
||
75 | */ |
||
76 | 1 | public function multi(bool $multi) |
|
77 | { |
||
78 | 1 | $this->multi = $multi; |
|
79 | 1 | return $this; |
|
80 | } |
||
81 | |||
82 | /** |
||
83 | * 是否为多应用模式 |
||
84 | * @access public |
||
85 | * @return bool |
||
86 | */ |
||
87 | 7 | public function isMulti(): bool |
|
88 | { |
||
89 | 7 | return $this->multi; |
|
90 | } |
||
91 | |||
92 | /** |
||
93 | * 设置应用名称 |
||
94 | * @access public |
||
95 | * @param string $name 应用名称 |
||
1 ignored issue
–
show
|
|||
96 | * @return $this |
||
97 | */ |
||
98 | 1 | public function name(string $name) |
|
99 | { |
||
100 | 1 | $this->name = $name; |
|
101 | 1 | return $this; |
|
102 | } |
||
103 | |||
104 | /** |
||
105 | * 获取应用名称 |
||
106 | * @access public |
||
107 | * @return string |
||
108 | */ |
||
109 | 5 | public function getName(): string |
|
110 | { |
||
111 | 5 | return $this->name ?: ''; |
|
112 | } |
||
113 | |||
114 | /** |
||
115 | * 设置应用目录 |
||
116 | * @access public |
||
117 | * @param string $path 应用目录 |
||
1 ignored issue
–
show
|
|||
118 | * @return $this |
||
119 | */ |
||
120 | 1 | public function path(string $path) |
|
121 | { |
||
122 | 1 | $this->path = $path; |
|
123 | 1 | return $this; |
|
124 | } |
||
125 | |||
126 | /** |
||
127 | * 执行应用程序 |
||
128 | * @access public |
||
129 | * @param Request|null $request |
||
1 ignored issue
–
show
|
|||
130 | * @return Response |
||
131 | */ |
||
132 | 8 | public function run(Request $request = null): Response |
|
133 | { |
||
134 | //自动创建request对象 |
||
135 | 8 | $request = $request ?? $this->app->make('request', [], true); |
|
136 | 8 | $this->app->instance('request', $request); |
|
137 | |||
138 | try { |
||
139 | 8 | $response = $this->runWithRequest($request); |
|
140 | 2 | } catch (Throwable $e) { |
|
141 | 2 | $this->reportException($e); |
|
142 | |||
143 | 2 | $response = $this->renderException($request, $e); |
|
144 | } |
||
145 | |||
146 | 8 | return $response->setCookie($this->app->cookie); |
|
147 | } |
||
148 | |||
149 | /** |
||
150 | * 初始化 |
||
151 | */ |
||
152 | 7 | protected function initialize() |
|
153 | { |
||
154 | 7 | if (!$this->app->initialized()) { |
|
155 | 7 | $this->app->initialize(); |
|
156 | } |
||
157 | 7 | } |
|
158 | |||
159 | /** |
||
160 | * 执行应用程序 |
||
161 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
162 | * @return mixed |
||
163 | */ |
||
164 | 7 | protected function runWithRequest(Request $request) |
|
165 | { |
||
166 | 7 | $this->initialize(); |
|
167 | |||
168 | // 加载全局中间件 |
||
169 | 7 | if (is_file($this->app->getBasePath() . 'middleware.php')) { |
|
170 | 7 | $this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php'); |
|
171 | } |
||
172 | |||
173 | 7 | if ($this->multi) { |
|
174 | 6 | $this->parseMultiApp(); |
|
175 | } |
||
176 | |||
177 | // 设置开启事件机制 |
||
178 | 6 | $this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
|
179 | |||
180 | // 监听HttpRun |
||
181 | 6 | $this->app->event->trigger('HttpRun'); |
|
182 | |||
183 | $withRoute = $this->app->config->get('app.with_route', true) ? function () { |
||
184 | 6 | $this->loadRoutes(); |
|
185 | 6 | } : null; |
|
186 | |||
187 | 6 | return $this->app->route->dispatch($request, $withRoute); |
|
188 | } |
||
189 | |||
190 | /** |
||
191 | * 加载路由 |
||
192 | * @access protected |
||
193 | * @return void |
||
194 | */ |
||
195 | 6 | protected function loadRoutes(): void |
|
215 | } |
||
216 | } |
||
217 | 6 | } |
|
218 | |||
219 | /** |
||
220 | * 获取路由目录 |
||
221 | * @access protected |
||
222 | * @return string |
||
223 | */ |
||
224 | 6 | protected function getRoutePath(): string |
|
225 | { |
||
226 | 6 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
|
227 | } |
||
228 | |||
229 | /** |
||
230 | * Report the exception to the exception handler. |
||
231 | * |
||
232 | * @param Throwable $e |
||
1 ignored issue
–
show
|
|||
233 | * @return void |
||
234 | */ |
||
235 | 1 | protected function reportException(Throwable $e) |
|
236 | { |
||
237 | 1 | $this->app->make(Handle::class)->report($e); |
|
238 | 1 | } |
|
239 | |||
240 | /** |
||
241 | * Render the exception to a response. |
||
242 | * |
||
243 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
244 | * @param Throwable $e |
||
1 ignored issue
–
show
|
|||
245 | * @return Response |
||
246 | */ |
||
247 | 1 | protected function renderException($request, Throwable $e) |
|
250 | } |
||
251 | |||
252 | /** |
||
253 | * 获取当前运行入口名称 |
||
254 | * @access protected |
||
1 ignored issue
–
show
|
|||
255 | * @codeCoverageIgnore |
||
256 | * @return string |
||
1 ignored issue
–
show
|
|||
257 | */ |
||
258 | protected function getScriptName(): string |
||
259 | { |
||
260 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
261 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
262 | } elseif (isset($_SERVER['argv'][0])) { |
||
263 | $file = realpath($_SERVER['argv'][0]); |
||
264 | } |
||
265 | |||
266 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : ''; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * 解析多应用 |
||
271 | */ |
||
272 | 6 | protected function parseMultiApp(): void |
|
317 | 5 | } |
|
318 | |||
319 | /** |
||
320 | * 加载应用文件 |
||
321 | * @param string $appName 应用名 |
||
1 ignored issue
–
show
|
|||
322 | * @return void |
||
323 | */ |
||
324 | 5 | protected function loadApp(string $appName): void |
|
372 | 5 | } |
|
373 | |||
374 | /** |
||
375 | * HttpEnd |
||
376 | * @param Response $response |
||
1 ignored issue
–
show
|
|||
377 | * @return void |
||
378 | */ |
||
379 | 1 | public function end(Response $response): void |
|
389 |