Total Complexity | 138 |
Total Lines | 982 |
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) |
||
346 | { |
||
347 | if ($var instanceof \Closure) { |
||
348 | $this->option['model'][] = $var; |
||
349 | } elseif (is_array($var)) { |
||
350 | $this->option['model'] = $var; |
||
351 | } elseif (is_null($model)) { |
||
352 | $this->option['model']['id'] = [$var, true]; |
||
353 | } else { |
||
354 | $this->option['model'][$var] = [$model, $exception]; |
||
355 | } |
||
356 | |||
357 | return $this; |
||
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) |
||
452 | { |
||
453 | return $this->setOption('param_depr', $depr); |
||
454 | } |
||
455 | |||
456 | /** |
||
457 | * 设置需要合并的路由参数 |
||
458 | * @access public |
||
459 | * @param array $option 路由参数 |
||
460 | * @return $this |
||
461 | */ |
||
462 | public function mergeOptions(array $option = []) |
||
463 | { |
||
464 | $this->mergeOptions = array_merge($this->mergeOptions, $option); |
||
465 | return $this; |
||
466 | } |
||
467 | |||
468 | /** |
||
469 | * 检查是否为HTTPS请求 |
||
470 | * @access public |
||
471 | * @param bool $https 是否为HTTPS |
||
472 | * @return $this |
||
473 | */ |
||
474 | public function https(bool $https = true) |
||
475 | { |
||
476 | return $this->setOption('https', $https); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * 检查是否为AJAX请求 |
||
481 | * @access public |
||
482 | * @param bool $ajax 是否为AJAX |
||
483 | * @return $this |
||
484 | */ |
||
485 | public function ajax(bool $ajax = true) |
||
486 | { |
||
487 | return $this->setOption('ajax', $ajax); |
||
488 | } |
||
489 | |||
490 | /** |
||
491 | * 检查是否为PJAX请求 |
||
492 | * @access public |
||
493 | * @param bool $pjax 是否为PJAX |
||
494 | * @return $this |
||
495 | */ |
||
496 | public function pjax(bool $pjax = true) |
||
497 | { |
||
498 | return $this->setOption('pjax', $pjax); |
||
499 | } |
||
500 | |||
501 | /** |
||
502 | * 当前路由到一个模板地址 当使用数组的时候可以传入模板变量 |
||
503 | * @access public |
||
504 | * @param bool|array $view 视图 |
||
505 | * @return $this |
||
506 | */ |
||
507 | public function view($view = true) |
||
510 | } |
||
511 | |||
512 | /** |
||
513 | * 当前路由为重定向 |
||
514 | * @access public |
||
515 | * @param bool $redirect 是否为重定向 |
||
516 | * @return $this |
||
517 | */ |
||
518 | public function redirect(bool $redirect = true) |
||
519 | { |
||
520 | return $this->setOption('redirect', $redirect); |
||
521 | } |
||
522 | |||
523 | /** |
||
524 | * 设置status |
||
525 | * @access public |
||
526 | * @param int $status 状态码 |
||
527 | * @return $this |
||
528 | */ |
||
529 | public function status(int $status) |
||
530 | { |
||
531 | return $this->setOption('status', $status); |
||
532 | } |
||
533 | |||
534 | /** |
||
535 | * 设置路由完整匹配 |
||
536 | * @access public |
||
537 | * @param bool $match 是否完整匹配 |
||
538 | * @return $this |
||
539 | */ |
||
540 | public function completeMatch(bool $match = true) |
||
541 | { |
||
542 | return $this->setOption('complete_match', $match); |
||
543 | } |
||
544 | |||
545 | /** |
||
546 | * 是否去除URL最后的斜线 |
||
547 | * @access public |
||
548 | * @param bool $remove 是否去除最后斜线 |
||
549 | * @return $this |
||
550 | */ |
||
551 | public function removeSlash(bool $remove = true) |
||
552 | { |
||
553 | return $this->setOption('remove_slash', $remove); |
||
554 | } |
||
555 | |||
556 | /** |
||
557 | * 设置是否允许跨域 |
||
558 | * @access public |
||
559 | * @param bool $allow 是否允许跨域 |
||
560 | * @param array $header 头信息 |
||
561 | * @return $this |
||
562 | */ |
||
563 | public function allowCrossDomain(bool $allow = true, array $header = []) |
||
564 | { |
||
565 | if (!empty($header)) { |
||
566 | $this->header($header); |
||
567 | } |
||
568 | |||
569 | if ($allow && $this->parent) { |
||
570 | $this->parent->addRuleItem($this, 'options'); |
||
571 | } |
||
572 | |||
573 | return $this->setOption('cross_domain', $allow); |
||
574 | } |
||
575 | |||
576 | /** |
||
577 | * 检查OPTIONS请求 |
||
578 | * @access public |
||
579 | * @param Request $request 当前请求对象 |
||
580 | * @return Dispatch|void |
||
581 | */ |
||
582 | protected function checkCrossDomain(Request $request) |
||
583 | { |
||
584 | if (!empty($this->option['cross_domain'])) { |
||
585 | |||
586 | $header = [ |
||
587 | 'Access-Control-Allow-Origin' => '*', |
||
588 | 'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE', |
||
589 | 'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With', |
||
590 | ]; |
||
591 | |||
592 | if (!empty($this->option['header'])) { |
||
593 | $header = array_merge($header, $this->option['header']); |
||
594 | } |
||
595 | |||
596 | $this->option['header'] = $header; |
||
597 | |||
598 | if ($request->method(true) == 'OPTIONS') { |
||
599 | return new ResponseDispatch($request, $this, Response::create()->code(204)->header($header)); |
||
600 | } |
||
601 | } |
||
602 | } |
||
603 | |||
604 | /** |
||
605 | * 设置路由规则全局有效 |
||
606 | * @access public |
||
607 | * @return $this |
||
608 | */ |
||
609 | public function crossDomainRule() |
||
610 | { |
||
611 | if ($this instanceof RuleGroup) { |
||
612 | $method = '*'; |
||
613 | } else { |
||
614 | $method = $this->method; |
||
615 | } |
||
616 | |||
617 | $this->router->setCrossDomainRule($this, $method); |
||
618 | |||
619 | return $this; |
||
620 | } |
||
621 | |||
622 | /** |
||
623 | * 合并分组参数 |
||
624 | * @access public |
||
625 | * @return array |
||
626 | */ |
||
627 | public function mergeGroupOptions(): array |
||
628 | { |
||
629 | $parentOption = $this->parent->getOption(); |
||
630 | // 合并分组参数 |
||
631 | foreach ($this->mergeOptions as $item) { |
||
632 | if (isset($parentOption[$item]) && isset($this->option[$item])) { |
||
633 | $this->option[$item] = array_merge($parentOption[$item], $this->option[$item]); |
||
634 | } |
||
635 | } |
||
636 | |||
637 | $this->option = array_merge($parentOption, $this->option); |
||
638 | |||
639 | return $this->option; |
||
640 | } |
||
641 | |||
642 | /** |
||
643 | * 解析匹配到的规则路由 |
||
644 | * @access public |
||
645 | * @param Request $request 请求对象 |
||
646 | * @param string $rule 路由规则 |
||
647 | * @param mixed $route 路由地址 |
||
648 | * @param string $url URL地址 |
||
649 | * @param array $option 路由参数 |
||
650 | * @param array $matches 匹配的变量 |
||
651 | * @return Dispatch |
||
652 | */ |
||
653 | public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch |
||
654 | { |
||
655 | if (is_string($route) && isset($option['prefix'])) { |
||
656 | // 路由地址前缀 |
||
657 | $route = $option['prefix'] . $route; |
||
658 | } |
||
659 | |||
660 | // 替换路由地址中的变量 |
||
661 | if (is_string($route) && !empty($matches)) { |
||
662 | $search = $replace = []; |
||
663 | |||
664 | foreach ($matches as $key => $value) { |
||
665 | $search[] = '<' . $key . '>'; |
||
666 | $replace[] = $value; |
||
667 | |||
668 | $search[] = ':' . $key; |
||
669 | $replace[] = $value; |
||
670 | } |
||
671 | |||
672 | $route = str_replace($search, $replace, $route); |
||
673 | } |
||
674 | |||
675 | // 解析额外参数 |
||
676 | $count = substr_count($rule, '/'); |
||
677 | $url = array_slice(explode('|', $url), $count + 1); |
||
678 | $this->parseUrlParams(implode('|', $url), $matches); |
||
679 | |||
680 | $this->route = $route; |
||
681 | $this->vars = $matches; |
||
682 | $this->option = $option; |
||
683 | $this->doAfter = true; |
||
684 | |||
685 | // 发起路由调度 |
||
686 | return $this->dispatch($request, $route, $option); |
||
687 | } |
||
688 | |||
689 | /** |
||
690 | * 发起路由调度 |
||
691 | * @access protected |
||
692 | * @param Request $request Request对象 |
||
693 | * @param mixed $route 路由地址 |
||
694 | * @param array $option 路由参数 |
||
695 | * @return Dispatch |
||
696 | */ |
||
697 | protected function dispatch(Request $request, $route, array $option): Dispatch |
||
698 | { |
||
699 | if ($route instanceof Dispatch) { |
||
700 | $result = $route; |
||
701 | } elseif ($route instanceof \Closure) { |
||
702 | // 执行闭包 |
||
703 | $result = new CallbackDispatch($request, $this, $route); |
||
704 | } elseif ($route instanceof Response) { |
||
705 | $result = new ResponseDispatch($request, $this, $route); |
||
706 | } elseif (isset($option['view']) && false !== $option['view']) { |
||
707 | $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []); |
||
708 | } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) { |
||
709 | // 路由到重定向地址 |
||
710 | $result = new RedirectDispatch($request, $this, $route, [], $option['status'] ?? 301); |
||
711 | } elseif (false !== strpos($route, '\\')) { |
||
712 | // 路由到类的方法 |
||
713 | $result = $this->dispatchMethod($request, $route); |
||
714 | } else { |
||
715 | // 路由到控制器/操作 |
||
716 | $result = $this->dispatchController($request, $route); |
||
717 | } |
||
718 | |||
719 | return $result; |
||
720 | } |
||
721 | |||
722 | /** |
||
723 | * 解析URL地址为 模块/控制器/操作 |
||
724 | * @access protected |
||
725 | * @param Request $request Request对象 |
||
726 | * @param string $route 路由地址 |
||
727 | * @return CallbackDispatch |
||
728 | */ |
||
729 | protected function dispatchMethod(Request $request, string $route): CallbackDispatch |
||
737 | } |
||
738 | |||
739 | /** |
||
740 | * 解析URL地址为 模块/控制器/操作 |
||
741 | * @access protected |
||
742 | * @param Request $request Request对象 |
||
743 | * @param string $route 路由地址 |
||
744 | * @return ControllerDispatch |
||
745 | */ |
||
746 | protected function dispatchController(Request $request, string $route): ControllerDispatch |
||
747 | { |
||
748 | list($path, $var) = $this->parseUrlPath($route); |
||
749 | |||
750 | $action = array_pop($path); |
||
751 | $controller = !empty($path) ? array_pop($path) : null; |
||
752 | |||
753 | // 路由到模块/控制器/操作 |
||
754 | return new ControllerDispatch($request, $this, [$controller, $action], $var); |
||
755 | } |
||
756 | |||
757 | /** |
||
758 | * 路由检查 |
||
759 | * @access protected |
||
760 | * @param array $option 路由参数 |
||
761 | * @param Request $request Request对象 |
||
762 | * @return bool |
||
763 | */ |
||
764 | protected function checkOption(array $option, Request $request): bool |
||
765 | { |
||
766 | // 请求类型检测 |
||
767 | if (!empty($option['method'])) { |
||
768 | if (is_string($option['method']) && false === stripos($option['method'], $request->method())) { |
||
769 | return false; |
||
770 | } |
||
771 | } |
||
772 | |||
773 | // AJAX PJAX 请求检查 |
||
774 | foreach (['ajax', 'pjax'] as $item) { |
||
775 | if (isset($option[$item])) { |
||
776 | $call = 'is' . $item; |
||
777 | if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) { |
||
778 | return false; |
||
779 | } |
||
780 | } |
||
781 | } |
||
782 | |||
783 | // 伪静态后缀检测 |
||
784 | if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|')) |
||
785 | || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) { |
||
786 | return false; |
||
787 | } |
||
788 | |||
789 | // 域名检查 |
||
790 | if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) { |
||
791 | return false; |
||
792 | } |
||
793 | |||
794 | // HTTPS检查 |
||
795 | if ((isset($option['https']) && $option['https'] && !$request->isSsl()) |
||
796 | || (isset($option['https']) && !$option['https'] && $request->isSsl())) { |
||
797 | return false; |
||
798 | } |
||
799 | |||
800 | // 请求参数检查 |
||
801 | if (isset($option['filter'])) { |
||
802 | foreach ($option['filter'] as $name => $value) { |
||
803 | if ($request->param($name, '', null) != $value) { |
||
804 | return false; |
||
805 | } |
||
806 | } |
||
807 | } |
||
808 | |||
809 | return true; |
||
810 | } |
||
811 | |||
812 | /** |
||
813 | * 解析URL地址中的参数Request对象 |
||
814 | * @access protected |
||
815 | * @param string $rule 路由规则 |
||
816 | * @param array $var 变量 |
||
817 | * @return void |
||
818 | */ |
||
819 | protected function parseUrlParams(string $url, array &$var = []): void |
||
820 | { |
||
821 | if ($url) { |
||
822 | preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) { |
||
823 | $var[$match[1]] = strip_tags($match[2]); |
||
824 | }, $url); |
||
825 | } |
||
826 | } |
||
827 | |||
828 | /** |
||
829 | * 解析URL的pathinfo参数和变量 |
||
830 | * @access public |
||
831 | * @param string $url URL地址 |
||
832 | * @return array |
||
833 | */ |
||
834 | public function parseUrlPath(string $url): array |
||
835 | { |
||
836 | // 分隔符替换 确保路由定义使用统一的分隔符 |
||
837 | $url = str_replace('|', '/', $url); |
||
838 | $url = trim($url, '/'); |
||
839 | $var = []; |
||
840 | |||
841 | if (false !== strpos($url, '?')) { |
||
842 | // [控制器/操作?]参数1=值1&参数2=值2... |
||
843 | $info = parse_url($url); |
||
844 | $path = explode('/', $info['path']); |
||
845 | parse_str($info['query'], $var); |
||
846 | } elseif (strpos($url, '/')) { |
||
847 | // [控制器/操作] |
||
848 | $path = explode('/', $url); |
||
849 | } elseif (false !== strpos($url, '=')) { |
||
850 | // 参数1=值1&参数2=值2... |
||
851 | parse_str($url, $var); |
||
852 | $path = []; |
||
853 | } else { |
||
854 | $path = [$url]; |
||
855 | } |
||
856 | |||
857 | return [$path, $var]; |
||
858 | } |
||
859 | |||
860 | /** |
||
861 | * 生成路由的正则规则 |
||
862 | * @access protected |
||
863 | * @param string $rule 路由规则 |
||
864 | * @param array $match 匹配的变量 |
||
865 | * @param array $pattern 路由变量规则 |
||
866 | * @param array $option 路由参数 |
||
867 | * @param bool $completeMatch 路由是否完全匹配 |
||
868 | * @param string $suffix 路由正则变量后缀 |
||
869 | * @return string |
||
870 | */ |
||
871 | protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string |
||
895 | } |
||
896 | |||
897 | /** |
||
898 | * 生成路由变量的正则规则 |
||
899 | * @access protected |
||
900 | * @param string $name 路由变量 |
||
901 | * @param array $pattern 变量规则 |
||
902 | * @param string $suffix 路由正则变量后缀 |
||
903 | * @return string |
||
904 | */ |
||
905 | protected function buildNameRegex(string $name, array $pattern, string $suffix): string |
||
906 | { |
||
907 | $optional = ''; |
||
908 | $slash = substr($name, 0, 1); |
||
909 | |||
910 | if (in_array($slash, ['/', '-'])) { |
||
911 | $prefix = '\\' . $slash; |
||
912 | $name = substr($name, 1); |
||
913 | $slash = substr($name, 0, 1); |
||
914 | } else { |
||
915 | $prefix = ''; |
||
916 | } |
||
917 | |||
918 | if ('<' != $slash) { |
||
919 | return $prefix . preg_quote($name, '/'); |
||
920 | } |
||
921 | |||
922 | if (strpos($name, '?')) { |
||
923 | $name = substr($name, 1, -2); |
||
924 | $optional = '?'; |
||
925 | } elseif (strpos($name, '>')) { |
||
926 | $name = substr($name, 1, -1); |
||
927 | } |
||
928 | |||
929 | if (isset($pattern[$name])) { |
||
930 | $nameRule = $pattern[$name]; |
||
931 | if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) { |
||
932 | $nameRule = substr($nameRule, 1, -1); |
||
933 | } |
||
934 | } else { |
||
935 | $nameRule = $this->router->config('default_route_pattern'); |
||
936 | } |
||
937 | |||
938 | return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional; |
||
939 | } |
||
940 | |||
941 | /** |
||
942 | * 分析路由规则中的变量 |
||
943 | * @access protected |
||
944 | * @param string $rule 路由规则 |
||
945 | * @return array |
||
946 | */ |
||
947 | protected function parseVar(string $rule): array |
||
948 | { |
||
949 | // 提取路由规则中的变量 |
||
950 | $var = []; |
||
951 | |||
952 | if (preg_match_all('/<\w+\??>/', $rule, $matches)) { |
||
953 | foreach ($matches[0] as $name) { |
||
954 | $optional = false; |
||
955 | |||
956 | if (strpos($name, '?')) { |
||
957 | $name = substr($name, 1, -2); |
||
958 | $optional = true; |
||
959 | } else { |
||
960 | $name = substr($name, 1, -1); |
||
961 | } |
||
962 | |||
963 | $var[$name] = $optional ? 2 : 1; |
||
964 | } |
||
965 | } |
||
966 | |||
967 | return $var; |
||
968 | } |
||
969 | |||
970 | /** |
||
971 | * 设置路由参数 |
||
972 | * @access public |
||
973 | * @param string $method 方法名 |
||
974 | * @param array $args 调用参数 |
||
975 | * @return $this |
||
976 | */ |
||
977 | public function __call($method, $args) |
||
978 | { |
||
979 | if (count($args) > 1) { |
||
980 | $args[0] = $args; |
||
981 | } |
||
982 | array_unshift($args, $method); |
||
983 | |||
984 | return call_user_func_array([$this, 'option'], $args); |
||
985 | } |
||
986 | |||
987 | public function __sleep() |
||
988 | { |
||
989 | return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern', 'doAfter']; |
||
990 | } |
||
991 | |||
992 | public function __wakeup() |
||
995 | } |
||
996 | |||
997 | public function __debugInfo() |
||
998 | { |
||
999 | return [ |
||
1000 | 'name' => $this->name, |
||
1001 | 'rule' => $this->rule, |
||
1002 | 'route' => $this->route, |
||
1003 | 'method' => $this->method, |
||
1004 | 'vars' => $this->vars, |
||
1005 | 'option' => $this->option, |
||
1006 | 'pattern' => $this->pattern, |
||
1007 | ]; |
||
1008 | } |
||
1009 | } |
||
1010 |