Passed
Push — 8.0 ( 846432...ae3f71 )
by liu
02:10
created

Rule::option()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
ccs 0
cts 3
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2023 http://thinkphp.cn All rights reserved.
6
// +----------------------------------------------------------------------
7
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
8
// +----------------------------------------------------------------------
9
// | Author: liu21st <[email protected]>
10
// +----------------------------------------------------------------------
11
declare (strict_types = 1);
12
13
namespace think\route;
14
15
use Closure;
16
use think\Container;
17
use think\middleware\AllowCrossDomain;
18
use think\middleware\CheckRequestCache;
19
use think\middleware\FormTokenCheck;
20
use think\Request;
21
use think\Route;
22
use think\route\dispatch\Callback as CallbackDispatch;
23
use think\route\dispatch\Controller as ControllerDispatch;
24
25
/**
26
 * 路由规则基础类
27
 */
28
abstract class Rule
29
{
30
    /**
31
     * 路由标识
32
     * @var string
33
     */
34
    protected $name;
35
36
    /**
37
     * 所在域名
38
     * @var string
39
     */
40
    protected $domain;
41
42
    /**
43
     * 路由对象
44
     * @var Route
45
     */
46
    protected $router;
47
48
    /**
49
     * 路由所属分组
50
     * @var RuleGroup
51
     */
52
    protected $parent;
53
54
    /**
55
     * 路由规则
56
     * @var mixed
57
     */
58
    protected $rule;
59
60
    /**
61
     * 路由地址
62
     * @var string|Closure
63
     */
64
    protected $route;
65
66
    /**
67
     * 请求类型
68
     * @var string
69
     */
70
    protected $method = '*';
71
72
    /**
73
     * 路由变量
74
     * @var array
75
     */
76
    protected $vars = [];
77
78
    /**
79
     * 路由参数
80
     * @var array
81
     */
82
    protected $option = [];
83
84
    /**
85
     * 路由变量规则
86
     * @var array
87
     */
88
    protected $pattern = [];
89
90
    /**
91
     * 需要和分组合并的路由参数
92
     * @var array
93
     */
94
    protected $mergeOptions = ['model', 'append', 'middleware'];
95
96
    abstract public function check(Request $request, string $url, bool $completeMatch = false);
97
98
    /**
99
     * 设置路由参数
100
     * @access public
101
     * @param  array $option 参数
102
     * @return $this
103
     */
104
    public function option(array $option)
105
    {
106
        $this->option = array_merge($this->option, $option);
107
108
        return $this;
109
    }
110
111
    /**
112
     * 设置单个路由参数
113
     * @access public
114
     * @param  string $name  参数名
115
     * @param  mixed  $value 值
116
     * @return $this
117
     */
118 27
    public function setOption(string $name, $value)
119
    {
120 27
        $this->option[$name] = $value;
121
122 27
        return $this;
123
    }
124
125
    /**
126
     * 注册变量规则
127
     * @access public
128
     * @param  array $pattern 变量规则
129
     * @return $this
130
     */
131
    public function pattern(array $pattern)
132
    {
133
        $this->pattern = array_merge($this->pattern, $pattern);
134
135
        return $this;
136
    }
137
138
    /**
139
     * 设置标识
140
     * @access public
141
     * @param  string $name 标识名
142
     * @return $this
143
     */
144
    public function name(string $name)
145
    {
146
        $this->name = $name;
147
148
        return $this;
149
    }
150
151
    /**
152
     * 获取路由对象
153
     * @access public
154
     * @return Route
155
     */
156 3
    public function getRouter(): Route
157
    {
158 3
        return $this->router;
159
    }
160
161
    /**
162
     * 获取Name
163
     * @access public
164
     * @return string
165
     */
166
    public function getName(): string
167
    {
168
        return $this->name ?: '';
169
    }
170
171
    /**
172
     * 获取当前路由规则
173
     * @access public
174
     * @return mixed
175
     */
176 9
    public function getRule()
177
    {
178 9
        return $this->rule;
179
    }
180
181
    /**
182
     * 获取当前路由地址
183
     * @access public
184
     * @return mixed
185
     */
186 27
    public function getRoute()
187
    {
188 27
        return $this->route;
189
    }
190
191
    /**
192
     * 获取当前路由的变量
193
     * @access public
194
     * @return array
195
     */
196 3
    public function getVars(): array
197
    {
198 3
        return $this->vars;
199
    }
200
201
    /**
202
     * 获取Parent对象
203
     * @access public
204
     * @return $this|null
205
     */
206
    public function getParent()
207
    {
208
        return $this->parent;
209
    }
210
211
    /**
212
     * 获取路由所在域名
213
     * @access public
214
     * @return string
215
     */
216 9
    public function getDomain(): string
217
    {
218 9
        return $this->domain ?: $this->parent->getDomain();
219
    }
220
221
    /**
222
     * 获取路由参数
223
     * @access public
224
     * @param  string $name 变量名
225
     * @return mixed
226
     */
227 27
    public function config(string $name = '')
228
    {
229 27
        return $this->router->config($name);
230
    }
231
232
    /**
233
     * 获取变量规则定义
234
     * @access public
235
     * @param  string $name 变量名
236
     * @return mixed
237
     */
238 24
    public function getPattern(string $name = '')
239
    {
240 24
        $pattern = $this->pattern;
241
242 24
        if ($this->parent) {
243 24
            $pattern = array_merge($this->parent->getPattern(), $pattern);
244
        }
245
246 24
        if ('' === $name) {
247 24
            return $pattern;
248
        }
249
250
        return $pattern[$name] ?? null;
251
    }
252
253
    /**
254
     * 获取路由参数定义
255
     * @access public
256
     * @param  string $name 参数名
257
     * @param  mixed  $default 默认值
258
     * @return mixed
259
     */
260 27
    public function getOption(string $name = '', $default = null)
261
    {
262 27
        $option = $this->option;
263
264 27
        if ($this->parent) {
265 24
            $parentOption = $this->parent->getOption();
266
267
            // 合并分组参数
268 24
            foreach ($this->mergeOptions as $item) {
269 24
                if (isset($parentOption[$item]) && isset($option[$item])) {
270
                    $option[$item] = array_merge($parentOption[$item], $option[$item]);
271
                }
272
            }
273
274 24
            $option = array_merge($parentOption, $option);
275
        }
276
277 27
        if ('' === $name) {
278 27
            return $option;
279
        }
280
281 9
        return $option[$name] ?? $default;
282
    }
283
284
    /**
285
     * 获取当前路由的请求类型
286
     * @access public
287
     * @return string
288
     */
289 24
    public function getMethod(): string
290
    {
291 24
        return strtolower($this->method);
292
    }
293
294
    /**
295
     * 设置路由请求类型
296
     * @access public
297
     * @param  string $method 请求类型
298
     * @return $this
299
     */
300
    public function method(string $method)
301
    {
302
        return $this->setOption('method', strtolower($method));
303
    }
304
305
    /**
306
     * 检查后缀
307
     * @access public
308
     * @param  string $ext URL后缀
309
     * @return $this
310
     */
311
    public function ext(string $ext = '')
312
    {
313
        return $this->setOption('ext', $ext);
314
    }
315
316
    /**
317
     * 检查禁止后缀
318
     * @access public
319
     * @param  string $ext URL后缀
320
     * @return $this
321
     */
322
    public function denyExt(string $ext = '')
323
    {
324
        return $this->setOption('deny_ext', $ext);
325
    }
326
327
    /**
328
     * 检查域名
329
     * @access public
330
     * @param  string $domain 域名
331
     * @return $this
332
     */
333
    public function domain(string $domain)
334
    {
335
        $this->domain = $domain;
336
        return $this->setOption('domain', $domain);
337
    }
338
339
    /**
340
     * 是否区分大小写
341
     * @access public
342
     * @param  bool $case 是否区分
343
     * @return $this
344
     */
345
    public function caseUrl(bool $case)
346
    {
347
        return $this->setOption('case_sensitive', $case);
348
    }
349
350
    /**
351
     * 设置参数过滤检查
352
     * @access public
353
     * @param  array $filter 参数过滤
354
     * @return $this
355
     */
356
    public function filter(array $filter)
357
    {
358
        $this->option['filter'] = $filter;
359
360
        return $this;
361
    }
362
363
    /**
364
     * 绑定模型
365
     * @access public
366
     * @param  array|string|Closure $var  路由变量名 多个使用 & 分割
367
     * @param  string|Closure       $model 绑定模型类
368
     * @param  bool                  $exception 是否抛出异常
369
     * @return $this
370
     */
371
    public function model(array | string | Closure $var, string | Closure $model = null, bool $exception = true)
372
    {
373
        if ($var instanceof Closure) {
0 ignored issues
show
introduced by
$var is never a sub-type of Closure.
Loading history...
374
            $this->option['model'][] = $var;
375
        } elseif (is_array($var)) {
0 ignored issues
show
introduced by
The condition is_array($var) is always true.
Loading history...
376
            $this->option['model'] = $var;
377
        } elseif (is_null($model)) {
378
            $this->option['model']['id'] = [$var, true];
379
        } else {
380
            $this->option['model'][$var] = [$model, $exception];
381
        }
382
383
        return $this;
384
    }
385
386
    /**
387
     * 附加路由隐式参数
388
     * @access public
389
     * @param  array $append 追加参数
390
     * @return $this
391
     */
392
    public function append(array $append = [])
393
    {
394
        $this->option['append'] = $append;
395
396
        return $this;
397
    }
398
399
    /**
400
     * 绑定验证
401
     * @access public
402
     * @param  mixed  $validate 验证器类
403
     * @param  string $scene 验证场景
404
     * @param  array  $message 验证提示
405
     * @param  bool   $batch 批量验证
406
     * @return $this
407
     */
408
    public function validate($validate, string $scene = null, array $message = [], bool $batch = false)
409
    {
410
        $this->option['validate'] = [$validate, $scene, $message, $batch];
411
412
        return $this;
413
    }
414
415
    /**
416
     * 指定路由中间件
417
     * @access public
418
     * @param string|array|Closure $middleware 中间件
419
     * @param mixed $params 参数
420
     * @return $this
421
     */
422 3
    public function middleware(string | array | Closure $middleware, ...$params)
423
    {
424 3
        if (empty($params) && is_array($middleware)) {
425
            $this->option['middleware'] = $middleware;
426
        } else {
427 3
            foreach ((array) $middleware as $item) {
428 3
                $this->option['middleware'][] = [$item, $params];
429
            }
430
        }
431
432 3
        return $this;
433
    }
434
435
    /**
436
     * 允许跨域
437
     * @access public
438
     * @param  array $header 自定义Header
439
     * @return $this
440
     */
441 3
    public function allowCrossDomain(array $header = [])
442
    {
443 3
        return $this->middleware(AllowCrossDomain::class, $header);
444
    }
445
446
    /**
447
     * 表单令牌验证
448
     * @access public
449
     * @param  string $token 表单令牌token名称
450
     * @return $this
451
     */
452
    public function token(string $token = '__token__')
453
    {
454
        return $this->middleware(FormTokenCheck::class, $token);
455
    }
456
457
    /**
458
     * 设置路由缓存
459
     * @access public
460
     * @param  array|string|int $cache 缓存
461
     * @return $this
462
     */
463
    public function cache(array | string | int $cache)
464
    {
465
        return $this->middleware(CheckRequestCache::class, $cache);
466
    }
467
468
    /**
469
     * 检查URL分隔符
470
     * @access public
471
     * @param  string $depr URL分隔符
472
     * @return $this
473
     */
474
    public function depr(string $depr)
475
    {
476
        return $this->setOption('param_depr', $depr);
477
    }
478
479
    /**
480
     * 设置需要合并的路由参数
481
     * @access public
482
     * @param  array $option 路由参数
483
     * @return $this
484
     */
485
    public function mergeOptions(array $option = [])
486
    {
487
        $this->mergeOptions = array_merge($this->mergeOptions, $option);
488
        return $this;
489
    }
490
491
    /**
492
     * 检查是否为HTTPS请求
493
     * @access public
494
     * @param  bool $https 是否为HTTPS
495
     * @return $this
496
     */
497
    public function https(bool $https = true)
498
    {
499
        return $this->setOption('https', $https);
500
    }
501
502
    /**
503
     * 检查是否为JSON请求
504
     * @access public
505
     * @param  bool $json 是否为JSON
506
     * @return $this
507
     */
508
    public function json(bool $json = true)
509
    {
510
        return $this->setOption('json', $json);
511
    }
512
513
    /**
514
     * 检查是否为AJAX请求
515
     * @access public
516
     * @param  bool $ajax 是否为AJAX
517
     * @return $this
518
     */
519
    public function ajax(bool $ajax = true)
520
    {
521
        return $this->setOption('ajax', $ajax);
522
    }
523
524
    /**
525
     * 检查是否为PJAX请求
526
     * @access public
527
     * @param  bool $pjax 是否为PJAX
528
     * @return $this
529
     */
530
    public function pjax(bool $pjax = true)
531
    {
532
        return $this->setOption('pjax', $pjax);
533
    }
534
535
    /**
536
     * 路由到一个模板地址 需要额外传入的模板变量
537
     * @access public
538
     * @param  array $view 视图
539
     * @return $this
540
     */
541
    public function view(array $view = [])
542
    {
543
        return $this->setOption('view', $view);
544
    }
545
546
    /**
547
     * 通过闭包检查路由是否匹配
548
     * @access public
549
     * @param  callable $match 闭包
550
     * @return $this
551
     */
552
    public function match(callable $match)
553
    {
554
        return $this->setOption('match', $match);
555
    }
556
557
    /**
558
     * 设置路由完整匹配
559
     * @access public
560
     * @param  bool $match 是否完整匹配
561
     * @return $this
562
     */
563
    public function completeMatch(bool $match = true)
564
    {
565
        return $this->setOption('complete_match', $match);
566
    }
567
568
    /**
569
     * 是否去除URL最后的斜线
570
     * @access public
571
     * @param  bool $remove 是否去除最后斜线
572
     * @return $this
573
     */
574 27
    public function removeSlash(bool $remove = true)
575
    {
576 27
        return $this->setOption('remove_slash', $remove);
577
    }
578
579
    /**
580
     * 设置路由规则全局有效
581
     * @access public
582
     * @return $this
583
     */
584
    public function crossDomainRule()
585
    {
586
        $this->router->setCrossDomainRule($this);
587
        return $this;
588
    }
589
590
    /**
591
     * 解析匹配到的规则路由
592
     * @access public
593
     * @param  Request $request 请求对象
594
     * @param  string  $rule 路由规则
595
     * @param  mixed   $route 路由地址
596
     * @param  string  $url URL地址
597
     * @param  array   $option 路由参数
598
     * @param  array   $matches 匹配的变量
599
     * @return Dispatch
600
     */
601 24
    public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch
602
    {
603 24
        if (is_string($route) && isset($option['prefix'])) {
604
            // 路由地址前缀
605
            $route = $option['prefix'] . $route;
606
        }
607
608
        // 替换路由地址中的变量
609 24
        $extraParams = true;
610 24
        $search      = $replace      = [];
611 24
        $depr        = $this->config('pathinfo_depr');
612 24
        foreach ($matches as $key => $value) {
613
            $search[]  = '<' . $key . '>';
614
            $replace[] = $value;
615
616
            $search[]  = ':' . $key;
617
            $replace[] = $value;
618
619
            if (str_contains($value, $depr)) {
0 ignored issues
show
Bug introduced by
It seems like $depr can also be of type null; however, parameter $needle of str_contains() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

619
            if (str_contains($value, /** @scrutinizer ignore-type */ $depr)) {
Loading history...
620
                $extraParams = false;
621
            }
622
        }
623
624 24
        if (is_string($route)) {
625 9
            $route = str_replace($search, $replace, $route);
626
        }
627
628
        // 解析额外参数
629 24
        if ($extraParams) {
630 24
            $count = substr_count($rule, '/');
631 24
            $url   = array_slice(explode('|', $url), $count + 1);
632 24
            $this->parseUrlParams(implode('|', $url), $matches);
633
        }
634
635 24
        $this->vars = $matches;
636
637
        // 发起路由调度
638 24
        return $this->dispatch($request, $route, $option);
639
    }
640
641
    /**
642
     * 发起路由调度
643
     * @access protected
644
     * @param  Request $request Request对象
645
     * @param  mixed   $route  路由地址
646
     * @param  array   $option 路由参数
647
     * @return Dispatch
648
     */
649 24
    protected function dispatch(Request $request, $route, array $option): Dispatch
650
    {
651 24
        if (isset($option['dispatcher']) && is_subclass_of($option['dispatcher'], Dispatch::class)) {
652
            // 指定分组的调度处理对象
653
            $result = new $option['dispatcher']($request, $this, $route, $this->vars);
654 24
        } elseif (is_subclass_of($route, Dispatch::class)) {
655
            $result = new $route($request, $this, $route, $this->vars);
656 24
        } elseif ($route instanceof Closure) {
657
            // 执行闭包
658 15
            $result = new CallbackDispatch($request, $this, $route, $this->vars);
659 9
        } elseif (is_array($route)) {
660
            // 路由到类的方法
661
            $result = $this->dispatchMethod($request, $route);
662 9
        } elseif (str_contains($route, '@') || str_contains($route, '::') || str_contains($route, '\\')) {
663
            // 路由到类的方法
664
            $route  = str_replace('::', '@', $route);
665
            $result = $this->dispatchMethod($request, $route);
666
        } else {
667
            // 路由到控制器/操作
668 9
            $result = $this->dispatchController($request, $route);
669
        }
670
671 24
        return $result;
672
    }
673
674
    /**
675
     * 调度到类的方法
676
     * @access protected
677
     * @param  Request $request Request对象
678
     * @param  string|array  $route 路由地址
679
     * @return CallbackDispatch
680
     */
681
    protected function dispatchMethod(Request $request, string | array $route): CallbackDispatch
682
    {
683
        if (is_string($route)) {
0 ignored issues
show
introduced by
The condition is_string($route) is always false.
Loading history...
684
            $path = $this->parseUrlPath($route);
685
686
            $route  = str_replace('/', '@', implode('/', $path));
687
            $method = str_contains($route, '@') ? explode('@', $route) : $route;
688
        } else {
689
            $method = $route;
690
        }
691
692
        return new CallbackDispatch($request, $this, $method, $this->vars);
693
    }
694
695
    /**
696
     * 调度到控制器方法 规则:模块/控制器/操作
697
     * @access protected
698
     * @param  Request $request Request对象
699
     * @param  string  $route 路由地址
700
     * @return ControllerDispatch
701
     */
702 9
    protected function dispatchController(Request $request, string $route): ControllerDispatch
703
    {
704 9
        $path = $this->parseUrlPath($route);
705
706 9
        $action     = array_pop($path);
707 9
        $controller = !empty($path) ? array_pop($path) : null;
708
709
        // 路由到模块/控制器/操作
710 9
        return new ControllerDispatch($request, $this, [$controller, $action], $this->vars);
711
    }
712
713
    /**
714
     * 路由检查
715
     * @access protected
716
     * @param  array   $option 路由参数
717
     * @param  Request $request Request对象
718
     * @return bool
719
     */
720 27
    protected function checkOption(array $option, Request $request): bool
721
    {
722
        // 检查当前路由是否匹配
723 27
        if (isset($option['match']) && is_callable($option['match'])) {
724
            if (false === $option['match']($this, $request)) {
725
                return false;
726
            }
727
        }
728
729
        // 请求类型检测
730 27
        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 27
        foreach (['ajax', 'pjax', 'json'] as $item) {
738 27
            if (isset($option[$item])) {
739
                $call = 'is' . $item;
740
                if ($option[$item] && !$request->$call() || !$option[$item] && $request->$call()) {
0 ignored issues
show
introduced by
Consider adding parentheses for clarity. Current Interpretation: ($option[$item] && ! $re...m] && $request->$call(), Probably Intended Meaning: $option[$item] && (! $re...] && $request->$call())
Loading history...
741
                    return false;
742
                }
743
            }
744
        }
745
746
        // 伪静态后缀检测
747 27
        if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|'))
748 27
            || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) {
749
            return false;
750
        }
751
752
        // 域名检查
753 27
        if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) {
754
            return false;
755
        }
756
757
        // HTTPS检查
758 27
        if ((isset($option['https']) && $option['https'] && !$request->isSsl())
759 27
            || (isset($option['https']) && !$option['https'] && $request->isSsl())
760
        ) {
761
            return false;
762
        }
763
764
        // 请求参数检查
765 27
        if (isset($option['filter'])) {
766
            foreach ($option['filter'] as $name => $value) {
767
                if ($request->param($name, '') != $value) {
768
                    return false;
769
                }
770
            }
771
        }
772
773 27
        return true;
774
    }
775
776
    /**
777
     * 解析URL地址中的参数Request对象
778
     * @access protected
779
     * @param  string $rule 路由规则
780
     * @param  array  $var 变量
781
     * @return void
782
     */
783 24
    protected function parseUrlParams(string $url, array &$var = []): void
784
    {
785 24
        if ($url) {
786
            preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
787
                $var[$match[1]] = strip_tags($match[2]);
788
            }, $url);
789
        }
790
    }
791
792
    /**
793
     * 解析URL的pathinfo参数
794
     * @access public
795
     * @param  string $url URL地址
796
     * @return array
797
     */
798 12
    public function parseUrlPath(string $url): array
799
    {
800
        // 分隔符替换 确保路由定义使用统一的分隔符
801 12
        $url = str_replace('|', '/', $url);
802 12
        $url = trim($url, '/');
803
804 12
        if (str_contains($url, '/')) {
805
            // [控制器/操作]
806 9
            $path = explode('/', $url);
807
        } else {
808 3
            $path = [$url];
809
        }
810
811 12
        return $path;
812
    }
813
814
    /**
815
     * 生成路由的正则规则
816
     * @access protected
817
     * @param  string $rule 路由规则
818
     * @param  array  $match 匹配的变量
819
     * @param  array  $pattern   路由变量规则
820
     * @param  array  $option    路由参数
821
     * @param  bool   $completeMatch   路由是否完全匹配
822
     * @param  string $suffix   路由正则变量后缀
823
     * @return string
824
     */
825
    protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string
826
    {
827
        foreach ($match as $name) {
828
            $value = $this->buildNameRegex($name, $pattern, $suffix);
829
            if ($value) {
830
                $origin[]  = $name;
831
                $replace[] = $value;
832
            }
833
        }
834
835
        // 是否区分 / 地址访问
836
        if ('/' != $rule) {
837
            if (!empty($option['remove_slash'])) {
838
                $rule = rtrim($rule, '/');
839
            } elseif (str_ends_with($rule, '/')) {
840
                $rule     = rtrim($rule, '/');
841
                $hasSlash = true;
842
            }
843
        }
844
845
        $regex = isset($replace) ? str_replace($origin, $replace, $rule) : $rule;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $origin does not seem to be defined for all execution paths leading up to this point.
Loading history...
846
        $regex = str_replace([')?/', ')?-'], [')/', ')-'], $regex);
847
848
        if (isset($hasSlash)) {
849
            $regex .= '/';
850
        }
851
852
        return $regex . ($completeMatch ? '$' : '');
853
    }
854
855
    /**
856
     * 生成路由变量的正则规则
857
     * @access protected
858
     * @param  string $name    路由变量
859
     * @param  array  $pattern 变量规则
860
     * @param  string $suffix  路由正则变量后缀
861
     * @return string
862
     */
863
    protected function buildNameRegex(string $name, array $pattern, string $suffix): string
864
    {
865
        $optional = '';
866
        $slash    = substr($name, 0, 1);
867
868
        if (in_array($slash, ['/', '-'])) {
869
            $prefix = $slash;
870
            $name   = substr($name, 1);
871
            $slash  = substr($name, 0, 1);
872
        } else {
873
            $prefix = '';
874
        }
875
876
        if ('<' != $slash) {
877
            return '';
878
        }
879
880
        if (str_contains($name, '?')) {
881
            $name     = substr($name, 1, -2);
882
            $optional = '?';
883
        } elseif (str_contains($name, '>')) {
884
            $name = substr($name, 1, -1);
885
        }
886
887
        if (isset($pattern[$name])) {
888
            $nameRule = $pattern[$name];
889
            if (str_starts_with($nameRule, '/') && str_ends_with($nameRule, '/')) {
890
                $nameRule = substr($nameRule, 1, -1);
891
            }
892
        } else {
893
            $nameRule = $this->config('default_route_pattern');
894
        }
895
896
        return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional;
897
    }
898
899
    /**
900
     * 设置路由参数
901
     * @access public
902
     * @param  string $method 方法名
903
     * @param  array  $args   调用参数
904
     * @return $this
905
     */
906
    public function __call($method, $args)
907
    {
908
        if (count($args) > 1) {
909
            $args[0] = $args;
910
        }
911
        array_unshift($args, $method);
912
913
        return call_user_func_array([$this, 'setOption'], $args);
914
    }
915
916
    public function __sleep()
917
    {
918
        return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern'];
919
    }
920
921
    public function __wakeup()
922
    {
923
        $this->router = Container::pull('route');
924
    }
925
926
    public function __debugInfo()
927
    {
928
        return [
929
            'name'    => $this->name,
930
            'rule'    => $this->rule,
931
            'route'   => $this->route,
932
            'method'  => $this->method,
933
            'vars'    => $this->vars,
934
            'option'  => $this->option,
935
            'pattern' => $this->pattern,
936
        ];
937
    }
938
}
939