Total Complexity | 129 |
Total Lines | 920 |
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', '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 = []) |
||
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) |
||
387 | } |
||
388 | |||
389 | /** |
||
390 | * 指定路由中间件 |
||
391 | * @access public |
||
392 | * @param string|array|\Closure $middleware 中间件 |
||
393 | * @param mixed $param 参数 |
||
394 | * @return $this |
||
395 | */ |
||
396 | public function middleware($middleware, $param = null) |
||
397 | { |
||
398 | if (is_null($param) && is_array($middleware)) { |
||
399 | $this->option['middleware'] = $middleware; |
||
400 | } else { |
||
401 | foreach ((array) $middleware as $item) { |
||
402 | $this->option['middleware'][] = [$item, $param]; |
||
403 | } |
||
404 | } |
||
405 | |||
406 | return $this; |
||
407 | } |
||
408 | |||
409 | /** |
||
410 | * 设置路由缓存 |
||
411 | * @access public |
||
412 | * @param array|string $cache 缓存 |
||
413 | * @return $this |
||
414 | */ |
||
415 | public function cache($cache) |
||
416 | { |
||
417 | return $this->setOption('cache', $cache); |
||
418 | } |
||
419 | |||
420 | /** |
||
421 | * 检查URL分隔符 |
||
422 | * @access public |
||
423 | * @param string $depr URL分隔符 |
||
424 | * @return $this |
||
425 | */ |
||
426 | public function depr(string $depr) |
||
427 | { |
||
428 | return $this->setOption('param_depr', $depr); |
||
429 | } |
||
430 | |||
431 | /** |
||
432 | * 设置需要合并的路由参数 |
||
433 | * @access public |
||
434 | * @param array $option 路由参数 |
||
435 | * @return $this |
||
436 | */ |
||
437 | public function mergeOptions(array $option = []) |
||
438 | { |
||
439 | $this->mergeOptions = array_merge($this->mergeOptions, $option); |
||
440 | return $this; |
||
441 | } |
||
442 | |||
443 | /** |
||
444 | * 检查是否为HTTPS请求 |
||
445 | * @access public |
||
446 | * @param bool $https 是否为HTTPS |
||
447 | * @return $this |
||
448 | */ |
||
449 | public function https(bool $https = true) |
||
450 | { |
||
451 | return $this->setOption('https', $https); |
||
452 | } |
||
453 | |||
454 | /** |
||
455 | * 检查是否为JSON请求 |
||
456 | * @access public |
||
457 | * @param bool $json 是否为JSON |
||
458 | * @return $this |
||
459 | */ |
||
460 | public function json(bool $json = true) |
||
461 | { |
||
462 | return $this->setOption('json', $json); |
||
463 | } |
||
464 | |||
465 | /** |
||
466 | * 检查是否为AJAX请求 |
||
467 | * @access public |
||
468 | * @param bool $ajax 是否为AJAX |
||
469 | * @return $this |
||
470 | */ |
||
471 | public function ajax(bool $ajax = true) |
||
472 | { |
||
473 | return $this->setOption('ajax', $ajax); |
||
474 | } |
||
475 | |||
476 | /** |
||
477 | * 检查是否为PJAX请求 |
||
478 | * @access public |
||
479 | * @param bool $pjax 是否为PJAX |
||
480 | * @return $this |
||
481 | */ |
||
482 | public function pjax(bool $pjax = true) |
||
483 | { |
||
484 | return $this->setOption('pjax', $pjax); |
||
485 | } |
||
486 | |||
487 | /** |
||
488 | * 当前路由到一个模板地址 当使用数组的时候可以传入模板变量 |
||
489 | * @access public |
||
490 | * @param bool|array $view 视图 |
||
491 | * @return $this |
||
492 | */ |
||
493 | public function view($view = true) |
||
496 | } |
||
497 | |||
498 | /** |
||
499 | * 当前路由为重定向 |
||
500 | * @access public |
||
501 | * @param bool $redirect 是否为重定向 |
||
502 | * @return $this |
||
503 | */ |
||
504 | public function redirect(bool $redirect = true) |
||
505 | { |
||
506 | return $this->setOption('redirect', $redirect); |
||
507 | } |
||
508 | |||
509 | /** |
||
510 | * 设置status |
||
511 | * @access public |
||
512 | * @param int $status 状态码 |
||
513 | * @return $this |
||
514 | */ |
||
515 | public function status(int $status) |
||
516 | { |
||
517 | return $this->setOption('status', $status); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * 设置路由完整匹配 |
||
522 | * @access public |
||
523 | * @param bool $match 是否完整匹配 |
||
524 | * @return $this |
||
525 | */ |
||
526 | public function completeMatch(bool $match = true) |
||
527 | { |
||
528 | return $this->setOption('complete_match', $match); |
||
529 | } |
||
530 | |||
531 | /** |
||
532 | * 是否去除URL最后的斜线 |
||
533 | * @access public |
||
534 | * @param bool $remove 是否去除最后斜线 |
||
535 | * @return $this |
||
536 | */ |
||
537 | public function removeSlash(bool $remove = true) |
||
538 | { |
||
539 | return $this->setOption('remove_slash', $remove); |
||
540 | } |
||
541 | |||
542 | /** |
||
543 | * 设置路由规则全局有效 |
||
544 | * @access public |
||
545 | * @return $this |
||
546 | */ |
||
547 | public function crossDomainRule() |
||
548 | { |
||
549 | if ($this instanceof RuleGroup) { |
||
550 | $method = '*'; |
||
551 | } else { |
||
552 | $method = $this->method; |
||
553 | } |
||
554 | |||
555 | $this->router->setCrossDomainRule($this, $method); |
||
556 | |||
557 | return $this; |
||
558 | } |
||
559 | |||
560 | /** |
||
561 | * 合并分组参数 |
||
562 | * @access public |
||
563 | * @return array |
||
564 | */ |
||
565 | public function mergeGroupOptions(): array |
||
566 | { |
||
567 | $parentOption = $this->parent->getOption(); |
||
568 | // 合并分组参数 |
||
569 | foreach ($this->mergeOptions as $item) { |
||
570 | if (isset($parentOption[$item]) && isset($this->option[$item])) { |
||
571 | $this->option[$item] = array_merge($parentOption[$item], $this->option[$item]); |
||
572 | } |
||
573 | } |
||
574 | |||
575 | $this->option = array_merge($parentOption, $this->option); |
||
576 | |||
577 | return $this->option; |
||
578 | } |
||
579 | |||
580 | /** |
||
581 | * 解析匹配到的规则路由 |
||
582 | * @access public |
||
583 | * @param Request $request 请求对象 |
||
584 | * @param string $rule 路由规则 |
||
585 | * @param mixed $route 路由地址 |
||
586 | * @param string $url URL地址 |
||
587 | * @param array $option 路由参数 |
||
588 | * @param array $matches 匹配的变量 |
||
589 | * @return Dispatch |
||
590 | */ |
||
591 | public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch |
||
592 | { |
||
593 | if (is_string($route) && isset($option['prefix'])) { |
||
594 | // 路由地址前缀 |
||
595 | $route = $option['prefix'] . $route; |
||
596 | } |
||
597 | |||
598 | // 替换路由地址中的变量 |
||
599 | if (is_string($route) && !empty($matches)) { |
||
600 | $search = $replace = []; |
||
601 | |||
602 | foreach ($matches as $key => $value) { |
||
603 | $search[] = '<' . $key . '>'; |
||
604 | $replace[] = $value; |
||
605 | |||
606 | $search[] = ':' . $key; |
||
607 | $replace[] = $value; |
||
608 | } |
||
609 | |||
610 | $route = str_replace($search, $replace, $route); |
||
611 | } |
||
612 | |||
613 | // 解析额外参数 |
||
614 | $count = substr_count($rule, '/'); |
||
615 | $url = array_slice(explode('|', $url), $count + 1); |
||
616 | $this->parseUrlParams(implode('|', $url), $matches); |
||
617 | |||
618 | $this->route = $route; |
||
619 | $this->vars = $matches; |
||
620 | $this->option = $option; |
||
621 | $this->doAfter = true; |
||
622 | |||
623 | // 发起路由调度 |
||
624 | return $this->dispatch($request, $route, $option); |
||
625 | } |
||
626 | |||
627 | /** |
||
628 | * 发起路由调度 |
||
629 | * @access protected |
||
630 | * @param Request $request Request对象 |
||
631 | * @param mixed $route 路由地址 |
||
632 | * @param array $option 路由参数 |
||
633 | * @return Dispatch |
||
634 | */ |
||
635 | protected function dispatch(Request $request, $route, array $option): Dispatch |
||
636 | { |
||
637 | if ($route instanceof Dispatch) { |
||
638 | $result = $route; |
||
639 | } elseif ($route instanceof \Closure) { |
||
640 | // 执行闭包 |
||
641 | $result = new CallbackDispatch($request, $this, $route); |
||
642 | } elseif ($route instanceof Response) { |
||
643 | $result = new ResponseDispatch($request, $this, $route); |
||
644 | } elseif (isset($option['view']) && false !== $option['view']) { |
||
645 | $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []); |
||
646 | } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) { |
||
647 | // 路由到重定向地址 |
||
648 | $result = new RedirectDispatch($request, $this, $route, [], $option['status'] ?? 301); |
||
649 | } elseif (false !== strpos($route, '\\')) { |
||
650 | // 路由到类的方法 |
||
651 | $result = $this->dispatchMethod($request, $route); |
||
652 | } else { |
||
653 | // 路由到控制器/操作 |
||
654 | $result = $this->dispatchController($request, $route); |
||
655 | } |
||
656 | |||
657 | return $result; |
||
658 | } |
||
659 | |||
660 | /** |
||
661 | * 解析URL地址为 模块/控制器/操作 |
||
662 | * @access protected |
||
663 | * @param Request $request Request对象 |
||
664 | * @param string $route 路由地址 |
||
665 | * @return CallbackDispatch |
||
666 | */ |
||
667 | protected function dispatchMethod(Request $request, string $route): CallbackDispatch |
||
675 | } |
||
676 | |||
677 | /** |
||
678 | * 解析URL地址为 模块/控制器/操作 |
||
679 | * @access protected |
||
680 | * @param Request $request Request对象 |
||
681 | * @param string $route 路由地址 |
||
682 | * @return ControllerDispatch |
||
683 | */ |
||
684 | protected function dispatchController(Request $request, string $route): ControllerDispatch |
||
685 | { |
||
686 | list($path, $var) = $this->parseUrlPath($route); |
||
687 | |||
688 | $action = array_pop($path); |
||
689 | $controller = !empty($path) ? array_pop($path) : null; |
||
690 | |||
691 | // 路由到模块/控制器/操作 |
||
692 | return new ControllerDispatch($request, $this, [$controller, $action], $var); |
||
693 | } |
||
694 | |||
695 | /** |
||
696 | * 路由检查 |
||
697 | * @access protected |
||
698 | * @param array $option 路由参数 |
||
699 | * @param Request $request Request对象 |
||
700 | * @return bool |
||
701 | */ |
||
702 | protected function checkOption(array $option, Request $request): bool |
||
703 | { |
||
704 | // 请求类型检测 |
||
705 | if (!empty($option['method'])) { |
||
706 | if (is_string($option['method']) && false === stripos($option['method'], $request->method())) { |
||
707 | return false; |
||
708 | } |
||
709 | } |
||
710 | |||
711 | // AJAX PJAX 请求检查 |
||
712 | foreach (['ajax', 'pjax', 'json'] as $item) { |
||
713 | if (isset($option[$item])) { |
||
714 | $call = 'is' . $item; |
||
715 | if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) { |
||
716 | return false; |
||
717 | } |
||
718 | } |
||
719 | } |
||
720 | |||
721 | // 伪静态后缀检测 |
||
722 | if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|')) |
||
723 | || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) { |
||
724 | return false; |
||
725 | } |
||
726 | |||
727 | // 域名检查 |
||
728 | if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) { |
||
729 | return false; |
||
730 | } |
||
731 | |||
732 | // HTTPS检查 |
||
733 | if ((isset($option['https']) && $option['https'] && !$request->isSsl()) |
||
734 | || (isset($option['https']) && !$option['https'] && $request->isSsl())) { |
||
735 | return false; |
||
736 | } |
||
737 | |||
738 | // 请求参数检查 |
||
739 | if (isset($option['filter'])) { |
||
740 | foreach ($option['filter'] as $name => $value) { |
||
741 | if ($request->param($name, '', null) != $value) { |
||
742 | return false; |
||
743 | } |
||
744 | } |
||
745 | } |
||
746 | |||
747 | return true; |
||
748 | } |
||
749 | |||
750 | /** |
||
751 | * 解析URL地址中的参数Request对象 |
||
752 | * @access protected |
||
753 | * @param string $rule 路由规则 |
||
754 | * @param array $var 变量 |
||
755 | * @return void |
||
756 | */ |
||
757 | protected function parseUrlParams(string $url, array &$var = []): void |
||
758 | { |
||
759 | if ($url) { |
||
760 | preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) { |
||
761 | $var[$match[1]] = strip_tags($match[2]); |
||
762 | }, $url); |
||
763 | } |
||
764 | } |
||
765 | |||
766 | /** |
||
767 | * 解析URL的pathinfo参数和变量 |
||
768 | * @access public |
||
769 | * @param string $url URL地址 |
||
770 | * @return array |
||
771 | */ |
||
772 | public function parseUrlPath(string $url): array |
||
773 | { |
||
774 | // 分隔符替换 确保路由定义使用统一的分隔符 |
||
775 | $url = str_replace('|', '/', $url); |
||
776 | $url = trim($url, '/'); |
||
777 | $var = []; |
||
778 | |||
779 | if (false !== strpos($url, '?')) { |
||
780 | // [控制器/操作?]参数1=值1&参数2=值2... |
||
781 | $info = parse_url($url); |
||
782 | $path = explode('/', $info['path']); |
||
783 | parse_str($info['query'], $var); |
||
784 | } elseif (strpos($url, '/')) { |
||
785 | // [控制器/操作] |
||
786 | $path = explode('/', $url); |
||
787 | } elseif (false !== strpos($url, '=')) { |
||
788 | // 参数1=值1&参数2=值2... |
||
789 | parse_str($url, $var); |
||
790 | $path = []; |
||
791 | } else { |
||
792 | $path = [$url]; |
||
793 | } |
||
794 | |||
795 | return [$path, $var]; |
||
796 | } |
||
797 | |||
798 | /** |
||
799 | * 生成路由的正则规则 |
||
800 | * @access protected |
||
801 | * @param string $rule 路由规则 |
||
802 | * @param array $match 匹配的变量 |
||
803 | * @param array $pattern 路由变量规则 |
||
804 | * @param array $option 路由参数 |
||
805 | * @param bool $completeMatch 路由是否完全匹配 |
||
806 | * @param string $suffix 路由正则变量后缀 |
||
807 | * @return string |
||
808 | */ |
||
809 | protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string |
||
833 | } |
||
834 | |||
835 | /** |
||
836 | * 生成路由变量的正则规则 |
||
837 | * @access protected |
||
838 | * @param string $name 路由变量 |
||
839 | * @param array $pattern 变量规则 |
||
840 | * @param string $suffix 路由正则变量后缀 |
||
841 | * @return string |
||
842 | */ |
||
843 | protected function buildNameRegex(string $name, array $pattern, string $suffix): string |
||
844 | { |
||
845 | $optional = ''; |
||
846 | $slash = substr($name, 0, 1); |
||
847 | |||
848 | if (in_array($slash, ['/', '-'])) { |
||
849 | $prefix = '\\' . $slash; |
||
850 | $name = substr($name, 1); |
||
851 | $slash = substr($name, 0, 1); |
||
852 | } else { |
||
853 | $prefix = ''; |
||
854 | } |
||
855 | |||
856 | if ('<' != $slash) { |
||
857 | return $prefix . preg_quote($name, '/'); |
||
858 | } |
||
859 | |||
860 | if (strpos($name, '?')) { |
||
861 | $name = substr($name, 1, -2); |
||
862 | $optional = '?'; |
||
863 | } elseif (strpos($name, '>')) { |
||
864 | $name = substr($name, 1, -1); |
||
865 | } |
||
866 | |||
867 | if (isset($pattern[$name])) { |
||
868 | $nameRule = $pattern[$name]; |
||
869 | if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) { |
||
870 | $nameRule = substr($nameRule, 1, -1); |
||
871 | } |
||
872 | } else { |
||
873 | $nameRule = $this->router->config('default_route_pattern'); |
||
874 | } |
||
875 | |||
876 | return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional; |
||
877 | } |
||
878 | |||
879 | /** |
||
880 | * 分析路由规则中的变量 |
||
881 | * @access protected |
||
882 | * @param string $rule 路由规则 |
||
883 | * @return array |
||
884 | */ |
||
885 | protected function parseVar(string $rule): array |
||
886 | { |
||
887 | // 提取路由规则中的变量 |
||
888 | $var = []; |
||
889 | |||
890 | if (preg_match_all('/<\w+\??>/', $rule, $matches)) { |
||
891 | foreach ($matches[0] as $name) { |
||
892 | $optional = false; |
||
893 | |||
894 | if (strpos($name, '?')) { |
||
895 | $name = substr($name, 1, -2); |
||
896 | $optional = true; |
||
897 | } else { |
||
898 | $name = substr($name, 1, -1); |
||
899 | } |
||
900 | |||
901 | $var[$name] = $optional ? 2 : 1; |
||
902 | } |
||
903 | } |
||
904 | |||
905 | return $var; |
||
906 | } |
||
907 | |||
908 | /** |
||
909 | * 设置路由参数 |
||
910 | * @access public |
||
911 | * @param string $method 方法名 |
||
912 | * @param array $args 调用参数 |
||
913 | * @return $this |
||
914 | */ |
||
915 | public function __call($method, $args) |
||
916 | { |
||
917 | if (count($args) > 1) { |
||
918 | $args[0] = $args; |
||
919 | } |
||
920 | array_unshift($args, $method); |
||
921 | |||
922 | return call_user_func_array([$this, 'option'], $args); |
||
923 | } |
||
924 | |||
925 | public function __sleep() |
||
926 | { |
||
927 | return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern', 'doAfter']; |
||
928 | } |
||
929 | |||
930 | public function __wakeup() |
||
931 | { |
||
932 | $this->router = Container::pull('route'); |
||
933 | } |
||
934 | |||
935 | public function __debugInfo() |
||
945 | ]; |
||
946 | } |
||
947 | } |
||
948 |