Passed
Push — 5.2 ( da0c84...54339d )
by liu
02:41
created

Rule::checkOption()   D

Complexity

Conditions 26
Paths 39

Size

Total Lines 46
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 26
eloc 21
nc 39
nop 2
dl 0
loc 46
rs 4.1666
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
// +----------------------------------------------------------------------
3
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
4
// +----------------------------------------------------------------------
5
// | Copyright (c) 2006~2019 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 think\Container;
16
use think\Request;
17
use think\Response;
18
use think\Route;
19
use think\route\dispatch\Callback as CallbackDispatch;
20
use think\route\dispatch\Controller as ControllerDispatch;
21
use think\route\dispatch\Redirect as RedirectDispatch;
22
use think\route\dispatch\Response as ResponseDispatch;
23
use think\route\dispatch\View as ViewDispatch;
24
25
abstract class Rule
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 string
172
     */
173
    public function getRule(): string
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 = '')
225
    {
226
        return $this->router->config($name);
227
    }
228
229
    /**
230
     * 获取变量规则定义
231
     * @access public
232
     * @param  string  $name 变量名
233
     * @return mixed
234
     */
235
    public function getPattern(string $name = '')
236
    {
237
        if ('' === $name) {
238
            return $this->pattern;
239
        }
240
241
        return $this->pattern[$name] ?? null;
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)
287
    {
288
        return $this->setOption('method', strtolower($method));
289
    }
290
291
    /**
292
     * 设置路由前置行为
293
     * @access public
294
     * @param  array|\Closure    $before
295
     * @return $this
296
     */
297
    public function before($before)
298
    {
299
        return $this->setOption('before', $before);
300
    }
301
302
    /**
303
     * 设置路由后置行为
304
     * @access public
305
     * @param  array|\Closure     $after
306
     * @return $this
307
     */
308
    public function after($after)
309
    {
310
        return $this->setOption('after', $after);
311
    }
312
313
    /**
314
     * 检查后缀
315
     * @access public
316
     * @param  string     $ext
317
     * @return $this
318
     */
319
    public function ext(string $ext = '')
320
    {
321
        return $this->setOption('ext', $ext);
322
    }
323
324
    /**
325
     * 检查禁止后缀
326
     * @access public
327
     * @param  string     $ext
328
     * @return $this
329
     */
330
    public function denyExt(string $ext = '')
331
    {
332
        return $this->setOption('deny_ext', $ext);
333
    }
334
335
    /**
336
     * 检查域名
337
     * @access public
338
     * @param  string     $domain
339
     * @return $this
340
     */
341
    public function domain(string $domain)
342
    {
343
        return $this->setOption('domain', $domain);
344
    }
345
346
    /**
347
     * 设置参数过滤检查
348
     * @access public
349
     * @param  array     $filter
350
     * @return $this
351
     */
352
    public function filter(array $filter)
353
    {
354
        $this->option['filter'] = $filter;
355
356
        return $this;
357
    }
358
359
    /**
360
     * 绑定模型
361
     * @access public
362
     * @param  array|string|\Closure    $var  路由变量名 多个使用 & 分割
363
     * @param  string|\Closure          $model 绑定模型类
364
     * @param  bool                     $exception 是否抛出异常
365
     * @return $this
366
     */
367
    public function model($var, $model = null, bool $exception = true)
368
    {
369
        if ($var instanceof \Closure) {
370
            $this->option['model'][] = $var;
371
        } elseif (is_array($var)) {
372
            $this->option['model'] = $var;
373
        } elseif (is_null($model)) {
374
            $this->option['model']['id'] = [$var, true];
375
        } else {
376
            $this->option['model'][$var] = [$model, $exception];
377
        }
378
379
        return $this;
380
    }
381
382
    /**
383
     * 附加路由隐式参数
384
     * @access public
385
     * @param  array     $append
386
     * @return $this
387
     */
388
    public function append(array $append = [])
389
    {
390
        $this->option['append'] = $append;
391
392
        return $this;
393
    }
394
395
    /**
396
     * 绑定验证
397
     * @access public
398
     * @param  mixed    $validate 验证器类
399
     * @param  string   $scene 验证场景
400
     * @param  array    $message 验证提示
401
     * @param  bool     $batch 批量验证
402
     * @return $this
403
     */
404
    public function validate($validate, string $scene = null, array $message = [], bool $batch = false)
405
    {
406
        $this->option['validate'] = [$validate, $scene, $message, $batch];
407
408
        return $this;
409
    }
410
411
    /**
412
     * 绑定Response对象
413
     * @access public
414
     * @param  mixed     $response
415
     * @return $this
416
     */
417
    public function response($response)
418
    {
419
        $this->option['response'][] = $response;
420
        return $this;
421
    }
422
423
    /**
424
     * 设置Response Header信息
425
     * @access public
426
     * @param  array $header  头信息
427
     * @return $this
428
     */
429
    public function header(array $header)
430
    {
431
        $this->option['header'] = $header;
432
433
        return $this;
434
    }
435
436
    /**
437
     * 指定路由中间件
438
     * @access public
439
     * @param  string|array|\Closure    $middleware
440
     * @param  mixed                    $param
441
     * @return $this
442
     */
443
    public function middleware($middleware, $param = null)
444
    {
445
        if (is_null($param) && is_array($middleware)) {
446
            $this->option['middleware'] = $middleware;
447
        } else {
448
            foreach ((array) $middleware as $item) {
449
                $this->option['middleware'][] = [$item, $param];
450
            }
451
        }
452
453
        return $this;
454
    }
455
456
    /**
457
     * 设置路由缓存
458
     * @access public
459
     * @param  array|string     $cache
460
     * @return $this
461
     */
462
    public function cache($cache)
463
    {
464
        return $this->setOption('cache', $cache);
465
    }
466
467
    /**
468
     * 检查URL分隔符
469
     * @access public
470
     * @param  string     $depr
471
     * @return $this
472
     */
473
    public function depr(string $depr)
474
    {
475
        return $this->setOption('param_depr', $depr);
476
    }
477
478
    /**
479
     * 设置需要合并的路由参数
480
     * @access public
481
     * @param  array     $option
482
     * @return $this
483
     */
484
    public function mergeOptions(array $option = [])
485
    {
486
        $this->mergeOptions = array_merge($this->mergeOptions, $option);
487
        return $this;
488
    }
489
490
    /**
491
     * 检查是否为HTTPS请求
492
     * @access public
493
     * @param  bool     $https
494
     * @return $this
495
     */
496
    public function https(bool $https = true)
497
    {
498
        return $this->setOption('https', $https);
499
    }
500
501
    /**
502
     * 检查是否为AJAX请求
503
     * @access public
504
     * @param  bool     $ajax
505
     * @return $this
506
     */
507
    public function ajax(bool $ajax = true)
508
    {
509
        return $this->setOption('ajax', $ajax);
510
    }
511
512
    /**
513
     * 检查是否为PJAX请求
514
     * @access public
515
     * @param  bool     $pjax
516
     * @return $this
517
     */
518
    public function pjax(bool $pjax = true)
519
    {
520
        return $this->setOption('pjax', $pjax);
521
    }
522
523
    /**
524
     * 当前路由到一个模板地址 当使用数组的时候可以传入模板变量
525
     * @access public
526
     * @param  bool|array     $view
527
     * @return $this
528
     */
529
    public function view($view = true)
530
    {
531
        return $this->setOption('view', $view);
532
    }
533
534
    /**
535
     * 当前路由为重定向
536
     * @access public
537
     * @param  bool   $redirect 是否为重定向
538
     * @return $this
539
     */
540
    public function redirect(bool $redirect = true)
541
    {
542
        return $this->setOption('redirect', $redirect);
543
    }
544
545
    /**
546
     * 设置status
547
     * @access public
548
     * @param  int   $status
549
     * @return $this
550
     */
551
    public function status(int $status)
552
    {
553
        return $this->setOption('status', $status);
554
    }
555
556
    /**
557
     * 设置路由完整匹配
558
     * @access public
559
     * @param  bool     $match
560
     * @return $this
561
     */
562
    public function completeMatch(bool $match = true)
563
    {
564
        return $this->setOption('complete_match', $match);
565
    }
566
567
    /**
568
     * 是否去除URL最后的斜线
569
     * @access public
570
     * @param  bool     $remove
571
     * @return $this
572
     */
573
    public function removeSlash(bool $remove = true)
574
    {
575
        return $this->setOption('remove_slash', $remove);
576
    }
577
578
    /**
579
     * 设置是否允许跨域
580
     * @access public
581
     * @param  bool     $allow
582
     * @param  array    $header
583
     * @return $this
584
     */
585
    public function allowCrossDomain(bool $allow = true, array $header = [])
586
    {
587
        if (!empty($header)) {
588
            $this->header($header);
589
        }
590
591
        if ($allow && $this->parent) {
592
            $this->parent->addRuleItem($this, 'options');
593
        }
594
595
        return $this->setOption('cross_domain', $allow);
596
    }
597
598
    /**
599
     * 检查OPTIONS请求
600
     * @access public
601
     * @param  Request     $request
602
     * @return Dispatch|void
603
     */
604
    protected function checkCrossDomain(Request $request)
605
    {
606
        if (!empty($this->option['cross_domain'])) {
607
608
            $header = [
609
                'Access-Control-Allow-Origin'  => '*',
610
                'Access-Control-Allow-Methods' => 'GET, POST, PATCH, PUT, DELETE',
611
                'Access-Control-Allow-Headers' => 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With',
612
            ];
613
614
            if (!empty($this->option['header'])) {
615
                $header = array_merge($header, $this->option['header']);
616
            }
617
618
            $this->option['header'] = $header;
619
620
            if ($request->method(true) == 'OPTIONS') {
621
                return new ResponseDispatch($request, $this, Response::create()->code(204)->header($header));
622
            }
623
        }
624
    }
625
626
    /**
627
     * 设置路由规则全局有效
628
     * @access public
629
     * @return $this
630
     */
631
    public function crossDomainRule()
632
    {
633
        if ($this instanceof RuleGroup) {
634
            $method = '*';
635
        } else {
636
            $method = $this->method;
637
        }
638
639
        $this->router->setCrossDomainRule($this, $method);
640
641
        return $this;
642
    }
643
644
    /**
645
     * 合并分组参数
646
     * @access public
647
     * @return array
648
     */
649
    public function mergeGroupOptions(): array
650
    {
651
        $parentOption = $this->parent->getOption();
652
        // 合并分组参数
653
        foreach ($this->mergeOptions as $item) {
654
            if (isset($parentOption[$item]) && isset($this->option[$item])) {
655
                $this->option[$item] = array_merge($parentOption[$item], $this->option[$item]);
656
            }
657
        }
658
659
        $this->option = array_merge($parentOption, $this->option);
660
661
        return $this->option;
662
    }
663
664
    /**
665
     * 解析匹配到的规则路由
666
     * @access public
667
     * @param  Request   $request 请求对象
668
     * @param  string    $rule 路由规则
669
     * @param  string    $route 路由地址
670
     * @param  string    $url URL地址
671
     * @param  array     $option 路由参数
672
     * @param  array     $matches 匹配的变量
673
     * @return Dispatch
674
     */
675
    public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch
676
    {
677
        if (is_string($route) && isset($option['prefix'])) {
678
            // 路由地址前缀
679
            $route = $option['prefix'] . $route;
680
        }
681
682
        // 替换路由地址中的变量
683
        if (is_string($route) && !empty($matches)) {
684
            $search = $replace = [];
685
686
            foreach ($matches as $key => $value) {
687
                $search[]  = '<' . $key . '>';
688
                $replace[] = $value;
689
690
                $search[]  = ':' . $key;
691
                $replace[] = $value;
692
            }
693
694
            $route = str_replace($search, $replace, $route);
695
        }
696
697
        // 解析额外参数
698
        $count = substr_count($rule, '/');
699
        $url   = array_slice(explode('|', $url), $count + 1);
700
        $this->parseUrlParams(implode('|', $url), $matches);
701
702
        $this->route   = $route;
703
        $this->vars    = $matches;
704
        $this->option  = $option;
705
        $this->doAfter = true;
706
707
        // 发起路由调度
708
        return $this->dispatch($request, $route, $option);
709
    }
710
711
    /**
712
     * 发起路由调度
713
     * @access protected
714
     * @param  Request   $request Request对象
715
     * @param  mixed     $route  路由地址
716
     * @param  array     $option 路由参数
717
     * @return Dispatch
718
     */
719
    protected function dispatch(Request $request, $route, array $option): Dispatch
720
    {
721
        if ($route instanceof Dispatch) {
722
            $result = $route;
723
        } elseif ($route instanceof \Closure) {
724
            // 执行闭包
725
            $result = new CallbackDispatch($request, $this, $route);
726
        } elseif ($route instanceof Response) {
727
            $result = new ResponseDispatch($request, $this, $route);
728
        } elseif (isset($option['view']) && false !== $option['view']) {
729
            $result = new ViewDispatch($request, $this, $route, is_array($option['view']) ? $option['view'] : []);
730
        } elseif (!empty($option['redirect']) || 0 === strpos($route, '/') || strpos($route, '://')) {
731
            // 路由到重定向地址
732
            $result = new RedirectDispatch($request, $this, $route, [], $option['status'] ?? 301);
733
        } elseif (false !== strpos($route, '\\')) {
734
            // 路由到类的方法
735
            $result = $this->dispatchMethod($request, $route);
736
        } else {
737
            // 路由到控制器/操作
738
            $result = $this->dispatchController($request, $route);
739
        }
740
741
        return $result;
742
    }
743
744
    /**
745
     * 解析URL地址为 模块/控制器/操作
746
     * @access protected
747
     * @param  Request   $request Request对象
748
     * @param  string    $route 路由地址
749
     * @return CallbackDispatch
750
     */
751
    protected function dispatchMethod(Request $request, string $route): CallbackDispatch
752
    {
753
        list($path, $var) = $this->parseUrlPath($route);
754
755
        $route  = str_replace('/', '@', implode('/', $path));
756
        $method = strpos($route, '@') ? explode('@', $route) : $route;
757
758
        return new CallbackDispatch($request, $this, $method, $var);
759
    }
760
761
    /**
762
     * 解析URL地址为 模块/控制器/操作
763
     * @access protected
764
     * @param  Request   $request Request对象
765
     * @param  string    $route 路由地址
766
     * @return ControllerDispatch
767
     */
768
    protected function dispatchController(Request $request, string $route): ControllerDispatch
769
    {
770
        list($path, $var) = $this->parseUrlPath($route);
771
772
        $action     = array_pop($path);
773
        $controller = !empty($path) ? array_pop($path) : null;
774
775
        // 路由到模块/控制器/操作
776
        return new ControllerDispatch($request, $this, [$controller, $action], $var);
777
    }
778
779
    /**
780
     * 路由检查
781
     * @access protected
782
     * @param  array     $option 路由参数
783
     * @param  Request   $request Request对象
784
     * @return bool
785
     */
786
    protected function checkOption(array $option, Request $request): bool
787
    {
788
        // 请求类型检测
789
        if (!empty($option['method'])) {
790
            if (is_string($option['method']) && false === stripos($option['method'], $request->method())) {
791
                return false;
792
            }
793
        }
794
795
        // AJAX PJAX 请求检查
796
        foreach (['ajax', 'pjax'] as $item) {
797
            if (isset($option[$item])) {
798
                $call = 'is' . $item;
799
                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...
800
                    return false;
801
                }
802
            }
803
        }
804
805
        // 伪静态后缀检测
806
        if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|'))
807
            || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) {
808
            return false;
809
        }
810
811
        // 域名检查
812
        if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) {
813
            return false;
814
        }
815
816
        // HTTPS检查
817
        if ((isset($option['https']) && $option['https'] && !$request->isSsl())
818
            || (isset($option['https']) && !$option['https'] && $request->isSsl())) {
819
            return false;
820
        }
821
822
        // 请求参数检查
823
        if (isset($option['filter'])) {
824
            foreach ($option['filter'] as $name => $value) {
825
                if ($request->param($name, '', null) != $value) {
826
                    return false;
827
                }
828
            }
829
        }
830
831
        return true;
832
    }
833
834
    /**
835
     * 解析URL地址中的参数Request对象
836
     * @access protected
837
     * @param  string    $rule 路由规则
838
     * @param  array     $var 变量
839
     * @return void
840
     */
841
    protected function parseUrlParams(string $url, array &$var = []): void
842
    {
843
        if ($url) {
844
            preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
845
                $var[$match[1]] = strip_tags($match[2]);
846
            }, $url);
847
        }
848
    }
849
850
    /**
851
     * 解析URL的pathinfo参数和变量
852
     * @access public
853
     * @param  string    $url URL地址
854
     * @return array
855
     */
856
    public function parseUrlPath(string $url): array
857
    {
858
        // 分隔符替换 确保路由定义使用统一的分隔符
859
        $url = str_replace('|', '/', $url);
860
        $url = trim($url, '/');
861
        $var = [];
862
863
        if (false !== strpos($url, '?')) {
864
            // [控制器/操作?]参数1=值1&参数2=值2...
865
            $info = parse_url($url);
866
            $path = explode('/', $info['path']);
867
            parse_str($info['query'], $var);
868
        } elseif (strpos($url, '/')) {
869
            // [控制器/操作]
870
            $path = explode('/', $url);
871
        } elseif (false !== strpos($url, '=')) {
872
            // 参数1=值1&参数2=值2...
873
            parse_str($url, $var);
874
            $path = [];
875
        } else {
876
            $path = [$url];
877
        }
878
879
        return [$path, $var];
880
    }
881
882
    /**
883
     * 生成路由的正则规则
884
     * @access protected
885
     * @param  string    $rule 路由规则
886
     * @param  array     $match 匹配的变量
887
     * @param  array     $pattern   路由变量规则
888
     * @param  array     $option    路由参数
889
     * @param  bool      $completeMatch   路由是否完全匹配
890
     * @param  string    $suffix   路由正则变量后缀
891
     * @return string
892
     */
893
    protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string
894
    {
895
        foreach ($match as $name) {
896
            $replace[] = $this->buildNameRegex($name, $pattern, $suffix);
897
        }
898
899
        // 是否区分 / 地址访问
900
        if ('/' != $rule) {
901
            if (!empty($option['remove_slash'])) {
902
                $rule = rtrim($rule, '/');
903
            } elseif (substr($rule, -1) == '/') {
904
                $rule     = rtrim($rule, '/');
905
                $hasSlash = true;
906
            }
907
        }
908
909
        $regex = str_replace(array_unique($match), array_unique($replace), $rule);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $replace seems to be defined by a foreach iteration on line 895. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
910
        $regex = str_replace([')?/', ')/', ')?-', ')-', '\\\\/'], [')\/', ')\/', ')\-', ')\-', '\/'], $regex);
911
912
        if (isset($hasSlash)) {
913
            $regex .= '\/';
914
        }
915
916
        return $regex . ($completeMatch ? '$' : '');
917
    }
918
919
    /**
920
     * 生成路由变量的正则规则
921
     * @access protected
922
     * @param  string    $name      路由变量
923
     * @param  array     $pattern   变量规则
924
     * @param  string    $suffix    路由正则变量后缀
925
     * @return string
926
     */
927
    protected function buildNameRegex(string $name, array $pattern, string $suffix): string
928
    {
929
        $optional = '';
930
        $slash    = substr($name, 0, 1);
931
932
        if (in_array($slash, ['/', '-'])) {
933
            $prefix = '\\' . $slash;
934
            $name   = substr($name, 1);
935
            $slash  = substr($name, 0, 1);
936
        } else {
937
            $prefix = '';
938
        }
939
940
        if ('<' != $slash) {
941
            return $prefix . preg_quote($name, '/');
942
        }
943
944
        if (strpos($name, '?')) {
945
            $name     = substr($name, 1, -2);
946
            $optional = '?';
947
        } elseif (strpos($name, '>')) {
948
            $name = substr($name, 1, -1);
949
        }
950
951
        if (isset($pattern[$name])) {
952
            $nameRule = $pattern[$name];
953
            if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) {
954
                $nameRule = substr($nameRule, 1, -1);
955
            }
956
        } else {
957
            $nameRule = $this->router->config('default_route_pattern');
958
        }
959
960
        return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional;
961
    }
962
963
    /**
964
     * 分析路由规则中的变量
965
     * @access protected
966
     * @param  string    $rule 路由规则
967
     * @return array
968
     */
969
    protected function parseVar(string $rule): array
970
    {
971
        // 提取路由规则中的变量
972
        $var = [];
973
974
        if (preg_match_all('/<\w+\??>/', $rule, $matches)) {
975
            foreach ($matches[0] as $name) {
976
                $optional = false;
977
978
                if (strpos($name, '?')) {
979
                    $name     = substr($name, 1, -2);
980
                    $optional = true;
981
                } else {
982
                    $name = substr($name, 1, -1);
983
                }
984
985
                $var[$name] = $optional ? 2 : 1;
986
            }
987
        }
988
989
        return $var;
990
    }
991
992
    /**
993
     * 设置路由参数
994
     * @access public
995
     * @param  string    $method     方法名
996
     * @param  array     $args       调用参数
997
     * @return $this
998
     */
999
    public function __call($method, $args)
1000
    {
1001
        if (count($args) > 1) {
1002
            $args[0] = $args;
1003
        }
1004
        array_unshift($args, $method);
1005
1006
        return call_user_func_array([$this, 'option'], $args);
1007
    }
1008
1009
    public function __sleep()
1010
    {
1011
        return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern', 'doAfter'];
1012
    }
1013
1014
    public function __wakeup()
1015
    {
1016
        $this->router = Container::pull('route');
1017
    }
1018
}
1019