Total Complexity | 50 |
Total Lines | 304 |
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 |
||
20 | class Http |
||
21 | { |
||
22 | |||
23 | /** |
||
24 | * @var App |
||
25 | */ |
||
26 | protected $app; |
||
27 | |||
28 | /** |
||
29 | * 是否多应用模式 |
||
30 | * @var bool |
||
31 | */ |
||
32 | protected $multi = false; |
||
33 | |||
34 | /** |
||
35 | * 是否域名绑定应用 |
||
36 | * @var bool |
||
37 | */ |
||
38 | protected $bindDomain = false; |
||
39 | |||
40 | /** |
||
41 | * 应用名称 |
||
42 | * @var string |
||
43 | */ |
||
44 | protected $name; |
||
45 | |||
46 | public function __construct(App $app) |
||
47 | { |
||
48 | $this->app = $app; |
||
49 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
||
50 | } |
||
51 | |||
52 | /** |
||
53 | * 是否域名绑定应用 |
||
54 | * @access public |
||
55 | * @return bool |
||
56 | */ |
||
57 | public function isBindDomain(): bool |
||
58 | { |
||
59 | return $this->bindDomain; |
||
60 | } |
||
61 | |||
62 | /** |
||
63 | * 设置应用模式 |
||
64 | * @access public |
||
65 | * @param bool $multi |
||
1 ignored issue
–
show
|
|||
66 | * @return $this |
||
67 | */ |
||
68 | public function multi(bool $multi) |
||
69 | { |
||
70 | $this->multi = $multi; |
||
71 | return $this; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * 是否为多应用模式 |
||
76 | * @access public |
||
77 | * @return bool |
||
78 | */ |
||
79 | public function isMulti(): bool |
||
80 | { |
||
81 | return $this->multi; |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * 设置应用名称 |
||
86 | * @access public |
||
87 | * @param string $name 应用名称 |
||
1 ignored issue
–
show
|
|||
88 | * @return $this |
||
89 | */ |
||
90 | public function name(string $name) |
||
91 | { |
||
92 | $this->name = $name; |
||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | /** |
||
97 | * 获取应用名称 |
||
98 | * @access public |
||
99 | * @return string |
||
100 | */ |
||
101 | public function getName(): string |
||
102 | { |
||
103 | return $this->name ?: ''; |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * 执行应用程序 |
||
108 | * @access public |
||
109 | * @param Request|null $request |
||
1 ignored issue
–
show
|
|||
110 | * @return Response |
||
111 | */ |
||
112 | public function run(Request $request = null): Response |
||
113 | { |
||
114 | //自动创建request对象 |
||
115 | $request = $request ?? $this->app->make('request', [], true); |
||
116 | $this->app->instance('request', $request); |
||
117 | |||
118 | try { |
||
119 | $response = $this->runWithRequest($request); |
||
120 | } catch (\Throwable $e) { |
||
121 | $this->reportException($e); |
||
122 | |||
123 | $response = $this->renderException($request, $e); |
||
124 | } |
||
125 | |||
126 | return $response; |
||
127 | } |
||
128 | |||
129 | /** |
||
130 | * 执行应用程序 |
||
131 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
132 | * @return mixed |
||
133 | */ |
||
134 | protected function runWithRequest(Request $request) |
||
135 | { |
||
136 | $this->app->initialize(); |
||
137 | |||
138 | if ($this->multi) { |
||
139 | $this->parseMultiApp(); |
||
140 | } |
||
141 | |||
142 | $withRoute = $this->app->config->get('app.with_route') ? function () { |
||
143 | $this->loadRoutes(); |
||
144 | } : null; |
||
145 | |||
146 | return $this->app->route->dispatch($request, $withRoute); |
||
147 | } |
||
148 | |||
149 | /** |
||
150 | * 加载路由 |
||
151 | * @access protected |
||
152 | * @return void |
||
153 | */ |
||
154 | protected function loadRoutes(): void |
||
174 | } |
||
175 | } |
||
176 | } |
||
177 | |||
178 | /** |
||
179 | * 获取路由目录 |
||
180 | * @access protected |
||
181 | * @return string |
||
182 | */ |
||
183 | protected function getRoutePath(): string |
||
184 | { |
||
185 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
186 | } |
||
187 | |||
188 | /** |
||
189 | * Report the exception to the exception handler. |
||
190 | * |
||
191 | * @param \Throwable $e |
||
1 ignored issue
–
show
|
|||
192 | * @return void |
||
193 | */ |
||
194 | protected function reportException(\Throwable $e) |
||
195 | { |
||
196 | $this->app['error_handle']->report($e); |
||
197 | } |
||
198 | |||
199 | /** |
||
200 | * Render the exception to a response. |
||
201 | * |
||
202 | * @param Request $request |
||
1 ignored issue
–
show
|
|||
203 | * @param \Throwable $e |
||
1 ignored issue
–
show
|
|||
204 | * @return Response |
||
205 | */ |
||
206 | protected function renderException($request, \Throwable $e) |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * 获取当前运行入口名称 |
||
213 | * @access protected |
||
214 | * @return string |
||
215 | */ |
||
216 | protected function getScriptName(): string |
||
217 | { |
||
218 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
219 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
220 | } elseif (isset($_SERVER['argv'][0])) { |
||
221 | $file = realpath($_SERVER['argv'][0]); |
||
222 | } |
||
223 | |||
224 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : ''; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * 解析多应用 |
||
229 | */ |
||
230 | protected function parseMultiApp(): void |
||
279 | } |
||
280 | |||
281 | protected function loadApp(string $appName): void |
||
324 | } |
||
325 | } |
||
326 | } |
||
327 | } |
||
328 |