| Total Complexity | 53 |
| Total Lines | 325 |
| 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 string |
||
| 31 | */ |
||
| 32 | protected $path; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * 是否多应用模式 |
||
| 36 | * @var bool |
||
| 37 | */ |
||
| 38 | protected $multi = false; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * 是否域名绑定应用 |
||
| 42 | * @var bool |
||
| 43 | */ |
||
| 44 | protected $bindDomain = false; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 应用名称 |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | protected $name; |
||
| 51 | |||
| 52 | public function __construct(App $app) |
||
| 53 | { |
||
| 54 | $this->app = $app; |
||
| 55 | $this->multi = is_dir($this->app->getBasePath() . 'controller') ? false : true; |
||
| 56 | } |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 是否域名绑定应用 |
||
| 60 | * @access public |
||
| 61 | * @return bool |
||
| 62 | */ |
||
| 63 | public function isBindDomain(): bool |
||
| 64 | { |
||
| 65 | return $this->bindDomain; |
||
| 66 | } |
||
| 67 | |||
| 68 | /** |
||
| 69 | * 设置应用模式 |
||
| 70 | * @access public |
||
| 71 | * @param bool $multi |
||
|
1 ignored issue
–
show
|
|||
| 72 | * @return $this |
||
| 73 | */ |
||
| 74 | public function multi(bool $multi) |
||
| 75 | { |
||
| 76 | $this->multi = $multi; |
||
| 77 | return $this; |
||
| 78 | } |
||
| 79 | |||
| 80 | /** |
||
| 81 | * 是否为多应用模式 |
||
| 82 | * @access public |
||
| 83 | * @return bool |
||
| 84 | */ |
||
| 85 | public function isMulti(): bool |
||
| 86 | { |
||
| 87 | return $this->multi; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * 设置应用名称 |
||
| 92 | * @access public |
||
| 93 | * @param string $name 应用名称 |
||
|
1 ignored issue
–
show
|
|||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function name(string $name) |
||
| 97 | { |
||
| 98 | $this->name = $name; |
||
| 99 | return $this; |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * 获取应用名称 |
||
| 104 | * @access public |
||
| 105 | * @return string |
||
| 106 | */ |
||
| 107 | public function getName(): string |
||
| 108 | { |
||
| 109 | return $this->name ?: ''; |
||
| 110 | } |
||
| 111 | |||
| 112 | /** |
||
| 113 | * 设置应用目录 |
||
| 114 | * @access public |
||
| 115 | * @param string $path 应用目录 |
||
|
1 ignored issue
–
show
|
|||
| 116 | * @return $this |
||
| 117 | */ |
||
| 118 | public function path(string $path) |
||
| 119 | { |
||
| 120 | $this->path = $path; |
||
| 121 | return $this; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * 执行应用程序 |
||
| 126 | * @access public |
||
| 127 | * @param Request|null $request |
||
|
1 ignored issue
–
show
|
|||
| 128 | * @return Response |
||
| 129 | */ |
||
| 130 | public function run(Request $request = null): Response |
||
| 131 | { |
||
| 132 | //自动创建request对象 |
||
| 133 | $request = $request ?? $this->app->make('request', [], true); |
||
| 134 | $this->app->instance('request', $request); |
||
| 135 | |||
| 136 | try { |
||
| 137 | $response = $this->runWithRequest($request); |
||
| 138 | } catch (\Throwable $e) { |
||
| 139 | $this->reportException($e); |
||
| 140 | |||
| 141 | $response = $this->renderException($request, $e); |
||
| 142 | } |
||
| 143 | |||
| 144 | return $response; |
||
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * 执行应用程序 |
||
| 149 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 150 | * @return mixed |
||
| 151 | */ |
||
| 152 | protected function runWithRequest(Request $request) |
||
| 153 | { |
||
| 154 | $this->app->initialize(); |
||
| 155 | |||
| 156 | if ($this->multi) { |
||
| 157 | $this->parseMultiApp(); |
||
| 158 | } |
||
| 159 | |||
| 160 | $withRoute = $this->app->config->get('app.with_route') ? function () { |
||
| 161 | $this->loadRoutes(); |
||
| 162 | } : null; |
||
| 163 | |||
| 164 | return $this->app->route->dispatch($request, $withRoute); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * 加载路由 |
||
| 169 | * @access protected |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | protected function loadRoutes(): void |
||
| 192 | } |
||
| 193 | } |
||
| 194 | } |
||
| 195 | |||
| 196 | /** |
||
| 197 | * 获取路由目录 |
||
| 198 | * @access protected |
||
| 199 | * @return string |
||
| 200 | */ |
||
| 201 | protected function getRoutePath(): string |
||
| 202 | { |
||
| 203 | return $this->app->getRootPath() . 'route' . DIRECTORY_SEPARATOR . ($this->isMulti() ? $this->getName() . DIRECTORY_SEPARATOR : ''); |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Report the exception to the exception handler. |
||
| 208 | * |
||
| 209 | * @param \Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 210 | * @return void |
||
| 211 | */ |
||
| 212 | protected function reportException(\Throwable $e) |
||
| 213 | { |
||
| 214 | $this->app['error_handle']->report($e); |
||
| 215 | } |
||
| 216 | |||
| 217 | /** |
||
| 218 | * Render the exception to a response. |
||
| 219 | * |
||
| 220 | * @param Request $request |
||
|
1 ignored issue
–
show
|
|||
| 221 | * @param \Throwable $e |
||
|
1 ignored issue
–
show
|
|||
| 222 | * @return Response |
||
| 223 | */ |
||
| 224 | protected function renderException($request, \Throwable $e) |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * 获取当前运行入口名称 |
||
| 231 | * @access protected |
||
| 232 | * @return string |
||
| 233 | */ |
||
| 234 | protected function getScriptName(): string |
||
| 235 | { |
||
| 236 | if (isset($_SERVER['SCRIPT_FILENAME'])) { |
||
| 237 | $file = $_SERVER['SCRIPT_FILENAME']; |
||
| 238 | } elseif (isset($_SERVER['argv'][0])) { |
||
| 239 | $file = realpath($_SERVER['argv'][0]); |
||
| 240 | } |
||
| 241 | |||
| 242 | return isset($file) ? pathinfo($file, PATHINFO_FILENAME) : ''; |
||
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * 解析多应用 |
||
| 247 | */ |
||
| 248 | protected function parseMultiApp(): void |
||
| 297 | } |
||
| 298 | |||
| 299 | protected function loadApp(string $appName): void |
||
| 345 | } |
||
| 346 | } |
||
| 347 |