| Total Complexity | 64 |
| Total Lines | 388 |
| Duplicated Lines | 0 % |
| Coverage | 90.3% |
| 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) |
|
| 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 |
|
| 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 | if (substr($path, -1) != DIRECTORY_SEPARATOR) { |
|
| 124 | 1 | $path .= DIRECTORY_SEPARATOR; |
|
| 125 | } |
||
| 126 | |||
| 127 | 1 | $this->path = $path; |
|
| 128 | 1 | return $this; |
|
| 129 | } |
||
| 130 | |||
| 131 | /** |
||
| 132 | * 执行应用程序 |
||
| 133 | * @access public |
||
| 134 | * @param Request|null $request |
||
|
1 ignored issue
–
show
|
|||
| 135 | * @return Response |
||
| 136 | */ |
||
| 137 | 8 | public function run(Request $request = null): Response |
|
| 138 | { |
||
| 139 | //自动创建request对象 |
||
| 140 | 8 | $request = $request ?? $this->app->make('request', [], true); |
|
| 141 | 8 | $this->app->instance('request', $request); |
|
| 142 | |||
| 143 | try { |
||
| 144 | 8 | $response = $this->runWithRequest($request); |
|
| 145 | 2 | } catch (Throwable $e) { |
|
| 146 | 2 | $this->reportException($e); |
|
| 147 | |||
| 148 | 2 | $response = $this->renderException($request, $e); |
|
| 149 | } |
||
| 150 | |||
| 151 | 8 | return $response->setCookie($this->app->cookie); |
|
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * 初始化 |
||
| 156 | */ |
||
| 157 | 7 | protected function initialize() |
|
| 158 | { |
||
| 159 | 7 | if (!$this->app->initialized()) { |
|
| 160 | 7 | $this->app->initialize(); |
|
| 161 | } |
||
| 162 | 7 | } |
|
| 163 | |||
| 164 | /** |
||
| 165 | * 执行应用程序 |
||
| 166 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 167 | * @return mixed |
||
| 168 | */ |
||
| 169 | 7 | protected function runWithRequest(Request $request) |
|
| 170 | { |
||
| 171 | 7 | $this->initialize(); |
|
| 172 | |||
| 173 | // 加载全局中间件 |
||
| 174 | 7 | if (is_file($this->app->getBasePath() . 'middleware.php')) { |
|
| 175 | 7 | $this->app->middleware->import(include $this->app->getBasePath() . 'middleware.php'); |
|
| 176 | } |
||
| 177 | |||
| 178 | 7 | if ($this->multi) { |
|
| 179 | 6 | $this->parseMultiApp(); |
|
| 180 | } |
||
| 181 | |||
| 182 | // 设置开启事件机制 |
||
| 183 | 6 | $this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
|
| 184 | |||
| 185 | // 监听HttpRun |
||
| 186 | 6 | $this->app->event->trigger('HttpRun'); |
|
| 187 | |||
| 188 | $withRoute = $this->app->config->get('app.with_route', true) ? function () { |
||
| 189 | 6 | $this->loadRoutes(); |
|
| 190 | 6 | } : null; |
|
| 191 | |||
| 192 | 6 | return $this->app->route->dispatch($request, $withRoute); |
|
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * 加载路由 |
||
| 197 | * @access protected |
||
| 198 | * @return void |
||
| 199 | */ |
||
| 200 | 6 | protected function loadRoutes(): void |
|
| 220 | } |
||
| 221 | } |
||
| 222 | 6 | } |
|
| 223 | |||
| 224 | /** |
||
| 225 | * 获取路由目录 |
||
| 226 | * @access protected |
||
| 227 | * @return string |
||
| 228 | */ |
||
| 229 | 6 | protected function getRoutePath(): string |
|
| 230 | { |
||
| 231 | 6 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
|
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Report the exception to the exception handler. |
||
| 236 | * |
||
| 237 | * @param Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 238 | * @return void |
||
| 239 | */ |
||
| 240 | 1 | protected function reportException(Throwable $e) |
|
| 241 | { |
||
| 242 | 1 | $this->app->make(Handle::class)->report($e); |
|
| 243 | 1 | } |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Render the exception to a response. |
||
| 247 | * |
||
| 248 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 249 | * @param Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 250 | * @return Response |
||
| 251 | */ |
||
| 252 | 1 | protected function renderException($request, Throwable $e) |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * 获取当前运行入口名称 |
||
| 259 | * @access protected |
||
|
1 ignored issue
–
show
|
|||
| 260 | * @codeCoverageIgnore |
||
| 261 | * @return string |
||
|
1 ignored issue
–
show
|
|||
| 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 | 6 | protected function parseMultiApp(): void |
|
| 333 | 5 | } |
|
| 334 | |||
| 335 | /** |
||
| 336 | * 加载应用文件 |
||
| 337 | * @param string $appName 应用名 |
||
|
1 ignored issue
–
show
|
|||
| 338 | * @return void |
||
| 339 | */ |
||
| 340 | 5 | protected function loadApp(string $appName): void |
|
| 341 | { |
||
| 342 | 5 | $this->name = $appName; |
|
| 343 | 5 | $this->app->request->setApp($appName); |
|
| 344 | 5 | $this->app->setAppPath($this->path ?: $this->app->getBasePath() . $appName . DIRECTORY_SEPARATOR); |
|
| 345 | 5 | $this->app->setRuntimePath($this->app->getRootPath() . 'runtime' . DIRECTORY_SEPARATOR . $appName . DIRECTORY_SEPARATOR); |
|
| 346 | |||
| 347 | //加载app文件 |
||
| 348 | 5 | if (is_dir($this->app->getAppPath())) { |
|
| 349 | 1 | $appPath = $this->app->getAppPath(); |
|
| 350 | |||
| 351 | if (is_file($appPath . 'common.php')) { |
||
| 352 | include_once $appPath . 'common.php'; |
||
| 353 | 1 | } |
|
| 354 | |||
| 355 | 1 | $configPath = $this->app->getConfigPath(); |
|
| 356 | 1 | ||
| 357 | $files = []; |
||
| 358 | |||
| 359 | 1 | if (is_dir($configPath . $appName)) { |
|
| 360 | $files = array_merge($files, glob($configPath . $appName . DIRECTORY_SEPARATOR . '*' . $this->app->getConfigExt())); |
||
| 361 | 1 | } elseif (is_dir($appPath . 'config')) { |
|
| 362 | $files = array_merge($files, glob($appPath . 'config' . DIRECTORY_SEPARATOR . '*' . $this->app->getConfigExt())); |
||
| 363 | 1 | } |
|
| 364 | 1 | ||
| 365 | foreach ($files as $file) { |
||
| 366 | $this->app->config->load($file, pathinfo($file, PATHINFO_FILENAME)); |
||
| 367 | } |
||
| 368 | |||
| 369 | 1 | if (is_file($appPath . 'event.php')) { |
|
| 370 | $this->app->loadEvent(include $appPath . 'event.php'); |
||
| 371 | } |
||
| 372 | |||
| 373 | 1 | if (is_file($appPath . 'middleware.php')) { |
|
| 374 | 1 | $this->app->middleware->import(include $appPath . 'middleware.php'); |
|
| 375 | } |
||
| 376 | |||
| 377 | 1 | if (is_file($appPath . 'provider.php')) { |
|
| 378 | 1 | $this->app->bind(include $appPath . 'provider.php'); |
|
| 379 | } |
||
| 380 | } |
||
| 381 | 1 | ||
| 382 | 1 | // 加载应用默认语言包 |
|
| 383 | $this->app->loadLangPack($this->app->lang->defaultLangSet()); |
||
| 384 | |||
| 385 | // 设置应用命名空间 |
||
| 386 | $this->app->setNamespace($this->app->config->get('app.app_namespace') ?: 'app\\' . $appName); |
||
| 387 | } |
||
| 388 | 5 | ||
| 389 | /** |
||
| 390 | * HttpEnd |
||
| 391 | 5 | * @param Response $response |
|
|
1 ignored issue
–
show
|
|||
| 392 | 5 | * @return void |
|
| 393 | */ |
||
| 394 | public function end(Response $response): void |
||
| 395 | { |
||
| 396 | $this->app->event->trigger('HttpEnd', $response); |
||
| 397 | |||
| 398 | // 写入日志 |
||
| 399 | 1 | $this->app->log->save(); |
|
| 400 | // 写入Session |
||
| 401 | 1 | $this->app->session->save(); |
|
| 402 | } |
||
| 403 | |||
| 404 | 1 | public function __debugInfo() |
|
| 411 | ]; |
||
| 412 | } |
||
| 413 | } |
||
| 414 |