| Total Complexity | 51 |
| Total Lines | 345 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Dispatch 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 Dispatch, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 20 | abstract class Dispatch |
||
|
1 ignored issue
–
show
|
|||
| 21 | { |
||
| 22 | /** |
||
| 23 | * 应用对象 |
||
| 24 | * @var App |
||
| 25 | */ |
||
| 26 | protected $app; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * 请求对象 |
||
| 30 | * @var Request |
||
| 31 | */ |
||
| 32 | protected $request; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * 路由规则 |
||
| 36 | * @var Rule |
||
| 37 | */ |
||
| 38 | protected $rule; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * 调度信息 |
||
| 42 | * @var mixed |
||
| 43 | */ |
||
| 44 | protected $dispatch; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * 调度参数 |
||
| 48 | * @var array |
||
| 49 | */ |
||
| 50 | protected $param; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * 状态码 |
||
| 54 | * @var string |
||
| 55 | */ |
||
| 56 | protected $code; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * 是否进行大小写转换 |
||
| 60 | * @var bool |
||
| 61 | */ |
||
| 62 | protected $convert; |
||
| 63 | |||
| 64 | public function __construct(Request $request, Rule $rule, $dispatch, $param = [], $code = null) |
||
| 65 | { |
||
| 66 | $this->request = $request; |
||
| 67 | $this->rule = $rule; |
||
| 68 | $this->app = Container::get('app'); |
||
| 69 | $this->dispatch = $dispatch; |
||
| 70 | $this->param = $param; |
||
| 71 | $this->code = $code; |
||
| 72 | |||
| 73 | if (isset($param['convert'])) { |
||
| 74 | $this->convert = $param['convert']; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | public function init() |
||
| 79 | { |
||
| 80 | // 执行路由后置操作 |
||
| 81 | if ($this->rule->doAfter()) { |
||
| 82 | // 设置请求的路由信息 |
||
| 83 | |||
| 84 | // 设置当前请求的参数 |
||
| 85 | $this->request->setRouteVars($this->rule->getVars()); |
||
| 86 | $this->request->routeInfo([ |
||
| 87 | 'rule' => $this->rule->getRule(), |
||
| 88 | 'route' => $this->rule->getRoute(), |
||
| 89 | 'option' => $this->rule->getOption(), |
||
| 90 | 'var' => $this->rule->getVars(), |
||
| 91 | ]); |
||
| 92 | |||
| 93 | $this->doRouteAfter(); |
||
| 94 | } |
||
| 95 | |||
| 96 | return $this; |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * 检查路由后置操作 |
||
| 101 | * @access protected |
||
| 102 | * @return void |
||
| 103 | */ |
||
| 104 | protected function doRouteAfter() |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * 执行路由调度 |
||
| 147 | * @access public |
||
| 148 | * @return mixed |
||
| 149 | */ |
||
| 150 | public function run() |
||
| 171 | } |
||
| 172 | |||
| 173 | protected function autoResponse($data) |
||
| 174 | { |
||
| 175 | if ($data instanceof Response) { |
||
| 176 | $response = $data; |
||
| 177 | } elseif (!is_null($data)) { |
||
| 178 | // 默认自动识别响应输出类型 |
||
| 179 | $isAjax = $this->request->isAjax(); |
||
| 180 | $type = $isAjax ? $this->rule->getConfig('default_ajax_return') : $this->rule->getConfig('default_return_type'); |
||
| 181 | |||
| 182 | $response = Response::create($data, $type); |
||
| 183 | } else { |
||
| 184 | $data = ob_get_clean(); |
||
| 185 | $content = false === $data ? '' : $data; |
||
| 186 | $status = '' === $content && $this->request->isJson() ? 204 : 200; |
||
| 187 | |||
| 188 | $response = Response::create($content, '', $status); |
||
| 189 | } |
||
| 190 | |||
| 191 | return $response; |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * 检查路由后置行为 |
||
| 196 | * @access protected |
||
| 197 | * @param mixed $after 后置行为 |
||
| 198 | * @return mixed |
||
| 199 | */ |
||
| 200 | protected function checkAfter($after) |
||
| 201 | { |
||
| 202 | $this->app['log']->notice('路由后置行为建议使用中间件替代!'); |
||
| 203 | |||
| 204 | $hook = $this->app['hook']; |
||
| 205 | |||
| 206 | $result = null; |
||
| 207 | |||
| 208 | foreach ((array) $after as $behavior) { |
||
| 209 | $result = $hook->exec($behavior); |
||
| 210 | |||
| 211 | if (!is_null($result)) { |
||
| 212 | break; |
||
| 213 | } |
||
| 214 | } |
||
| 215 | |||
| 216 | // 路由规则重定向 |
||
| 217 | if ($result instanceof Response) { |
||
| 218 | return $result; |
||
| 219 | } |
||
| 220 | |||
| 221 | return false; |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * 验证数据 |
||
| 226 | * @access protected |
||
| 227 | * @param array $option |
||
| 228 | * @return void |
||
| 229 | * @throws ValidateException |
||
| 230 | */ |
||
| 231 | protected function autoValidate($option) |
||
| 232 | { |
||
| 233 | list($validate, $scene, $message, $batch) = $option; |
||
| 234 | |||
| 235 | if (is_array($validate)) { |
||
| 236 | // 指定验证规则 |
||
| 237 | $v = $this->app->validate(); |
||
| 238 | $v->rule($validate); |
||
| 239 | } else { |
||
| 240 | // 调用验证器 |
||
| 241 | $v = $this->app->validate($validate); |
||
| 242 | if (!empty($scene)) { |
||
| 243 | $v->scene($scene); |
||
| 244 | } |
||
| 245 | } |
||
| 246 | |||
| 247 | if (!empty($message)) { |
||
| 248 | $v->message($message); |
||
| 249 | } |
||
| 250 | |||
| 251 | // 批量验证 |
||
| 252 | if ($batch) { |
||
| 253 | $v->batch(true); |
||
| 254 | } |
||
| 255 | |||
| 256 | if (!$v->check($this->request->param())) { |
||
| 257 | throw new ValidateException($v->getError()); |
||
| 258 | } |
||
| 259 | } |
||
| 260 | |||
| 261 | /** |
||
| 262 | * 处理路由请求缓存 |
||
| 263 | * @access protected |
||
| 264 | * @param Request $request 请求对象 |
||
| 265 | * @param string|array $cache 路由缓存 |
||
| 266 | * @return void |
||
| 267 | */ |
||
| 268 | protected function parseRequestCache($cache) |
||
| 269 | { |
||
| 270 | if (is_array($cache)) { |
||
| 271 | list($key, $expire, $tag) = array_pad($cache, 3, null); |
||
| 272 | } else { |
||
| 273 | $key = str_replace('|', '/', $this->request->url()); |
||
| 274 | $expire = $cache; |
||
| 275 | $tag = null; |
||
| 276 | } |
||
| 277 | |||
| 278 | $cache = $this->request->cache($key, $expire, $tag); |
||
| 279 | $this->app->setResponseCache($cache); |
||
| 280 | } |
||
| 281 | |||
| 282 | /** |
||
| 283 | * 路由绑定模型实例 |
||
| 284 | * @access protected |
||
| 285 | * @param array|\Clousre $bindModel 绑定模型 |
||
| 286 | * @param array $matches 路由变量 |
||
| 287 | * @return void |
||
| 288 | */ |
||
| 289 | protected function createBindModel($bindModel, $matches) |
||
| 325 | } |
||
| 326 | } |
||
| 327 | } |
||
| 328 | |||
| 329 | public function convert($convert) |
||
| 330 | { |
||
| 331 | $this->convert = $convert; |
||
| 332 | |||
| 333 | return $this; |
||
| 334 | } |
||
| 335 | |||
| 336 | public function getDispatch() |
||
| 337 | { |
||
| 338 | return $this->dispatch; |
||
| 339 | } |
||
| 340 | |||
| 341 | public function getParam() |
||
| 342 | { |
||
| 343 | return $this->param; |
||
| 344 | } |
||
| 345 | |||
| 346 | abstract public function exec(); |
||
| 347 | |||
| 348 | public function __sleep() |
||
| 349 | { |
||
| 350 | return ['rule', 'dispatch', 'convert', 'param', 'code', 'controller', 'actionName']; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function __wakeup() |
||
| 357 | } |
||
| 358 | |||
| 359 | public function __debugInfo() |
||
| 360 | { |
||
| 361 | $data = get_object_vars($this); |
||
| 365 | } |
||
| 366 | } |
||
| 367 |