| Total Complexity | 131 |
| Total Lines | 945 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Rule 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 Rule, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | abstract class Rule |
||
|
1 ignored issue
–
show
|
|||
| 26 | { |
||
| 27 | /** |
||
| 28 | * 路由标识 |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $name; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * 路由对象 |
||
| 35 | * @var Route |
||
| 36 | */ |
||
| 37 | protected $router; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * 路由所属分组 |
||
| 41 | * @var RuleGroup |
||
| 42 | */ |
||
| 43 | protected $parent; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * 路由规则 |
||
| 47 | * @var mixed |
||
| 48 | */ |
||
| 49 | protected $rule; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * 路由地址 |
||
| 53 | * @var string|\Closure |
||
| 54 | */ |
||
| 55 | protected $route; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * 请求类型 |
||
| 59 | * @var string |
||
| 60 | */ |
||
| 61 | protected $method; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * 路由变量 |
||
| 65 | * @var array |
||
| 66 | */ |
||
| 67 | protected $vars = []; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * 路由参数 |
||
| 71 | * @var array |
||
| 72 | */ |
||
| 73 | protected $option = []; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * 路由变量规则 |
||
| 77 | * @var array |
||
| 78 | */ |
||
| 79 | protected $pattern = []; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * 需要和分组合并的路由参数 |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $mergeOptions = ['after', 'model', 'header', 'response', 'append', 'middleware']; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * 是否需要后置操作 |
||
| 89 | * @var bool |
||
| 90 | */ |
||
| 91 | protected $doAfter = false; |
||
| 92 | |||
| 93 | abstract public function check(Request $request, string $url, bool $completeMatch = false); |
||
| 94 | |||
| 95 | /** |
||
| 96 | * 设置路由参数 |
||
| 97 | * @access public |
||
| 98 | * @param array $option 参数 |
||
| 99 | * @return $this |
||
| 100 | */ |
||
| 101 | public function option(array $option) |
||
| 102 | { |
||
| 103 | $this->option = array_merge($this->option, $option); |
||
| 104 | |||
| 105 | return $this; |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * 设置单个路由参数 |
||
| 110 | * @access public |
||
| 111 | * @param string $name 参数名 |
||
| 112 | * @param mixed $value 值 |
||
| 113 | * @return $this |
||
| 114 | */ |
||
| 115 | public function setOption(string $name, $value) |
||
| 116 | { |
||
| 117 | $this->option[$name] = $value; |
||
| 118 | |||
| 119 | return $this; |
||
| 120 | } |
||
| 121 | |||
| 122 | /** |
||
| 123 | * 注册变量规则 |
||
| 124 | * @access public |
||
| 125 | * @param array $pattern 变量规则 |
||
| 126 | * @return $this |
||
| 127 | */ |
||
| 128 | public function pattern(array $pattern) |
||
| 129 | { |
||
| 130 | $this->pattern = array_merge($this->pattern, $pattern); |
||
| 131 | |||
| 132 | return $this; |
||
| 133 | } |
||
| 134 | |||
| 135 | /** |
||
| 136 | * 设置标识 |
||
| 137 | * @access public |
||
| 138 | * @param string $name 标识名 |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function name(string $name) |
||
| 142 | { |
||
| 143 | $this->name = $name; |
||
| 144 | |||
| 145 | return $this; |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * 获取路由对象 |
||
| 150 | * @access public |
||
| 151 | * @return Route |
||
| 152 | */ |
||
| 153 | public function getRouter(): Route |
||
| 154 | { |
||
| 155 | return $this->router; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * 获取Name |
||
| 160 | * @access public |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | public function getName(): string |
||
| 164 | { |
||
| 165 | return $this->name; |
||
| 166 | } |
||
| 167 | |||
| 168 | /** |
||
| 169 | * 获取当前路由规则 |
||
| 170 | * @access public |
||
| 171 | * @return mixed |
||
| 172 | */ |
||
| 173 | public function getRule() |
||
| 174 | { |
||
| 175 | return $this->rule; |
||
| 176 | } |
||
| 177 | |||
| 178 | /** |
||
| 179 | * 获取当前路由地址 |
||
| 180 | * @access public |
||
| 181 | * @return mixed |
||
| 182 | */ |
||
| 183 | public function getRoute() |
||
| 184 | { |
||
| 185 | return $this->route; |
||
| 186 | } |
||
| 187 | |||
| 188 | /** |
||
| 189 | * 获取当前路由的变量 |
||
| 190 | * @access public |
||
| 191 | * @return array |
||
| 192 | */ |
||
| 193 | public function getVars(): array |
||
| 194 | { |
||
| 195 | return $this->vars; |
||
| 196 | } |
||
| 197 | |||
| 198 | /** |
||
| 199 | * 获取Parent对象 |
||
| 200 | * @access public |
||
| 201 | * @return $this|null |
||
| 202 | */ |
||
| 203 | public function getParent() |
||
| 204 | { |
||
| 205 | return $this->parent; |
||
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * 获取路由所在域名 |
||
| 210 | * @access public |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | public function getDomain(): string |
||
| 214 | { |
||
| 215 | return $this->parent->getDomain(); |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * 获取路由参数 |
||
| 220 | * @access public |
||
| 221 | * @param string $name 变量名 |
||
| 222 | * @return mixed |
||
| 223 | */ |
||
| 224 | public function config(string $name = '') |
||
| 227 | } |
||
| 228 | |||
| 229 | /** |
||
| 230 | * 获取变量规则定义 |
||
| 231 | * @access public |
||
| 232 | * @param string $name 变量名 |
||
| 233 | * @return mixed |
||
| 234 | */ |
||
| 235 | public function getPattern(string $name = '') |
||
| 236 | { |
||
| 242 | } |
||
| 243 | |||
| 244 | /** |
||
| 245 | * 获取路由参数定义 |
||
| 246 | * @access public |
||
| 247 | * @param string $name 参数名 |
||
| 248 | * @param mixed $default 默认值 |
||
| 249 | * @return mixed |
||
| 250 | */ |
||
| 251 | public function getOption(string $name = '', $default = null) |
||
| 252 | { |
||
| 253 | if ('' === $name) { |
||
| 254 | return $this->option; |
||
| 255 | } |
||
| 256 | |||
| 257 | return $this->option[$name] ?? $default; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * 获取当前路由的请求类型 |
||
| 262 | * @access public |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getMethod(): string |
||
| 266 | { |
||
| 267 | return strtolower($this->method); |
||
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * 路由是否有后置操作 |
||
| 272 | * @access public |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | public function doAfter(): bool |
||
| 276 | { |
||
| 277 | return $this->doAfter; |
||
| 278 | } |
||
| 279 | |||
| 280 | /** |
||
| 281 | * 设置路由请求类型 |
||
| 282 | * @access public |
||
| 283 | * @param string $method 请求类型 |
||
| 284 | * @return $this |
||
| 285 | */ |
||
| 286 | public function method(string $method) |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * 检查后缀 |
||
| 293 | * @access public |
||
| 294 | * @param string $ext URL后缀 |
||
| 295 | * @return $this |
||
| 296 | */ |
||
| 297 | public function ext(string $ext = '') |
||
| 298 | { |
||
| 299 | return $this->setOption('ext', $ext); |
||
| 300 | } |
||
| 301 | |||
| 302 | /** |
||
| 303 | * 检查禁止后缀 |
||
| 304 | * @access public |
||
| 305 | * @param string $ext URL后缀 |
||
| 306 | * @return $this |
||
| 307 | */ |
||
| 308 | public function denyExt(string $ext = '') |
||
| 309 | { |
||
| 310 | return $this->setOption('deny_ext', $ext); |
||
| 311 | } |
||
| 312 | |||
| 313 | /** |
||
| 314 | * 检查域名 |
||
| 315 | * @access public |
||
| 316 | * @param string $domain 域名 |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function domain(string $domain) |
||
| 320 | { |
||
| 321 | return $this->setOption('domain', $domain); |
||
| 322 | } |
||
| 323 | |||
| 324 | /** |
||
| 325 | * 设置参数过滤检查 |
||
| 326 | * @access public |
||
| 327 | * @param array $filter 参数过滤 |
||
| 328 | * @return $this |
||
| 329 | */ |
||
| 330 | public function filter(array $filter) |
||
| 331 | { |
||
| 332 | $this->option['filter'] = $filter; |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | /** |
||
| 338 | * 绑定模型 |
||
| 339 | * @access public |
||
| 340 | * @param array|string|\Closure $var 路由变量名 多个使用 & 分割 |
||
| 341 | * @param string|\Closure $model 绑定模型类 |
||
| 342 | * @param bool $exception 是否抛出异常 |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function model($var, $model = null, bool $exception = true) |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * 附加路由隐式参数 |
||
| 362 | * @access public |
||
| 363 | * @param array $append 追加参数 |
||
| 364 | * @return $this |
||
| 365 | */ |
||
| 366 | public function append(array $append = []) |
||
| 367 | { |
||
| 368 | $this->option['append'] = $append; |
||
| 369 | |||
| 370 | return $this; |
||
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * 绑定验证 |
||
| 375 | * @access public |
||
| 376 | * @param mixed $validate 验证器类 |
||
| 377 | * @param string $scene 验证场景 |
||
| 378 | * @param array $message 验证提示 |
||
| 379 | * @param bool $batch 批量验证 |
||
| 380 | * @return $this |
||
| 381 | */ |
||
| 382 | public function validate($validate, string $scene = null, array $message = [], bool $batch = false) |
||
| 383 | { |
||
| 384 | $this->option['validate'] = [$validate, $scene, $message, $batch]; |
||
| 385 | |||
| 386 | return $this; |
||
| 387 | } |
||
| 388 | |||
| 389 | /** |
||
| 390 | * 绑定Response对象 |
||
| 391 | * @access public |
||
| 392 | * @param mixed $response Response对象 |
||
| 393 | * @return $this |
||
| 394 | */ |
||
| 395 | public function response($response) |
||
| 396 | { |
||
| 397 | $this->option['response'][] = $response; |
||
| 398 | return $this; |
||
| 399 | } |
||
| 400 | |||
| 401 | /** |
||
| 402 | * 设置Response Header信息 |
||
| 403 | * @access public |
||
| 404 | * @param array $header 头信息 |
||
| 405 | * @return $this |
||
| 406 | */ |
||
| 407 | public function header(array $header) |
||
| 408 | { |
||
| 409 | $this->option['header'] = $header; |
||
| 410 | |||
| 411 | return $this; |
||
| 412 | } |
||
| 413 | |||
| 414 | /** |
||
| 415 | * 指定路由中间件 |
||
| 416 | * @access public |
||
| 417 | * @param string|array|\Closure $middleware 中间件 |
||
| 418 | * @param mixed $param 参数 |
||
| 419 | * @return $this |
||
| 420 | */ |
||
| 421 | public function middleware($middleware, $param = null) |
||
| 422 | { |
||
| 423 | if (is_null($param) && is_array($middleware)) { |
||
| 424 | $this->option['middleware'] = $middleware; |
||
| 425 | } else { |
||
| 426 | foreach ((array) $middleware as $item) { |
||
| 427 | $this->option['middleware'][] = [$item, $param]; |
||
| 428 | } |
||
| 429 | } |
||
| 430 | |||
| 431 | return $this; |
||
| 432 | } |
||
| 433 | |||
| 434 | /** |
||
| 435 | * 设置路由缓存 |
||
| 436 | * @access public |
||
| 437 | * @param array|string $cache 缓存 |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function cache($cache) |
||
| 441 | { |
||
| 442 | return $this->setOption('cache', $cache); |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * 检查URL分隔符 |
||
| 447 | * @access public |
||
| 448 | * @param string $depr URL分隔符 |
||
| 449 | * @return $this |
||
| 450 | */ |
||
| 451 | public function depr(string $depr) |
||
| 454 | } |
||
| 455 | |||
| 456 | /** |
||
| 457 | * 设置需要合并的路由参数 |
||
| 458 | * @access public |
||
| 459 | * @param array $option 路由参数 |
||
| 460 | * @return $this |
||
| 461 | */ |
||
| 462 | public function mergeOptions(array $option = []) |
||
| 466 | } |
||
| 467 | |||
| 468 | /** |
||
| 469 | * 检查是否为HTTPS请求 |
||
| 470 | * @access public |
||
| 471 | * @param bool $https 是否为HTTPS |
||
| 472 | * @return $this |
||
| 473 | */ |
||
| 474 | public function https(bool $https = true) |
||
| 477 | } |
||
| 478 | |||
| 479 | /** |
||
| 480 | * 检查是否为JSON请求 |
||
| 481 | * @access public |
||
| 482 | * @param bool $json 是否为JSON |
||
| 483 | * @return $this |
||
| 484 | */ |
||
| 485 | public function json(bool $json = true) |
||
| 486 | { |
||
| 487 | return $this->setOption('json', $json); |
||
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * 检查是否为AJAX请求 |
||
| 492 | * @access public |
||
| 493 | * @param bool $ajax 是否为AJAX |
||
| 494 | * @return $this |
||
| 495 | */ |
||
| 496 | public function ajax(bool $ajax = true) |
||
| 497 | { |
||
| 498 | return $this->setOption('ajax', $ajax); |
||
| 499 | } |
||
| 500 | |||
| 501 | /** |
||
| 502 | * 检查是否为PJAX请求 |
||
| 503 | * @access public |
||
| 504 | * @param bool $pjax 是否为PJAX |
||
| 505 | * @return $this |
||
| 506 | */ |
||
| 507 | public function pjax(bool $pjax = true) |
||
| 508 | { |
||
| 509 | return $this->setOption('pjax', $pjax); |
||
| 510 | } |
||
| 511 | |||
| 512 | /** |
||
| 513 | * 当前路由到一个模板地址 当使用数组的时候可以传入模板变量 |
||
| 514 | * @access public |
||
| 515 | * @param bool|array $view 视图 |
||
| 516 | * @return $this |
||
| 517 | */ |
||
| 518 | public function view($view = true) |
||
| 521 | } |
||
| 522 | |||
| 523 | /** |
||
| 524 | * 当前路由为重定向 |
||
| 525 | * @access public |
||
| 526 | * @param bool $redirect 是否为重定向 |
||
| 527 | * @return $this |
||
| 528 | */ |
||
| 529 | public function redirect(bool $redirect = true) |
||
| 530 | { |
||
| 531 | return $this->setOption('redirect', $redirect); |
||
| 532 | } |
||
| 533 | |||
| 534 | /** |
||
| 535 | * 设置status |
||
| 536 | * @access public |
||
| 537 | * @param int $status 状态码 |
||
| 538 | * @return $this |
||
| 539 | */ |
||
| 540 | public function status(int $status) |
||
| 541 | { |
||
| 542 | return $this->setOption('status', $status); |
||
| 543 | } |
||
| 544 | |||
| 545 | /** |
||
| 546 | * 设置路由完整匹配 |
||
| 547 | * @access public |
||
| 548 | * @param bool $match 是否完整匹配 |
||
| 549 | * @return $this |
||
| 550 | */ |
||
| 551 | public function completeMatch(bool $match = true) |
||
| 552 | { |
||
| 553 | return $this->setOption('complete_match', $match); |
||
| 554 | } |
||
| 555 | |||
| 556 | /** |
||
| 557 | * 是否去除URL最后的斜线 |
||
| 558 | * @access public |
||
| 559 | * @param bool $remove 是否去除最后斜线 |
||
| 560 | * @return $this |
||
| 561 | */ |
||
| 562 | public function removeSlash(bool $remove = true) |
||
| 565 | } |
||
| 566 | |||
| 567 | /** |
||
| 568 | * 设置路由规则全局有效 |
||
| 569 | * @access public |
||
| 570 | * @return $this |
||
| 571 | */ |
||
| 572 | public function crossDomainRule() |
||
| 573 | { |
||
| 574 | if ($this instanceof RuleGroup) { |
||
| 575 | $method = '*'; |
||
| 576 | } else { |
||
| 577 | $method = $this->method; |
||
| 578 | } |
||
| 579 | |||
| 580 | $this->router->setCrossDomainRule($this, $method); |
||
| 581 | |||
| 582 | return $this; |
||
| 583 | } |
||
| 584 | |||
| 585 | /** |
||
| 586 | * 合并分组参数 |
||
| 587 | * @access public |
||
| 588 | * @return array |
||
| 589 | */ |
||
| 590 | public function mergeGroupOptions(): array |
||
| 591 | { |
||
| 592 | $parentOption = $this->parent->getOption(); |
||
| 593 | // 合并分组参数 |
||
| 594 | foreach ($this->mergeOptions as $item) { |
||
| 595 | if (isset($parentOption[$item]) && isset($this->option[$item])) { |
||
| 596 | $this->option[$item] = array_merge($parentOption[$item], $this->option[$item]); |
||
| 597 | } |
||
| 598 | } |
||
| 599 | |||
| 600 | $this->option = array_merge($parentOption, $this->option); |
||
| 601 | |||
| 602 | return $this->option; |
||
| 603 | } |
||
| 604 | |||
| 605 | /** |
||
| 606 | * 解析匹配到的规则路由 |
||
| 607 | * @access public |
||
| 608 | * @param Request $request 请求对象 |
||
| 609 | * @param string $rule 路由规则 |
||
| 610 | * @param mixed $route 路由地址 |
||
| 611 | * @param string $url URL地址 |
||
| 612 | * @param array $option 路由参数 |
||
| 613 | * @param array $matches 匹配的变量 |
||
| 614 | * @return Dispatch |
||
| 615 | */ |
||
| 616 | public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch |
||
| 617 | { |
||
| 618 | if (is_string($route) && isset($option['prefix'])) { |
||
| 619 | // 路由地址前缀 |
||
| 620 | $route = $option['prefix'] . $route; |
||
| 621 | } |
||
| 622 | |||
| 623 | // 替换路由地址中的变量 |
||
| 624 | if (is_string($route) && !empty($matches)) { |
||
| 625 | $search = $replace = []; |
||
| 626 | |||
| 627 | foreach ($matches as $key => $value) { |
||
| 628 | $search[] = '<' . $key . '>'; |
||
| 629 | $replace[] = $value; |
||
| 630 | |||
| 631 | $search[] = ':' . $key; |
||
| 632 | $replace[] = $value; |
||
| 633 | } |
||
| 634 | |||
| 635 | $route = str_replace($search, $replace, $route); |
||
| 636 | } |
||
| 637 | |||
| 638 | // 解析额外参数 |
||
| 639 | $count = substr_count($rule, '/'); |
||
| 640 | $url = array_slice(explode('|', $url), $count + 1); |
||
| 641 | $this->parseUrlParams(implode('|', $url), $matches); |
||
| 642 | |||
| 643 | $this->route = $route; |
||
| 644 | $this->vars = $matches; |
||
| 645 | $this->option = $option; |
||
| 646 | $this->doAfter = true; |
||
| 647 | |||
| 648 | // 发起路由调度 |
||
| 649 | return $this->dispatch($request, $route, $option); |
||
| 650 | } |
||
| 651 | |||
| 652 | /** |
||
| 653 | * 发起路由调度 |
||
| 654 | * @access protected |
||
| 655 | * @param Request $request Request对象 |
||
| 656 | * @param mixed $route 路由地址 |
||
| 657 | * @param array $option 路由参数 |
||
| 658 | * @return Dispatch |
||
| 659 | */ |
||
| 660 | protected function dispatch(Request $request, $route, array $option): Dispatch |
||
| 661 | { |
||
| 662 | if ($route instanceof Dispatch) { |
||
| 663 | $result = $route; |
||
| 664 | } elseif ($route instanceof \Closure) { |
||
| 665 | // 执行闭包 |
||
| 666 | $result = new CallbackDispatch($request, $this, $route); |
||
| 667 | } elseif ($route instanceof Response) { |
||
| 668 | $result = new ResponseDispatch($request, $this, $route); |
||
| 669 | } elseif (isset($option['view']) && false !== $option['view']) { |
||
| 670 | $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []); |
||
| 671 | } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) { |
||
| 672 | // 路由到重定向地址 |
||
| 673 | $result = new RedirectDispatch($request, $this, $route, [], $option['status'] ?? 301); |
||
| 674 | } elseif (false !== strpos($route, '\\')) { |
||
| 675 | // 路由到类的方法 |
||
| 676 | $result = $this->dispatchMethod($request, $route); |
||
| 677 | } else { |
||
| 678 | // 路由到控制器/操作 |
||
| 679 | $result = $this->dispatchController($request, $route); |
||
| 680 | } |
||
| 681 | |||
| 682 | return $result; |
||
| 683 | } |
||
| 684 | |||
| 685 | /** |
||
| 686 | * 解析URL地址为 模块/控制器/操作 |
||
| 687 | * @access protected |
||
| 688 | * @param Request $request Request对象 |
||
| 689 | * @param string $route 路由地址 |
||
| 690 | * @return CallbackDispatch |
||
| 691 | */ |
||
| 692 | protected function dispatchMethod(Request $request, string $route): CallbackDispatch |
||
| 700 | } |
||
| 701 | |||
| 702 | /** |
||
| 703 | * 解析URL地址为 模块/控制器/操作 |
||
| 704 | * @access protected |
||
| 705 | * @param Request $request Request对象 |
||
| 706 | * @param string $route 路由地址 |
||
| 707 | * @return ControllerDispatch |
||
| 708 | */ |
||
| 709 | protected function dispatchController(Request $request, string $route): ControllerDispatch |
||
| 710 | { |
||
| 711 | list($path, $var) = $this->parseUrlPath($route); |
||
| 712 | |||
| 713 | $action = array_pop($path); |
||
| 714 | $controller = !empty($path) ? array_pop($path) : null; |
||
| 715 | |||
| 716 | // 路由到模块/控制器/操作 |
||
| 717 | return new ControllerDispatch($request, $this, [$controller, $action], $var); |
||
| 718 | } |
||
| 719 | |||
| 720 | /** |
||
| 721 | * 路由检查 |
||
| 722 | * @access protected |
||
| 723 | * @param array $option 路由参数 |
||
| 724 | * @param Request $request Request对象 |
||
| 725 | * @return bool |
||
| 726 | */ |
||
| 727 | protected function checkOption(array $option, Request $request): bool |
||
| 728 | { |
||
| 729 | // 请求类型检测 |
||
| 730 | if (!empty($option['method'])) { |
||
| 731 | if (is_string($option['method']) && false === stripos($option['method'], $request->method())) { |
||
| 732 | return false; |
||
| 733 | } |
||
| 734 | } |
||
| 735 | |||
| 736 | // AJAX PJAX 请求检查 |
||
| 737 | foreach (['ajax', 'pjax', 'json'] as $item) { |
||
| 738 | if (isset($option[$item])) { |
||
| 739 | $call = 'is' . $item; |
||
| 740 | if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) { |
||
| 741 | return false; |
||
| 742 | } |
||
| 743 | } |
||
| 744 | } |
||
| 745 | |||
| 746 | // 伪静态后缀检测 |
||
| 747 | if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|')) |
||
| 748 | || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) { |
||
| 749 | return false; |
||
| 750 | } |
||
| 751 | |||
| 752 | // 域名检查 |
||
| 753 | if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) { |
||
| 754 | return false; |
||
| 755 | } |
||
| 756 | |||
| 757 | // HTTPS检查 |
||
| 758 | if ((isset($option['https']) && $option['https'] && !$request->isSsl()) |
||
| 759 | || (isset($option['https']) && !$option['https'] && $request->isSsl())) { |
||
| 760 | return false; |
||
| 761 | } |
||
| 762 | |||
| 763 | // 请求参数检查 |
||
| 764 | if (isset($option['filter'])) { |
||
| 765 | foreach ($option['filter'] as $name => $value) { |
||
| 766 | if ($request->param($name, '', null) != $value) { |
||
| 767 | return false; |
||
| 768 | } |
||
| 769 | } |
||
| 770 | } |
||
| 771 | |||
| 772 | return true; |
||
| 773 | } |
||
| 774 | |||
| 775 | /** |
||
| 776 | * 解析URL地址中的参数Request对象 |
||
| 777 | * @access protected |
||
| 778 | * @param string $rule 路由规则 |
||
| 779 | * @param array $var 变量 |
||
| 780 | * @return void |
||
| 781 | */ |
||
| 782 | protected function parseUrlParams(string $url, array &$var = []): void |
||
| 783 | { |
||
| 784 | if ($url) { |
||
| 785 | preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) { |
||
| 786 | $var[$match[1]] = strip_tags($match[2]); |
||
| 787 | }, $url); |
||
| 788 | } |
||
| 789 | } |
||
| 790 | |||
| 791 | /** |
||
| 792 | * 解析URL的pathinfo参数和变量 |
||
| 793 | * @access public |
||
| 794 | * @param string $url URL地址 |
||
| 795 | * @return array |
||
| 796 | */ |
||
| 797 | public function parseUrlPath(string $url): array |
||
| 798 | { |
||
| 799 | // 分隔符替换 确保路由定义使用统一的分隔符 |
||
| 800 | $url = str_replace('|', '/', $url); |
||
| 801 | $url = trim($url, '/'); |
||
| 802 | $var = []; |
||
| 803 | |||
| 804 | if (false !== strpos($url, '?')) { |
||
| 805 | // [控制器/操作?]参数1=值1&参数2=值2... |
||
| 806 | $info = parse_url($url); |
||
| 807 | $path = explode('/', $info['path']); |
||
| 808 | parse_str($info['query'], $var); |
||
| 809 | } elseif (strpos($url, '/')) { |
||
| 810 | // [控制器/操作] |
||
| 811 | $path = explode('/', $url); |
||
| 812 | } elseif (false !== strpos($url, '=')) { |
||
| 813 | // 参数1=值1&参数2=值2... |
||
| 814 | parse_str($url, $var); |
||
| 815 | $path = []; |
||
| 816 | } else { |
||
| 817 | $path = [$url]; |
||
| 818 | } |
||
| 819 | |||
| 820 | return [$path, $var]; |
||
| 821 | } |
||
| 822 | |||
| 823 | /** |
||
| 824 | * 生成路由的正则规则 |
||
| 825 | * @access protected |
||
| 826 | * @param string $rule 路由规则 |
||
| 827 | * @param array $match 匹配的变量 |
||
| 828 | * @param array $pattern 路由变量规则 |
||
| 829 | * @param array $option 路由参数 |
||
| 830 | * @param bool $completeMatch 路由是否完全匹配 |
||
| 831 | * @param string $suffix 路由正则变量后缀 |
||
| 832 | * @return string |
||
| 833 | */ |
||
| 834 | protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string |
||
| 858 | } |
||
| 859 | |||
| 860 | /** |
||
| 861 | * 生成路由变量的正则规则 |
||
| 862 | * @access protected |
||
| 863 | * @param string $name 路由变量 |
||
| 864 | * @param array $pattern 变量规则 |
||
| 865 | * @param string $suffix 路由正则变量后缀 |
||
| 866 | * @return string |
||
| 867 | */ |
||
| 868 | protected function buildNameRegex(string $name, array $pattern, string $suffix): string |
||
| 869 | { |
||
| 870 | $optional = ''; |
||
| 871 | $slash = substr($name, 0, 1); |
||
| 872 | |||
| 873 | if (in_array($slash, ['/', '-'])) { |
||
| 874 | $prefix = '\\' . $slash; |
||
| 875 | $name = substr($name, 1); |
||
| 876 | $slash = substr($name, 0, 1); |
||
| 877 | } else { |
||
| 878 | $prefix = ''; |
||
| 879 | } |
||
| 880 | |||
| 881 | if ('<' != $slash) { |
||
| 882 | return $prefix . preg_quote($name, '/'); |
||
| 883 | } |
||
| 884 | |||
| 885 | if (strpos($name, '?')) { |
||
| 886 | $name = substr($name, 1, -2); |
||
| 887 | $optional = '?'; |
||
| 888 | } elseif (strpos($name, '>')) { |
||
| 889 | $name = substr($name, 1, -1); |
||
| 890 | } |
||
| 891 | |||
| 892 | if (isset($pattern[$name])) { |
||
| 893 | $nameRule = $pattern[$name]; |
||
| 894 | if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) { |
||
| 895 | $nameRule = substr($nameRule, 1, -1); |
||
| 896 | } |
||
| 897 | } else { |
||
| 898 | $nameRule = $this->router->config('default_route_pattern'); |
||
| 899 | } |
||
| 900 | |||
| 901 | return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional; |
||
| 902 | } |
||
| 903 | |||
| 904 | /** |
||
| 905 | * 分析路由规则中的变量 |
||
| 906 | * @access protected |
||
| 907 | * @param string $rule 路由规则 |
||
| 908 | * @return array |
||
| 909 | */ |
||
| 910 | protected function parseVar(string $rule): array |
||
| 911 | { |
||
| 912 | // 提取路由规则中的变量 |
||
| 913 | $var = []; |
||
| 914 | |||
| 915 | if (preg_match_all('/<\w+\??>/', $rule, $matches)) { |
||
| 916 | foreach ($matches[0] as $name) { |
||
| 917 | $optional = false; |
||
| 918 | |||
| 919 | if (strpos($name, '?')) { |
||
| 920 | $name = substr($name, 1, -2); |
||
| 921 | $optional = true; |
||
| 922 | } else { |
||
| 923 | $name = substr($name, 1, -1); |
||
| 924 | } |
||
| 925 | |||
| 926 | $var[$name] = $optional ? 2 : 1; |
||
| 927 | } |
||
| 928 | } |
||
| 929 | |||
| 930 | return $var; |
||
| 931 | } |
||
| 932 | |||
| 933 | /** |
||
| 934 | * 设置路由参数 |
||
| 935 | * @access public |
||
| 936 | * @param string $method 方法名 |
||
| 937 | * @param array $args 调用参数 |
||
| 938 | * @return $this |
||
| 939 | */ |
||
| 940 | public function __call($method, $args) |
||
| 941 | { |
||
| 942 | if (count($args) > 1) { |
||
| 943 | $args[0] = $args; |
||
| 944 | } |
||
| 945 | array_unshift($args, $method); |
||
| 946 | |||
| 947 | return call_user_func_array([$this, 'option'], $args); |
||
| 948 | } |
||
| 949 | |||
| 950 | public function __sleep() |
||
| 951 | { |
||
| 952 | return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern', 'doAfter']; |
||
| 953 | } |
||
| 954 | |||
| 955 | public function __wakeup() |
||
| 956 | { |
||
| 957 | $this->router = Container::pull('route'); |
||
| 958 | } |
||
| 959 | |||
| 960 | public function __debugInfo() |
||
| 970 | ]; |
||
| 971 | } |
||
| 972 | } |
||
| 973 |