Total Complexity | 59 |
Total Lines | 372 |
Duplicated Lines | 0 % |
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 |
||
21 | class Http |
||
22 | { |
||
23 | |||
24 | /** |
||
25 | * @var App |
||
26 | */ |
||
27 | protected $app; |
||
28 | |||
29 | /** |
||
30 | * 应用路径 |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $path; |
||
34 | |||
35 | /** |
||
36 | * 是否多应用模式 |
||
37 | * @var bool |
||
38 | */ |
||
39 | protected $multi = false; |
||
40 | |||
41 | /** |
||
42 | * 是否域名绑定应用 |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $bindDomain = false; |
||
46 | |||
47 | /** |
||
48 | * 应用名称 |
||
49 | * @var string |
||
50 | */ |
||
51 | protected $name; |
||
52 | |||
53 | public function __construct(App $app) |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * 是否域名绑定应用 |
||
61 | * @access public |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function isBindDomain(): bool |
||
65 | { |
||
66 | return $this->bindDomain; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * 设置应用模式 |
||
71 | * @access public |
||
72 | * @param bool $multi |
||
1 ignored issue
–
show
|
|||
73 | * @return $this |
||
74 | */ |
||
75 | public function multi(bool $multi) |
||
76 | { |
||
77 | $this->multi = $multi; |
||
78 | return $this; |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * 是否为多应用模式 |
||
83 | * @access public |
||
84 | * @return bool |
||
85 | */ |
||
86 | public function isMulti(): bool |
||
87 | { |
||
88 | return $this->multi; |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * 设置应用名称 |
||
93 | * @access public |
||
94 | * @param string $name 应用名称 |
||
1 ignored issue
–
show
|
|||
95 | * @return $this |
||
96 | */ |
||
97 | public function name(string $name) |
||
98 | { |
||
99 | $this->name = $name; |
||
100 | return $this; |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * 获取应用名称 |
||
105 | * @access public |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getName(): string |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * 设置应用目录 |
||
115 | * @access public |
||
116 | * @param string $path 应用目录 |
||
1 ignored issue
–
show
|
|||
117 | * @return $this |
||
118 | */ |
||
119 | public function path(string $path) |
||
120 | { |
||
121 | $this->path = $path; |
||
122 | return $this; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * 执行应用程序 |
||
127 | * @access public |
||
128 | * @param Request|null $request |
||
1 ignored issue
–
show
|
|||
129 | * @return Response |
||
130 | */ |
||
131 | public function run(Request $request = null): Response |
||
132 | { |
||
133 | //自动创建request对象 |
||
134 | $request = $request ?? $this->app->make('request', [], true); |
||
135 | $this->app->instance('request', $request); |
||
136 | |||
137 | try { |
||
138 | $response = $this->runWithRequest($request); |
||
139 | } catch (Throwable $e) { |
||
140 | $this->reportException($e); |
||
141 | |||
142 | $response = $this->renderException($request, $e); |
||
143 | } |
||
144 | |||
145 | return $response; |
||
146 | } |
||
147 | |||
148 | /** |
||
149 | * 初始化 |
||
150 | */ |
||
151 | protected function initialize() |
||
155 | } |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * 执行应用程序 |
||
160 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
161 | * @return mixed |
||
162 | */ |
||
163 | protected function runWithRequest(Request $request) |
||
164 | { |
||
165 | $this->initialize(); |
||
166 | |||
167 | if ($this->multi) { |
||
168 | $this->parseMultiApp(); |
||
169 | } |
||
170 | |||
171 | // 设置开启事件机制 |
||
172 | $this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
||
173 | |||
174 | // 监听HttpRun |
||
175 | $this->app->event->trigger('HttpRun'); |
||
176 | |||
177 | // Session初始化 |
||
178 | $varSessionId = $this->app->config->get('route.var_session_id'); |
||
179 | if ($varSessionId && $this->request->request($varSessionId)) { |
||
180 | $this->app->session->setId($this->request->request($varSessionId)); |
||
181 | } else { |
||
182 | $sessionId = $this->app->cookie->get($this->app->config->get('session.cookie_name', 'PHPSESSID')) ?: ''; |
||
183 | $this->app->session->setId($sessionId); |
||
184 | } |
||
185 | |||
186 | $this->app->request->withCookie($this->app->cookie); |
||
187 | $this->app->request->withSession($this->app->session); |
||
188 | |||
189 | $withRoute = $this->app->config->get('app.with_route', true) ? function () { |
||
190 | $this->loadRoutes(); |
||
191 | } : null; |
||
192 | |||
193 | return $this->app->route->dispatch($request, $withRoute); |
||
194 | } |
||
195 | |||
196 | /** |
||
197 | * 加载路由 |
||
198 | * @access protected |
||
199 | * @return void |
||
200 | */ |
||
201 | protected function loadRoutes(): void |
||
221 | } |
||
222 | } |
||
223 | } |
||
224 | |||
225 | /** |
||
226 | * 获取路由目录 |
||
227 | * @access protected |
||
228 | * @return string |
||
229 | */ |
||
230 | protected function getRoutePath(): string |
||
231 | { |
||
232 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * Report the exception to the exception handler. |
||
237 | * |
||
238 | * @param Throwable $e |
||
1 ignored issue
–
show
|
|||
239 | * @return void |
||
240 | */ |
||
241 | protected function reportException(Throwable $e) |
||
242 | { |
||
243 | $this->app['error_handle']->report($e); |
||
244 | } |
||
245 | |||
246 | /** |
||
247 | * Render the exception to a response. |
||
248 | * |
||
249 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
250 | * @param Throwable $e |
||
1 ignored issue
–
show
|
|||
251 | * @return Response |
||
252 | */ |
||
253 | protected function renderException($request, Throwable $e) |
||
256 | } |
||
257 | |||
258 | /** |
||
259 | * 获取当前运行入口名称 |
||
260 | * @access protected |
||
261 | * @return string |
||
262 | */ |
||
263 | protected function getScriptName(): string |
||
264 | { |
||
265 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
266 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
267 | } elseif (isset($_SERVER['argv'][0])) { |
||
268 | $file = realpath($_SERVER['argv'][0]); |
||
269 | } |
||
270 | |||
271 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : ''; |
||
272 | } |
||
273 | |||
274 | /** |
||
275 | * 解析多应用 |
||
276 | */ |
||
277 | protected function parseMultiApp(): void |
||
326 | } |
||
327 | |||
328 | /** |
||
329 | * 加载应用文件 |
||
330 | * @param string $appName 应用名 |
||
1 ignored issue
–
show
|
|||
331 | * @return void |
||
332 | */ |
||
333 | protected function loadApp(string $appName): void |
||
379 | } |
||
380 | |||
381 | /** |
||
382 | * HttpEnd |
||
383 | * @return void |
||
384 | */ |
||
385 | public function end(Response $response): void |
||
393 | } |
||
394 | } |
||
395 |