| Total Complexity | 52 |
| Total Lines | 373 |
| 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 string |
||
| 43 | */ |
||
| 44 | protected $defaultApp = 'index'; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 应用名称 |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $name; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 应用映射 |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | protected $map = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 域名映射 |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | protected $domain = []; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * 是否需要使用路由 |
||
| 66 | * @var bool |
||
| 67 | */ |
||
| 68 | protected $withRoute = true; |
||
| 69 | |||
| 70 | public function __construct(App $app) |
||
| 71 | { |
||
| 72 | $this->app = $app; |
||
| 73 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
||
| 74 | } |
||
| 75 | |||
| 76 | /** |
||
| 77 | * 自动多应用访问 |
||
| 78 | * @access public |
||
| 79 | * @param array $map 应用路由映射 |
||
| 80 | * @return $this |
||
| 81 | */ |
||
| 82 | public function autoMulti(array $map = []) |
||
| 83 | { |
||
| 84 | $this->multi = true; |
||
| 85 | $this->auto = true; |
||
| 86 | $this->map = $map; |
||
| 87 | |||
| 88 | return $this; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * 域名应用映射 |
||
| 93 | * @access public |
||
| 94 | * @param array $map 应用域名映射 |
||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function domain(array $map) |
||
| 98 | { |
||
| 99 | $this->domain = $map; |
||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * 是否为自动多应用模式 |
||
| 105 | * @access public |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | public function isAutoMulti(): bool |
||
| 109 | { |
||
| 110 | return $this->auto; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * 设置应用模式 |
||
| 115 | * @access public |
||
| 116 | * @param bool $multi |
||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function multi(bool $multi) |
||
| 120 | { |
||
| 121 | $this->multi = $multi; |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * 是否为多应用模式 |
||
| 127 | * @access public |
||
| 128 | * @return bool |
||
| 129 | */ |
||
| 130 | public function isMulti(): bool |
||
| 131 | { |
||
| 132 | return $this->multi; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * 设置默认应用(对多应用有效) |
||
| 137 | * @access public |
||
| 138 | * @param string $name 应用名 |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function defaultApp(string $name) |
||
| 142 | { |
||
| 143 | $this->defaultApp = $name; |
||
| 144 | return $this; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * 设置应用名称 |
||
| 149 | * @access public |
||
| 150 | * @param string $name 应用名称 |
||
| 151 | * @return $this |
||
| 152 | */ |
||
| 153 | public function name(string $name) |
||
| 154 | { |
||
| 155 | $this->name = $name; |
||
| 156 | return $this; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * 获取应用名称 |
||
| 161 | * @access public |
||
| 162 | * @return string |
||
| 163 | */ |
||
| 164 | public function getName(): string |
||
| 165 | { |
||
| 166 | return $this->name ?: ''; |
||
| 167 | } |
||
| 168 | |||
| 169 | /** |
||
| 170 | * 设置是否使用路由 |
||
| 171 | * @access public |
||
| 172 | * @param bool $route |
||
| 173 | * @return $this |
||
| 174 | */ |
||
| 175 | public function withRoute(bool $route) |
||
| 179 | } |
||
| 180 | |||
| 181 | /** |
||
| 182 | * 执行应用程序 |
||
| 183 | * @access public |
||
| 184 | * @param Request|null $request |
||
| 185 | * @return Response |
||
| 186 | */ |
||
| 187 | public function run(Request $request = null): Response |
||
| 188 | { |
||
| 189 | //自动创建request对象 |
||
| 190 | $request = $request ?? $this->app->make('request', [], true); |
||
| 191 | $this->app->instance('request', $request); |
||
| 192 | |||
| 193 | try { |
||
| 194 | $response = $this->runWithRequest($request); |
||
| 195 | } catch (\Throwable $e) { |
||
| 196 | $this->reportException($e); |
||
| 197 | |||
| 198 | $response = $this->renderException($request, $e); |
||
| 199 | } |
||
| 200 | |||
| 201 | return $response; |
||
| 202 | } |
||
| 203 | |||
| 204 | /** |
||
| 205 | * 执行应用程序 |
||
| 206 | * @param Request $request |
||
| 207 | * @return mixed |
||
| 208 | */ |
||
| 209 | protected function runWithRequest(Request $request) |
||
| 210 | { |
||
| 211 | $this->app->initialize(); |
||
| 212 | |||
| 213 | if ($this->multi) { |
||
| 214 | $this->parseMultiApp(); |
||
| 215 | } |
||
| 216 | |||
| 217 | $this->app->middleware->add(function (Request $request) { |
||
| 218 | |||
| 219 | $withRoute = $this->withRoute ? function () { |
||
| 220 | $this->loadRoutes(); |
||
| 221 | } : null; |
||
| 222 | |||
| 223 | return $this->app->route->dispatch($request, $withRoute); |
||
| 224 | }); |
||
| 225 | |||
| 226 | return $this->app->middleware->dispatch($request); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * 加载路由 |
||
| 231 | * @access protected |
||
| 232 | * @return void |
||
| 233 | */ |
||
| 234 | protected function loadRoutes(): void |
||
| 254 | } |
||
| 255 | } |
||
| 256 | } |
||
| 257 | |||
| 258 | /** |
||
| 259 | * 获取路由目录 |
||
| 260 | * @access protected |
||
| 261 | * @return string |
||
| 262 | */ |
||
| 263 | protected function getRoutePath(): string |
||
| 264 | { |
||
| 265 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Report the exception to the exception handler. |
||
| 270 | * |
||
| 271 | * @param \Throwable $e |
||
| 272 | * @return void |
||
| 273 | */ |
||
| 274 | protected function reportException(\Throwable $e) |
||
| 275 | { |
||
| 276 | $this->app['error_handle']->report($e); |
||
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Render the exception to a response. |
||
| 281 | * |
||
| 282 | * @param Request $request |
||
| 283 | * @param \Throwable $e |
||
| 284 | * @return Response |
||
| 285 | */ |
||
| 286 | protected function renderException($request, \Throwable $e) |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * 获取当前运行入口名称 |
||
| 293 | * @access protected |
||
| 294 | * @return string |
||
| 295 | */ |
||
| 296 | protected function getScriptName(): string |
||
| 297 | { |
||
| 298 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
| 299 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
| 300 | } elseif (isset($_SERVER['argv'][0])) { |
||
| 301 | $file = realpath($_SERVER['argv'][0]); |
||
| 302 | } |
||
| 303 | |||
| 304 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : $this->defaultApp; |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * 解析多应用 |
||
| 309 | */ |
||
| 310 | protected function parseMultiApp(): void |
||
| 348 | } |
||
| 349 | |||
| 350 | protected function loadApp(string $appName): void |
||
| 393 | } |
||
| 394 | } |
||
| 395 | } |
||
| 396 | } |
||
| 397 |