| Total Complexity | 57 |
| Total Lines | 368 |
| 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) |
||
| 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 |
||
| 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() |
||
| 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 | $this->app->request->withCookie($this->app->cookie->get()); |
||
| 178 | $this->app->session->setName($this->app->config->get('session.name', 'PHPSESSID')); |
||
| 179 | $sessionId = $this->app->cookie->get($this->app->session->getName()) ?: ''; |
||
| 180 | $this->app->session->setId($sessionId); |
||
| 181 | $this->app->request->withSession($this->app->session->get()); |
||
| 182 | |||
| 183 | $withRoute = $this->app->config->get('app.with_route', true) ? function () { |
||
| 184 | $this->loadRoutes(); |
||
| 185 | } : null; |
||
| 186 | |||
| 187 | return $this->app->route->dispatch($request, $withRoute); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * 加载路由 |
||
| 192 | * @access protected |
||
| 193 | * @return void |
||
| 194 | */ |
||
| 195 | protected function loadRoutes(): void |
||
| 215 | } |
||
| 216 | } |
||
| 217 | } |
||
| 218 | |||
| 219 | /** |
||
| 220 | * 获取路由目录 |
||
| 221 | * @access protected |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | protected function getRoutePath(): string |
||
| 225 | { |
||
| 226 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Report the exception to the exception handler. |
||
| 231 | * |
||
| 232 | * @param Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 233 | * @return void |
||
| 234 | */ |
||
| 235 | protected function reportException(Throwable $e) |
||
| 236 | { |
||
| 237 | $this->app['error_handle']->report($e); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Render the exception to a response. |
||
| 242 | * |
||
| 243 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 244 | * @param Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 245 | * @return Response |
||
| 246 | */ |
||
| 247 | protected function renderException($request, Throwable $e) |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * 获取当前运行入口名称 |
||
| 254 | * @access protected |
||
| 255 | * @return string |
||
| 256 | */ |
||
| 257 | protected function getScriptName(): string |
||
| 258 | { |
||
| 259 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
| 260 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
| 261 | } elseif (isset($_SERVER['argv'][0])) { |
||
| 262 | $file = realpath($_SERVER['argv'][0]); |
||
| 263 | } |
||
| 264 | |||
| 265 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : ''; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * 解析多应用 |
||
| 270 | */ |
||
| 271 | protected function parseMultiApp(): void |
||
| 320 | } |
||
| 321 | |||
| 322 | /** |
||
| 323 | * 加载应用文件 |
||
| 324 | * @param string $appName 应用名 |
||
|
1 ignored issue
–
show
|
|||
| 325 | * @return void |
||
| 326 | */ |
||
| 327 | protected function loadApp(string $appName): void |
||
| 373 | } |
||
| 374 | |||
| 375 | /** |
||
| 376 | * HttpEnd |
||
| 377 | * @return void |
||
| 378 | */ |
||
| 379 | public function end(): void |
||
| 389 | } |
||
| 390 | } |
||
| 391 |