| Total Complexity | 54 |
| Total Lines | 397 |
| 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 $auto = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * 是否域名绑定应用 |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $bindDomain = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 默认应用名(多应用模式) |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $defaultApp = 'index'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 应用名称 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $name; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 应用映射 |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $map = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * 域名映射 |
||
| 66 | * @var array |
||
| 67 | */ |
||
| 68 | protected $domain = []; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * 是否需要使用路由 |
||
| 72 | * @var bool |
||
| 73 | */ |
||
| 74 | protected $withRoute = true; |
||
| 75 | |||
| 76 | public function __construct(App $app) |
||
| 77 | { |
||
| 78 | $this->app = $app; |
||
| 79 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * 自动多应用访问 |
||
| 84 | * @access public |
||
| 85 | * @param array $map 应用路由映射 |
||
|
1 ignored issue
–
show
|
|||
| 86 | * @return $this |
||
| 87 | */ |
||
| 88 | public function autoMulti(array $map = []) |
||
| 89 | { |
||
| 90 | $this->multi = true; |
||
| 91 | $this->auto = true; |
||
| 92 | $this->map = $map; |
||
| 93 | |||
| 94 | return $this; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * 域名应用映射 |
||
| 99 | * @access public |
||
| 100 | * @param array $map 应用域名映射 |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | public function domain(array $map) |
||
| 104 | { |
||
| 105 | $this->domain = $map; |
||
| 106 | return $this; |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * 域名绑定应用 |
||
| 111 | * @access public |
||
| 112 | * @return array |
||
| 113 | */ |
||
| 114 | public function getDomain(): array |
||
| 115 | { |
||
| 116 | return $this->domain; |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * 是否域名绑定应用 |
||
| 121 | * @access public |
||
| 122 | * @return bool |
||
| 123 | */ |
||
| 124 | public function isBindDomain(): bool |
||
| 125 | { |
||
| 126 | return $this->bindDomain; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * 是否为自动多应用模式 |
||
| 131 | * @access public |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | public function isAutoMulti(): bool |
||
| 135 | { |
||
| 136 | return $this->auto; |
||
| 137 | } |
||
| 138 | |||
| 139 | /** |
||
| 140 | * 设置应用模式 |
||
| 141 | * @access public |
||
| 142 | * @param bool $multi |
||
|
1 ignored issue
–
show
|
|||
| 143 | * @return $this |
||
| 144 | */ |
||
| 145 | public function multi(bool $multi) |
||
| 146 | { |
||
| 147 | $this->multi = $multi; |
||
| 148 | return $this; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * 是否为多应用模式 |
||
| 153 | * @access public |
||
| 154 | * @return bool |
||
| 155 | */ |
||
| 156 | public function isMulti(): bool |
||
| 157 | { |
||
| 158 | return $this->multi; |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * 设置默认应用(对多应用有效) |
||
| 163 | * @access public |
||
| 164 | * @param string $name 应用名 |
||
|
1 ignored issue
–
show
|
|||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function defaultApp(string $name) |
||
| 168 | { |
||
| 169 | $this->defaultApp = $name; |
||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * 设置应用名称 |
||
| 175 | * @access public |
||
| 176 | * @param string $name 应用名称 |
||
|
1 ignored issue
–
show
|
|||
| 177 | * @return $this |
||
| 178 | */ |
||
| 179 | public function name(string $name) |
||
| 180 | { |
||
| 181 | $this->name = $name; |
||
| 182 | return $this; |
||
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * 获取应用名称 |
||
| 187 | * @access public |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getName(): string |
||
| 191 | { |
||
| 192 | return $this->name ?: ''; |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * 设置是否使用路由 |
||
| 197 | * @access public |
||
| 198 | * @param bool $route |
||
|
1 ignored issue
–
show
|
|||
| 199 | * @return $this |
||
| 200 | */ |
||
| 201 | public function withRoute(bool $route) |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * 执行应用程序 |
||
| 209 | * @access public |
||
| 210 | * @param Request|null $request |
||
|
1 ignored issue
–
show
|
|||
| 211 | * @return Response |
||
| 212 | */ |
||
| 213 | public function run(Request $request = null): Response |
||
| 214 | { |
||
| 215 | //自动创建request对象 |
||
| 216 | $request = $request ?? $this->app->make('request', [], true); |
||
| 217 | $this->app->instance('request', $request); |
||
| 218 | |||
| 219 | try { |
||
| 220 | $response = $this->runWithRequest($request); |
||
| 221 | } catch (\Throwable $e) { |
||
| 222 | $this->reportException($e); |
||
| 223 | |||
| 224 | $response = $this->renderException($request, $e); |
||
| 225 | } |
||
| 226 | |||
| 227 | return $response; |
||
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * 执行应用程序 |
||
| 232 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | protected function runWithRequest(Request $request) |
||
| 236 | { |
||
| 237 | $this->app->initialize(); |
||
| 238 | |||
| 239 | if ($this->multi) { |
||
| 240 | $this->parseMultiApp(); |
||
| 241 | } |
||
| 242 | |||
| 243 | $withRoute = $this->withRoute ? function () { |
||
| 244 | $this->loadRoutes(); |
||
| 245 | } : null; |
||
| 246 | |||
| 247 | return $this->app->route->dispatch($request, $withRoute); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * 加载路由 |
||
| 252 | * @access protected |
||
| 253 | * @return void |
||
| 254 | */ |
||
| 255 | protected function loadRoutes(): void |
||
| 275 | } |
||
| 276 | } |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * 获取路由目录 |
||
| 281 | * @access protected |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | protected function getRoutePath(): string |
||
| 285 | { |
||
| 286 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Report the exception to the exception handler. |
||
| 291 | * |
||
| 292 | * @param \Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 293 | * @return void |
||
| 294 | */ |
||
| 295 | protected function reportException(\Throwable $e) |
||
| 296 | { |
||
| 297 | $this->app['error_handle']->report($e); |
||
| 298 | } |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Render the exception to a response. |
||
| 302 | * |
||
| 303 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 304 | * @param \Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 305 | * @return Response |
||
| 306 | */ |
||
| 307 | protected function renderException($request, \Throwable $e) |
||
| 310 | } |
||
| 311 | |||
| 312 | /** |
||
| 313 | * 获取当前运行入口名称 |
||
| 314 | * @access protected |
||
| 315 | * @return string |
||
| 316 | */ |
||
| 317 | protected function getScriptName(): string |
||
| 318 | { |
||
| 319 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
| 320 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
| 321 | } elseif (isset($_SERVER['argv'][0])) { |
||
| 322 | $file = realpath($_SERVER['argv'][0]); |
||
| 323 | } |
||
| 324 | |||
| 325 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : $this->defaultApp; |
||
| 326 | } |
||
| 327 | |||
| 328 | /** |
||
| 329 | * 解析多应用 |
||
| 330 | */ |
||
| 331 | protected function parseMultiApp(): void |
||
| 372 | } |
||
| 373 | |||
| 374 | protected function loadApp(string $appName): void |
||
| 417 | } |
||
| 418 | } |
||
| 419 | } |
||
| 420 | } |
||
| 421 |