| Total Complexity | 83 |
| Total Lines | 510 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Web 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 Web, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Web extends App |
||
| 41 | { |
||
| 42 | |||
| 43 | /** |
||
| 44 | * 是否多应用模式 |
||
| 45 | * @var bool |
||
| 46 | */ |
||
| 47 | protected $multi = false; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * 是否自动多应用 |
||
| 51 | * @var bool |
||
| 52 | */ |
||
| 53 | protected $auto = false; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * 默认应用名(多应用模式) |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $defaultApp = 'index'; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * 路由目录 |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | protected $routePath = ''; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * URL |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | protected $urlPath = ''; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * 是否需要使用路由 |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | protected $withRoute = true; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * 访问控制器层名称 |
||
| 81 | * @var string |
||
| 82 | */ |
||
| 83 | protected $controllerLayer = 'controller'; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 是否使用控制器类库后缀 |
||
| 87 | * @var bool |
||
| 88 | */ |
||
| 89 | protected $controllerSuffix = false; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * 空控制器名称 |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | protected $emptyController = 'Error'; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * 加载应用文件和配置 |
||
| 99 | * @access protected |
||
| 100 | * @return void |
||
| 101 | */ |
||
| 102 | protected function load(): void |
||
| 103 | { |
||
| 104 | if ($this->multi && is_file($this->basePath . 'event.php')) { |
||
| 105 | $this->loadEvent(include $this->basePath . 'event.php'); |
||
| 106 | } |
||
| 107 | |||
| 108 | if (is_file($this->appPath . 'event.php')) { |
||
| 109 | $this->loadEvent(include $this->appPath . 'event.php'); |
||
| 110 | } |
||
| 111 | |||
| 112 | if ($this->multi && is_file($this->basePath . 'common.php')) { |
||
| 113 | include_once $this->basePath . 'common.php'; |
||
| 114 | } |
||
| 115 | |||
| 116 | if (is_file($this->appPath . 'common.php')) { |
||
| 117 | include_once $this->appPath . 'common.php'; |
||
| 118 | } |
||
| 119 | |||
| 120 | include $this->thinkPath . 'helper.php'; |
||
| 121 | |||
| 122 | if ($this->multi && is_file($this->basePath . 'middleware.php')) { |
||
| 123 | $this->middleware->import(include $this->basePath . 'middleware.php'); |
||
| 124 | } |
||
| 125 | |||
| 126 | if (is_file($this->appPath . 'middleware.php')) { |
||
| 127 | $this->middleware->import(include $this->appPath . 'middleware.php'); |
||
| 128 | } |
||
| 129 | |||
| 130 | if ($this->multi && is_file($this->basePath . 'provider.php')) { |
||
| 131 | $this->bind(include $this->basePath . 'provider.php'); |
||
| 132 | } |
||
| 133 | |||
| 134 | if (is_file($this->appPath . 'provider.php')) { |
||
| 135 | $this->bind(include $this->appPath . 'provider.php'); |
||
| 136 | } |
||
| 137 | |||
| 138 | $files = []; |
||
| 139 | |||
| 140 | if (is_dir($this->configPath)) { |
||
| 141 | $files = glob($this->configPath . '*' . $this->configExt); |
||
| 142 | } |
||
| 143 | |||
| 144 | if ($this->multi) { |
||
| 145 | if (is_dir($this->appPath . 'config')) { |
||
| 146 | $files = array_merge($files, glob($this->appPath . 'config' . DIRECTORY_SEPARATOR . '*' . $this->configExt)); |
||
| 147 | } elseif (is_dir($this->configPath . $this->name)) { |
||
| 148 | $files = array_merge($files, glob($this->configPath . $this->name . DIRECTORY_SEPARATOR . '*' . $this->configExt)); |
||
| 149 | } |
||
| 150 | } |
||
| 151 | |||
| 152 | foreach ($files as $file) { |
||
| 153 | $this->config->load($file, pathinfo($file, PATHINFO_FILENAME)); |
||
| 154 | } |
||
| 155 | } |
||
| 156 | |||
| 157 | /** |
||
| 158 | * 分析应用(参数) |
||
| 159 | * @access protected |
||
| 160 | * @return void |
||
| 161 | */ |
||
| 162 | protected function parse(): void |
||
| 163 | { |
||
| 164 | if (is_file($this->rootPath . '.env')) { |
||
| 165 | $this->env->load($this->rootPath . '.env'); |
||
| 166 | } |
||
| 167 | |||
| 168 | $this->parseAppName(); |
||
| 169 | |||
| 170 | $this->parsePath(); |
||
| 171 | |||
| 172 | if (!$this->namespace) { |
||
| 173 | $this->namespace = $this->multi ? $this->rootNamespace . '\\' . $this->name : $this->rootNamespace; |
||
| 174 | } |
||
| 175 | |||
| 176 | $this->configExt = $this->env->get('config_ext', '.php'); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * 自动多应用访问 |
||
| 181 | * @access public |
||
| 182 | * @param array $map 应用路由映射 |
||
| 183 | * @return $this |
||
| 184 | */ |
||
| 185 | public function autoMulti(array $map = []) |
||
| 186 | { |
||
| 187 | $this->multi = true; |
||
| 188 | $this->auto = true; |
||
| 189 | $this->map = $map; |
||
| 190 | |||
| 191 | return $this; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * 是否为自动多应用模式 |
||
| 196 | * @access public |
||
| 197 | * @return bool |
||
| 198 | */ |
||
| 199 | public function isAutoMulti(): bool |
||
| 200 | { |
||
| 201 | return $this->auto; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * 设置应用模式 |
||
| 206 | * @access public |
||
| 207 | * @param bool $multi |
||
| 208 | * @return $this |
||
| 209 | */ |
||
| 210 | public function multi(bool $multi) |
||
| 211 | { |
||
| 212 | $this->multi = $multi; |
||
| 213 | return $this; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * 是否为多应用模式 |
||
| 218 | * @access public |
||
| 219 | * @return bool |
||
| 220 | */ |
||
| 221 | public function isMulti(): bool |
||
| 222 | { |
||
| 223 | return $this->multi; |
||
| 224 | } |
||
| 225 | |||
| 226 | /** |
||
| 227 | * 设置是否使用路由 |
||
| 228 | * @access public |
||
| 229 | * @param bool $route |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function withRoute(bool $route) |
||
| 233 | { |
||
| 234 | $this->withRoute = $route; |
||
| 235 | return $this; |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * 设置默认应用(对多应用有效) |
||
| 240 | * @access public |
||
| 241 | * @param string $name 应用名 |
||
| 242 | * @return $this |
||
| 243 | */ |
||
| 244 | public function defaultApp(string $name) |
||
| 245 | { |
||
| 246 | $this->defaultApp = $name; |
||
| 247 | return $this; |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * 设置控制器层名称 |
||
| 252 | * @access public |
||
| 253 | * @param string $layer 控制器层名称 |
||
| 254 | * @return $this |
||
| 255 | */ |
||
| 256 | public function controllerLayer(string $layer) |
||
| 257 | { |
||
| 258 | $this->controllerLayer = $layer; |
||
| 259 | return $this; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * 设置空控制器名称 |
||
| 264 | * @access public |
||
| 265 | * @param string $empty 空控制器名称 |
||
| 266 | * @return $this |
||
| 267 | */ |
||
| 268 | public function emptyController(string $empty) |
||
| 269 | { |
||
| 270 | $this->emptyController = $empty; |
||
| 271 | return $this; |
||
| 272 | } |
||
| 273 | |||
| 274 | /** |
||
| 275 | * 设置是否启用控制器类库后缀 |
||
| 276 | * @access public |
||
| 277 | * @param bool $suffix 启用控制器类库后缀 |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function controllerSuffix(bool $suffix = true) |
||
| 281 | { |
||
| 282 | $this->controllerSuffix = $suffix; |
||
| 283 | return $this; |
||
| 284 | } |
||
| 285 | |||
| 286 | /** |
||
| 287 | * 是否启用控制器类库后缀 |
||
| 288 | * @access public |
||
| 289 | * @return bool |
||
| 290 | */ |
||
| 291 | public function hasControllerSuffix(): bool |
||
| 292 | { |
||
| 293 | return $this->controllerSuffix; |
||
| 294 | } |
||
| 295 | |||
| 296 | /** |
||
| 297 | * 获取控制器层名称 |
||
| 298 | * @access public |
||
| 299 | * @return string |
||
| 300 | */ |
||
| 301 | public function getControllerLayer(): string |
||
| 302 | { |
||
| 303 | return $this->controllerLayer; |
||
| 304 | } |
||
| 305 | |||
| 306 | /** |
||
| 307 | * 执行应用程序 |
||
| 308 | * @access public |
||
| 309 | * @return Response |
||
| 310 | * @throws Exception |
||
| 311 | */ |
||
| 312 | public function run(): Response |
||
| 313 | { |
||
| 314 | try { |
||
| 315 | if ($this->withRoute) { |
||
| 316 | $dispatch = $this->routeCheck()->init(); |
||
| 317 | } else { |
||
| 318 | $dispatch = $this->route->url($this->getRealPath())->init(); |
||
| 319 | } |
||
| 320 | |||
| 321 | // 监听AppBegin |
||
| 322 | $this->event->trigger('AppBegin'); |
||
| 323 | |||
| 324 | $data = null; |
||
| 325 | } catch (HttpResponseException $exception) { |
||
| 326 | $dispatch = null; |
||
| 327 | $data = $exception->getResponse(); |
||
| 328 | } |
||
| 329 | |||
| 330 | $this->middleware->add(function (Request $request, $next) use ($dispatch, $data) { |
||
| 331 | return is_null($data) ? $dispatch->run() : $data; |
||
| 332 | }); |
||
| 333 | |||
| 334 | $response = $this->middleware->dispatch($this->request); |
||
| 335 | |||
| 336 | // 监听AppEnd |
||
| 337 | $this->event->trigger('AppEnd', $response); |
||
| 338 | |||
| 339 | return $response; |
||
| 340 | } |
||
| 341 | |||
| 342 | /** |
||
| 343 | * 实例化访问控制器 |
||
| 344 | * @access public |
||
| 345 | * @param string $name 资源地址 |
||
| 346 | * @return object |
||
| 347 | * @throws ClassNotFoundException |
||
| 348 | */ |
||
| 349 | public function controller(string $name) |
||
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * 路由初始化(路由规则注册) |
||
| 365 | * @access protected |
||
| 366 | * @return void |
||
| 367 | */ |
||
| 368 | protected function routeInit(): void |
||
| 369 | { |
||
| 370 | // 加载路由定义 |
||
| 371 | if (is_dir($this->routePath)) { |
||
| 372 | $files = glob($this->routePath . DIRECTORY_SEPARATOR . '*.php'); |
||
| 373 | foreach ($files as $file) { |
||
| 374 | include $file; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | if ($this->route->config('route_annotation')) { |
||
| 379 | // 自动生成注解路由定义 |
||
| 380 | if ($this->isDebug()) { |
||
| 381 | $this->build->buildRoute(); |
||
| 382 | } |
||
| 383 | |||
| 384 | $filename = $this->runtimePath . 'build_route.php'; |
||
| 385 | |||
| 386 | if (is_file($filename)) { |
||
| 387 | include $filename; |
||
| 388 | } |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | /** |
||
| 393 | * URL路由检测(根据PATH_INFO) |
||
| 394 | * @access protected |
||
| 395 | * @return Dispatch |
||
| 396 | */ |
||
| 397 | protected function routeCheck(): Dispatch |
||
| 429 | } |
||
| 430 | |||
| 431 | /** |
||
| 432 | * 获取路由缓存Key |
||
| 433 | * @access protected |
||
| 434 | * @return string |
||
| 435 | */ |
||
| 436 | protected function getRouteCacheKey(): string |
||
| 446 | } |
||
| 447 | |||
| 448 | /** |
||
| 449 | * 获取自动多应用模式下的实际URL Path |
||
| 450 | * @access public |
||
| 451 | * @return string |
||
| 452 | */ |
||
| 453 | public function getRealPath(): string |
||
| 454 | { |
||
| 455 | $path = $this->urlPath; |
||
| 456 | |||
| 457 | if ($path && $this->auto) { |
||
| 458 | $path = substr_replace($path, '', 0, strpos($path, '/') ? strpos($path, '/') + 1 : strlen($path)); |
||
| 459 | } |
||
| 460 | |||
| 461 | return $path; |
||
| 462 | } |
||
| 463 | |||
| 464 | /** |
||
| 465 | * 分析应用路径 |
||
| 466 | * @access protected |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | protected function parsePath(): void |
||
| 470 | { |
||
| 471 | if ($this->multi) { |
||
| 472 | $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR . $this->name . DIRECTORY_SEPARATOR; |
||
| 473 | $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR . $this->name . DIRECTORY_SEPARATOR; |
||
| 474 | } else { |
||
| 475 | $this->runtimePath = $this->rootPath . 'runtime' . DIRECTORY_SEPARATOR; |
||
| 476 | $this->routePath = $this->rootPath . 'route' . DIRECTORY_SEPARATOR; |
||
| 477 | } |
||
| 478 | |||
| 479 | if (!$this->appPath) { |
||
| 480 | $this->appPath = $this->multi ? $this->basePath . $this->name . DIRECTORY_SEPARATOR : $this->basePath; |
||
| 481 | } |
||
| 482 | |||
| 483 | $this->configPath = $this->rootPath . 'config' . DIRECTORY_SEPARATOR; |
||
| 484 | |||
| 485 | $this->env->set([ |
||
| 486 | 'think_path' => $this->thinkPath, |
||
| 487 | 'root_path' => $this->rootPath, |
||
| 488 | 'app_path' => $this->appPath, |
||
| 489 | 'runtime_path' => $this->runtimePath, |
||
| 490 | 'route_path' => $this->routePath, |
||
| 491 | 'config_path' => $this->configPath, |
||
| 492 | ]); |
||
| 493 | } |
||
| 494 | |||
| 495 | /** |
||
| 496 | * 分析当前请求的应用名 |
||
| 497 | * @access protected |
||
| 498 | * @return void |
||
| 499 | */ |
||
| 500 | protected function parseAppName(): void |
||
| 524 | } |
||
| 525 | |||
| 526 | /** |
||
| 527 | * 获取当前运行入口名称 |
||
| 528 | * @access protected |
||
| 529 | * @return string |
||
| 530 | */ |
||
| 531 | protected function getScriptName(): string |
||
| 540 | } |
||
| 541 | |||
| 542 | /** |
||
| 543 | * 获取路由目录 |
||
| 544 | * @access public |
||
| 545 | * @return string |
||
| 546 | */ |
||
| 547 | public function getRoutePath(): string |
||
| 550 | } |
||
| 551 | |||
| 552 | } |
||
| 553 |