| Total Complexity | 48 |
| Total Lines | 345 |
| 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 bool |
||
| 61 | */ |
||
| 62 | protected $withRoute = true; |
||
| 63 | |||
| 64 | public function __construct(App $app) |
||
| 65 | { |
||
| 66 | $this->app = $app; |
||
| 67 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
||
| 68 | } |
||
| 69 | |||
| 70 | /** |
||
| 71 | * 自动多应用访问 |
||
| 72 | * @access public |
||
| 73 | * @param array $map 应用路由映射 |
||
| 74 | * @return $this |
||
| 75 | */ |
||
| 76 | public function autoMulti(array $map = []) |
||
| 77 | { |
||
| 78 | $this->multi = true; |
||
| 79 | $this->auto = true; |
||
| 80 | $this->map = $map; |
||
| 81 | |||
| 82 | return $this; |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 是否为自动多应用模式 |
||
| 87 | * @access public |
||
| 88 | * @return bool |
||
| 89 | */ |
||
| 90 | public function isAutoMulti(): bool |
||
| 91 | { |
||
| 92 | return $this->auto; |
||
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * 设置应用模式 |
||
| 97 | * @access public |
||
| 98 | * @param bool $multi |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function multi(bool $multi) |
||
| 102 | { |
||
| 103 | $this->multi = $multi; |
||
| 104 | return $this; |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * 是否为多应用模式 |
||
| 109 | * @access public |
||
| 110 | * @return bool |
||
| 111 | */ |
||
| 112 | public function isMulti(): bool |
||
| 113 | { |
||
| 114 | return $this->multi; |
||
| 115 | } |
||
| 116 | |||
| 117 | /** |
||
| 118 | * 设置默认应用(对多应用有效) |
||
| 119 | * @access public |
||
| 120 | * @param string $name 应用名 |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function defaultApp(string $name) |
||
| 124 | { |
||
| 125 | $this->defaultApp = $name; |
||
| 126 | return $this; |
||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * 设置应用名称 |
||
| 131 | * @access public |
||
| 132 | * @param string $name 应用名称 |
||
| 133 | * @return $this |
||
| 134 | */ |
||
| 135 | public function name(string $name) |
||
| 136 | { |
||
| 137 | $this->name = $name; |
||
| 138 | return $this; |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * 获取应用名称 |
||
| 143 | * @access public |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | public function getName(): string |
||
| 147 | { |
||
| 148 | return $this->name ?: ''; |
||
| 149 | } |
||
| 150 | |||
| 151 | /** |
||
| 152 | * 设置是否使用路由 |
||
| 153 | * @access public |
||
| 154 | * @param bool $route |
||
| 155 | * @return $this |
||
| 156 | */ |
||
| 157 | public function withRoute(bool $route) |
||
| 161 | } |
||
| 162 | |||
| 163 | /** |
||
| 164 | * 执行应用程序 |
||
| 165 | * @access public |
||
| 166 | * @param Request|null $request |
||
| 167 | * @return Response |
||
| 168 | */ |
||
| 169 | public function run(Request $request = null): Response |
||
| 170 | { |
||
| 171 | //自动创建request对象 |
||
| 172 | $request = $request ?? $this->app->make('request', [], true); |
||
| 173 | $this->app->instance('request', $request); |
||
| 174 | |||
| 175 | try { |
||
| 176 | $response = $this->runWithRequest($request); |
||
| 177 | } catch (\Throwable $e) { |
||
| 178 | $this->reportException($e); |
||
| 179 | |||
| 180 | $response = $this->renderException($request, $e); |
||
| 181 | } |
||
| 182 | |||
| 183 | return $response; |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * 执行应用程序 |
||
| 188 | * @param Request $request |
||
| 189 | * @return mixed |
||
| 190 | */ |
||
| 191 | protected function runWithRequest(Request $request) |
||
| 192 | { |
||
| 193 | $this->app->initialize(); |
||
| 194 | |||
| 195 | if ($this->multi) { |
||
| 196 | $this->parseMultiApp(); |
||
| 197 | } |
||
| 198 | |||
| 199 | $this->app->middleware->add(function (Request $request) { |
||
| 200 | |||
| 201 | $withRoute = $this->withRoute ? function () { |
||
| 202 | $this->loadRoutes(); |
||
| 203 | } : null; |
||
| 204 | |||
| 205 | return $this->app->route->dispatch($request, $withRoute); |
||
| 206 | }); |
||
| 207 | |||
| 208 | return $this->app->middleware->dispatch($request); |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * 加载路由 |
||
| 213 | * @access protected |
||
| 214 | * @return void |
||
| 215 | */ |
||
| 216 | protected function loadRoutes(): void |
||
| 236 | } |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * 获取路由目录 |
||
| 242 | * @access protected |
||
| 243 | * @return string |
||
| 244 | */ |
||
| 245 | protected function getRoutePath(): string |
||
| 246 | { |
||
| 247 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
| 248 | } |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Report the exception to the exception handler. |
||
| 252 | * |
||
| 253 | * @param \Throwable $e |
||
| 254 | * @return void |
||
| 255 | */ |
||
| 256 | protected function reportException(\Throwable $e) |
||
| 257 | { |
||
| 258 | $this->app['error_handle']->report($e); |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Render the exception to a response. |
||
| 263 | * |
||
| 264 | * @param Request $request |
||
| 265 | * @param \Throwable $e |
||
| 266 | * @return Response |
||
| 267 | */ |
||
| 268 | protected function renderException($request, \Throwable $e) |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * 获取当前运行入口名称 |
||
| 275 | * @access protected |
||
| 276 | * @return string |
||
| 277 | */ |
||
| 278 | protected function getScriptName(): string |
||
| 279 | { |
||
| 280 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
| 281 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
| 282 | } elseif (isset($_SERVER['argv'][0])) { |
||
| 283 | $file = realpath($_SERVER['argv'][0]); |
||
| 284 | } |
||
| 285 | |||
| 286 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : $this->defaultApp; |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * 解析多应用 |
||
| 291 | */ |
||
| 292 | protected function parseMultiApp(): void |
||
| 320 | } |
||
| 321 | |||
| 322 | protected function loadApp(string $appName): void |
||
| 365 | } |
||
| 366 | } |
||
| 367 | } |
||
| 368 | } |
||
| 369 |