| Total Complexity | 56 |
| Total Lines | 360 |
| 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 |
||
| 21 | class Http |
||
| 22 | { |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var App |
||
| 26 | */ |
||
| 27 | protected $app; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * 应用路径 |
||
| 31 | * @var string |
||
| 32 | */ |
||
| 33 | protected $path; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * 是否多应用模式 |
||
| 37 | * @var bool |
||
| 38 | */ |
||
| 39 | protected $multi = false; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 是否域名绑定应用 |
||
| 43 | * @var bool |
||
| 44 | */ |
||
| 45 | protected $bindDomain = false; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * 应用名称 |
||
| 49 | * @var string |
||
| 50 | */ |
||
| 51 | protected $name; |
||
| 52 | |||
| 53 | public function __construct(App $app) |
||
| 54 | { |
||
| 55 | $this->app = $app; |
||
| 56 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * 是否域名绑定应用 |
||
| 61 | * @access public |
||
| 62 | * @return bool |
||
| 63 | */ |
||
| 64 | public function isBindDomain(): bool |
||
| 65 | { |
||
| 66 | return $this->bindDomain; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * 设置应用模式 |
||
| 71 | * @access public |
||
| 72 | * @param bool $multi |
||
|
1 ignored issue
–
show
|
|||
| 73 | * @return $this |
||
| 74 | */ |
||
| 75 | public function multi(bool $multi) |
||
| 76 | { |
||
| 77 | $this->multi = $multi; |
||
| 78 | return $this; |
||
| 79 | } |
||
| 80 | |||
| 81 | /** |
||
| 82 | * 是否为多应用模式 |
||
| 83 | * @access public |
||
| 84 | * @return bool |
||
| 85 | */ |
||
| 86 | public function isMulti(): bool |
||
| 87 | { |
||
| 88 | return $this->multi; |
||
| 89 | } |
||
| 90 | |||
| 91 | /** |
||
| 92 | * 设置应用名称 |
||
| 93 | * @access public |
||
| 94 | * @param string $name 应用名称 |
||
|
1 ignored issue
–
show
|
|||
| 95 | * @return $this |
||
| 96 | */ |
||
| 97 | public function name(string $name) |
||
| 98 | { |
||
| 99 | $this->name = $name; |
||
| 100 | return $this; |
||
| 101 | } |
||
| 102 | |||
| 103 | /** |
||
| 104 | * 获取应用名称 |
||
| 105 | * @access public |
||
| 106 | * @return string |
||
| 107 | */ |
||
| 108 | public function getName(): string |
||
| 109 | { |
||
| 110 | return $this->name ?: ''; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * 设置应用目录 |
||
| 115 | * @access public |
||
| 116 | * @param string $path 应用目录 |
||
|
1 ignored issue
–
show
|
|||
| 117 | * @return $this |
||
| 118 | */ |
||
| 119 | public function path(string $path) |
||
| 120 | { |
||
| 121 | $this->path = $path; |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * 执行应用程序 |
||
| 127 | * @access public |
||
| 128 | * @param Request|null $request |
||
|
1 ignored issue
–
show
|
|||
| 129 | * @return Response |
||
| 130 | */ |
||
| 131 | public function run(Request $request = null): Response |
||
| 132 | { |
||
| 133 | //自动创建request对象 |
||
| 134 | $request = $request ?? $this->app->make('request', [], true); |
||
| 135 | $this->app->instance('request', $request); |
||
| 136 | |||
| 137 | try { |
||
| 138 | $response = $this->runWithRequest($request); |
||
| 139 | } catch (Throwable $e) { |
||
| 140 | $this->reportException($e); |
||
| 141 | |||
| 142 | $response = $this->renderException($request, $e); |
||
| 143 | } |
||
| 144 | |||
| 145 | return $response; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * 初始化 |
||
| 150 | */ |
||
| 151 | protected function initialize() |
||
| 152 | { |
||
| 153 | if (!$this->app->initialized()) { |
||
| 154 | $this->app->initialize(); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * 执行应用程序 |
||
| 160 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 161 | * @return mixed |
||
| 162 | */ |
||
| 163 | protected function runWithRequest(Request $request) |
||
| 164 | { |
||
| 165 | $this->initialize(); |
||
| 166 | |||
| 167 | if ($this->multi) { |
||
| 168 | $this->parseMultiApp(); |
||
| 169 | } |
||
| 170 | |||
| 171 | // 设置开启事件机制 |
||
| 172 | $this->app->event->withEvent($this->app->config->get('app.with_event', true)); |
||
| 173 | |||
| 174 | // 监听HttpRun |
||
| 175 | $this->app->event->trigger('HttpRun'); |
||
| 176 | |||
| 177 | $withRoute = $this->app->config->get('app.with_route', true) ? function () { |
||
| 178 | $this->loadRoutes(); |
||
| 179 | } : null; |
||
| 180 | |||
| 181 | return $this->app->route->dispatch($request, $withRoute); |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | * 加载路由 |
||
| 186 | * @access protected |
||
| 187 | * @return void |
||
| 188 | */ |
||
| 189 | protected function loadRoutes(): void |
||
| 209 | } |
||
| 210 | } |
||
| 211 | } |
||
| 212 | |||
| 213 | /** |
||
| 214 | * 获取路由目录 |
||
| 215 | * @access protected |
||
| 216 | * @return string |
||
| 217 | */ |
||
| 218 | protected function getRoutePath(): string |
||
| 219 | { |
||
| 220 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
| 221 | } |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Report the exception to the exception handler. |
||
| 225 | * |
||
| 226 | * @param Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 227 | * @return void |
||
| 228 | */ |
||
| 229 | protected function reportException(Throwable $e) |
||
| 230 | { |
||
| 231 | $this->app['error_handle']->report($e); |
||
| 232 | } |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Render the exception to a response. |
||
| 236 | * |
||
| 237 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 238 | * @param Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 239 | * @return Response |
||
| 240 | */ |
||
| 241 | protected function renderException($request, Throwable $e) |
||
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * 获取当前运行入口名称 |
||
| 248 | * @access protected |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | protected function getScriptName(): string |
||
| 252 | { |
||
| 253 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
| 254 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
| 255 | } elseif (isset($_SERVER['argv'][0])) { |
||
| 256 | $file = realpath($_SERVER['argv'][0]); |
||
| 257 | } |
||
| 258 | |||
| 259 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : ''; |
||
| 260 | } |
||
| 261 | |||
| 262 | /** |
||
| 263 | * 解析多应用 |
||
| 264 | */ |
||
| 265 | protected function parseMultiApp(): void |
||
| 314 | } |
||
| 315 | |||
| 316 | /** |
||
| 317 | * 加载应用文件 |
||
| 318 | * @param string $appName 应用名 |
||
|
1 ignored issue
–
show
|
|||
| 319 | * @return void |
||
| 320 | */ |
||
| 321 | protected function loadApp(string $appName): void |
||
| 367 | } |
||
| 368 | |||
| 369 | /** |
||
| 370 | * HttpEnd |
||
| 371 | * @return void |
||
| 372 | */ |
||
| 373 | public function end(Response $response): void |
||
| 383 |