Completed
Push — 6.0 ( 2e07d8...7e19b1 )
by liu
02:16
created

Rule::cache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
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 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 33
    public function setOption(string $name, $value)
119
    {
120 33
        $this->option[$name] = $value;
121
122 33
        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 6
    public function getRouter(): Route
157
    {
158 6
        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 3
    public function getRule()
177
    {
178 3
        return $this->rule;
179
    }
180
181
    /**
182
     * 获取当前路由地址
183
     * @access public
184
     * @return mixed
185
     */
186 30
    public function getRoute()
187
    {
188 30
        return $this->route;
189
    }
190
191
    /**
192
     * 获取当前路由的变量
193
     * @access public
194
     * @return array
195
     */
196
    public function getVars(): array
197
    {
198
        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
    public function getDomain(): string
217
    {
218
        return $this->domain ?: $this->parent->getDomain();
219
    }
220
221
    /**
222
     * 获取路由参数
223
     * @access public
224
     * @param  string $name 变量名
225
     * @return mixed
226
     */
227 12
    public function config(string $name = '')
228
    {
229 12
        return $this->router->config($name);
230
    }
231
232
    /**
233
     * 获取变量规则定义
234
     * @access public
235
     * @param  string $name 变量名
236
     * @return mixed
237
     */
238 30
    public function getPattern(string $name = '')
239
    {
240 30
        if ('' === $name) {
241 30
            return $this->pattern;
242
        }
243
244
        return $this->pattern[$name] ?? null;
245
    }
246
247
    /**
248
     * 获取路由参数定义
249
     * @access public
250
     * @param  string $name 参数名
251
     * @param  mixed  $default 默认值
252
     * @return mixed
253
     */
254 33
    public function getOption(string $name = '', $default = null)
255
    {
256 33
        if ('' === $name) {
257 33
            return $this->option;
258
        }
259
260
        return $this->option[$name] ?? $default;
261
    }
262
263
    /**
264
     * 获取当前路由的请求类型
265
     * @access public
266
     * @return string
267
     */
268 3
    public function getMethod(): string
269
    {
270 3
        return strtolower($this->method);
271
    }
272
273
    /**
274
     * 设置路由请求类型
275
     * @access public
276
     * @param  string $method 请求类型
277
     * @return $this
278
     */
279
    public function method(string $method)
280
    {
281
        return $this->setOption('method', strtolower($method));
282
    }
283
284
    /**
285
     * 检查后缀
286
     * @access public
287
     * @param  string $ext URL后缀
288
     * @return $this
289
     */
290
    public function ext(string $ext = '')
291
    {
292
        return $this->setOption('ext', $ext);
293
    }
294
295
    /**
296
     * 检查禁止后缀
297
     * @access public
298
     * @param  string $ext URL后缀
299
     * @return $this
300
     */
301
    public function denyExt(string $ext = '')
302
    {
303
        return $this->setOption('deny_ext', $ext);
304
    }
305
306
    /**
307
     * 检查域名
308
     * @access public
309
     * @param  string $domain 域名
310
     * @return $this
311
     */
312
    public function domain(string $domain)
313
    {
314
        $this->domain = $domain;
315
        return $this->setOption('domain', $domain);
316
    }
317
318
    /**
319
     * 设置参数过滤检查
320
     * @access public
321
     * @param  array $filter 参数过滤
322
     * @return $this
323
     */
324
    public function filter(array $filter)
325
    {
326
        $this->option['filter'] = $filter;
327
328
        return $this;
329
    }
330
331
    /**
332
     * 绑定模型
333
     * @access public
334
     * @param  array|string|Closure $var  路由变量名 多个使用 & 分割
335
     * @param  string|Closure       $model 绑定模型类
336
     * @param  bool                  $exception 是否抛出异常
337
     * @return $this
338
     */
339
    public function model($var, $model = null, bool $exception = true)
340
    {
341
        if ($var instanceof Closure) {
342
            $this->option['model'][] = $var;
343
        } elseif (is_array($var)) {
344
            $this->option['model'] = $var;
345
        } elseif (is_null($model)) {
346
            $this->option['model']['id'] = [$var, true];
347
        } else {
348
            $this->option['model'][$var] = [$model, $exception];
349
        }
350
351
        return $this;
352
    }
353
354
    /**
355
     * 附加路由隐式参数
356
     * @access public
357
     * @param  array $append 追加参数
358
     * @return $this
359
     */
360
    public function append(array $append = [])
361
    {
362
        $this->option['append'] = $append;
363
364
        return $this;
365
    }
366
367
    /**
368
     * 绑定验证
369
     * @access public
370
     * @param  mixed  $validate 验证器类
371
     * @param  string $scene 验证场景
372
     * @param  array  $message 验证提示
373
     * @param  bool   $batch 批量验证
374
     * @return $this
375
     */
376
    public function validate($validate, string $scene = null, array $message = [], bool $batch = false)
377
    {
378
        $this->option['validate'] = [$validate, $scene, $message, $batch];
379
380
        return $this;
381
    }
382
383
    /**
384
     * 指定路由中间件
385
     * @access public
386
     * @param string|array|Closure $middleware 中间件
387
     * @param mixed $params 参数
388
     * @return $this
389
     */
390 3
    public function middleware($middleware, ...$params)
391
    {
392 3
        if (empty($params) && is_array($middleware)) {
393
            $this->option['middleware'] = $middleware;
394
        } else {
395 3
            foreach ((array) $middleware as $item) {
396 3
                $this->option['middleware'][] = [$item, $params];
397
            }
398
        }
399
400 3
        return $this;
401
    }
402
403
    /**
404
     * 允许跨域
405
     * @access public
406
     * @param  array $header 自定义Header
407
     * @return $this
408
     */
409 3
    public function allowCrossDomain(array $header = [])
410
    {
411 3
        return $this->middleware(AllowCrossDomain::class, $header);
412
    }
413
414
    /**
415
     * 表单令牌验证
416
     * @access public
417
     * @param  string $token 表单令牌token名称
418
     * @return $this
419
     */
420
    public function token(string $token = '__token__')
421
    {
422
        return $this->middleware(FormTokenCheck::class, $token);
423
    }
424
425
    /**
426
     * 设置路由缓存
427
     * @access public
428
     * @param  array|string $cache 缓存
429
     * @return $this
430
     */
431
    public function cache($cache)
432
    {
433
        return $this->middleware(CheckRequestCache::class, $cache);
434
    }
435
436
    /**
437
     * 检查URL分隔符
438
     * @access public
439
     * @param  string $depr URL分隔符
440
     * @return $this
441
     */
442
    public function depr(string $depr)
443
    {
444
        return $this->setOption('param_depr', $depr);
445
    }
446
447
    /**
448
     * 设置需要合并的路由参数
449
     * @access public
450
     * @param  array $option 路由参数
451
     * @return $this
452
     */
453
    public function mergeOptions(array $option = [])
454
    {
455
        $this->mergeOptions = array_merge($this->mergeOptions, $option);
456
        return $this;
457
    }
458
459
    /**
460
     * 检查是否为HTTPS请求
461
     * @access public
462
     * @param  bool $https 是否为HTTPS
463
     * @return $this
464
     */
465
    public function https(bool $https = true)
466
    {
467
        return $this->setOption('https', $https);
468
    }
469
470
    /**
471
     * 检查是否为JSON请求
472
     * @access public
473
     * @param  bool $json 是否为JSON
474
     * @return $this
475
     */
476
    public function json(bool $json = true)
477
    {
478
        return $this->setOption('json', $json);
479
    }
480
481
    /**
482
     * 检查是否为AJAX请求
483
     * @access public
484
     * @param  bool $ajax 是否为AJAX
485
     * @return $this
486
     */
487
    public function ajax(bool $ajax = true)
488
    {
489
        return $this->setOption('ajax', $ajax);
490
    }
491
492
    /**
493
     * 检查是否为PJAX请求
494
     * @access public
495
     * @param  bool $pjax 是否为PJAX
496
     * @return $this
497
     */
498
    public function pjax(bool $pjax = true)
499
    {
500
        return $this->setOption('pjax', $pjax);
501
    }
502
503
    /**
504
     * 路由到一个模板地址 需要额外传入的模板变量
505
     * @access public
506
     * @param  array $view 视图
507
     * @return $this
508
     */
509
    public function view(array $view = [])
510
    {
511
        return $this->setOption('view', $view);
512
    }
513
514
    /**
515
     * 设置路由完整匹配
516
     * @access public
517
     * @param  bool $match 是否完整匹配
518
     * @return $this
519
     */
520
    public function completeMatch(bool $match = true)
521
    {
522
        return $this->setOption('complete_match', $match);
523
    }
524
525
    /**
526
     * 是否去除URL最后的斜线
527
     * @access public
528
     * @param  bool $remove 是否去除最后斜线
529
     * @return $this
530
     */
531 33
    public function removeSlash(bool $remove = true)
532
    {
533 33
        return $this->setOption('remove_slash', $remove);
534
    }
535
536
    /**
537
     * 设置路由规则全局有效
538
     * @access public
539
     * @return $this
540
     */
541
    public function crossDomainRule()
542
    {
543
        if ($this instanceof RuleGroup) {
544
            $method = '*';
545
        } else {
546
            $method = $this->method;
547
        }
548
549
        $this->router->setCrossDomainRule($this, $method);
550
551
        return $this;
552
    }
553
554
    /**
555
     * 获取合并后的路由参数
556
     * @access public
557
     * @param  bool  $groupMerge 是否分组合并
558
     * @return array
559
     */
560 33
    public function getMergeOptions(bool $groupMerge = false): array
561
    {
562 33
        if ($this->parent) {
563 30
            $parentOption = $this->parent->getOption();
564 30
            $option       = $this->option;
565
566
            // 合并分组参数
567 30
            if ($groupMerge) {
568 30
                foreach ($this->mergeOptions as $item) {
569 30
                    if (isset($parentOption[$item]) && isset($option[$item])) {
570 10
                        $option[$item] = array_merge($parentOption[$item], $option[$item]);
571
                    }
572
                }
573
            }
574
575 30
            return array_merge($parentOption, $option);
576
        } else {
577 9
            return $this->getOption();
578
        }
579
    }
580
581
    /**
582
     * 解析匹配到的规则路由
583
     * @access public
584
     * @param  Request $request 请求对象
585
     * @param  string  $rule 路由规则
586
     * @param  mixed   $route 路由地址
587
     * @param  string  $url URL地址
588
     * @param  array   $option 路由参数
589
     * @param  array   $matches 匹配的变量
590
     * @return Dispatch
591
     */
592 30
    public function parseRule(Request $request, string $rule, $route, string $url, array $option = [], array $matches = []): Dispatch
593
    {
594 30
        if (is_string($route) && isset($option['prefix'])) {
595
            // 路由地址前缀
596
            $route = $option['prefix'] . $route;
597
        }
598
599
        // 替换路由地址中的变量
600 30
        if (is_string($route) && !empty($matches)) {
601 3
            $search = $replace = [];
602
603 3
            foreach ($matches as $key => $value) {
604 3
                $search[]  = '<' . $key . '>';
605 3
                $replace[] = $value;
606
607 3
                $search[]  = ':' . $key;
608 3
                $replace[] = $value;
609
            }
610
611 3
            $route = str_replace($search, $replace, $route);
612
        }
613
614
        // 解析额外参数
615 30
        $count = substr_count($rule, '/');
616 30
        $url   = array_slice(explode('|', $url), $count + 1);
617 30
        $this->parseUrlParams(implode('|', $url), $matches);
618
619 30
        $this->vars = $matches;
620
621
        // 发起路由调度
622 30
        return $this->dispatch($request, $route, $option);
623
    }
624
625
    /**
626
     * 发起路由调度
627
     * @access protected
628
     * @param  Request $request Request对象
629
     * @param  mixed   $route  路由地址
630
     * @param  array   $option 路由参数
631
     * @return Dispatch
632
     */
633 30
    protected function dispatch(Request $request, $route, array $option): Dispatch
634
    {
635 30
        if (is_subclass_of($route, Dispatch::class)) {
636
            $result = new $route($request, $this, $route, $this->vars);
637 30
        } elseif ($route instanceof Closure) {
638
            // 执行闭包
639 21
            $result = new CallbackDispatch($request, $this, $route, $this->vars);
640 12
        } elseif (false !== strpos($route, '@') || false !== strpos($route, '::')) {
641
            // 路由到类的方法
642
            $route  = str_replace('::', '@', $route);
643
            $result = $this->dispatchMethod($request, $route);
644
        } else {
645
            // 路由到控制器/操作
646 12
            $result = $this->dispatchController($request, $route);
647
        }
648
649 30
        return $result;
650
    }
651
652
    /**
653
     * 解析URL地址为 模块/控制器/操作
654
     * @access protected
655
     * @param  Request $request Request对象
656
     * @param  string  $route 路由地址
657
     * @return CallbackDispatch
658
     */
659
    protected function dispatchMethod(Request $request, string $route): CallbackDispatch
660
    {
661
        $path = $this->parseUrlPath($route);
662
663
        $route  = str_replace('/', '@', implode('/', $path));
664
        $method = strpos($route, '@') ? explode('@', $route) : $route;
665
666
        return new CallbackDispatch($request, $this, $method, $this->vars);
667
    }
668
669
    /**
670
     * 解析URL地址为 模块/控制器/操作
671
     * @access protected
672
     * @param  Request $request Request对象
673
     * @param  string  $route 路由地址
674
     * @return ControllerDispatch
675
     */
676 12
    protected function dispatchController(Request $request, string $route): ControllerDispatch
677
    {
678 12
        $path = $this->parseUrlPath($route);
679
680 12
        $action     = array_pop($path);
681 12
        $controller = !empty($path) ? array_pop($path) : null;
682
683
        // 路由到模块/控制器/操作
684 12
        return new ControllerDispatch($request, $this, [$controller, $action], $this->vars);
685
    }
686
687
    /**
688
     * 路由检查
689
     * @access protected
690
     * @param  array   $option 路由参数
691
     * @param  Request $request Request对象
692
     * @return bool
693
     */
694 33
    protected function checkOption(array $option, Request $request): bool
695
    {
696
        // 请求类型检测
697 33
        if (!empty($option['method'])) {
698
            if (is_string($option['method']) && false === stripos($option['method'], $request->method())) {
699
                return false;
700
            }
701
        }
702
703
        // AJAX PJAX 请求检查
704 33
        foreach (['ajax', 'pjax', 'json'] as $item) {
705 33
            if (isset($option[$item])) {
706
                $call = 'is' . $item;
707
                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...
708 11
                    return false;
709
                }
710
            }
711
        }
712
713
        // 伪静态后缀检测
714 33
        if ($request->url() != '/' && ((isset($option['ext']) && false === stripos('|' . $option['ext'] . '|', '|' . $request->ext() . '|'))
715 33
            || (isset($option['deny_ext']) && false !== stripos('|' . $option['deny_ext'] . '|', '|' . $request->ext() . '|')))) {
716
            return false;
717
        }
718
719
        // 域名检查
720 33
        if ((isset($option['domain']) && !in_array($option['domain'], [$request->host(true), $request->subDomain()]))) {
721
            return false;
722
        }
723
724
        // HTTPS检查
725 33
        if ((isset($option['https']) && $option['https'] && !$request->isSsl())
726 33
            || (isset($option['https']) && !$option['https'] && $request->isSsl())) {
727
            return false;
728
        }
729
730
        // 请求参数检查
731 33
        if (isset($option['filter'])) {
732
            foreach ($option['filter'] as $name => $value) {
733
                if ($request->param($name, '', null) != $value) {
734
                    return false;
735
                }
736
            }
737
        }
738
739 33
        return true;
740
    }
741
742
    /**
743
     * 解析URL地址中的参数Request对象
744
     * @access protected
745
     * @param  string $rule 路由规则
746
     * @param  array  $var 变量
747
     * @return void
748
     */
749 30
    protected function parseUrlParams(string $url, array &$var = []): void
750
    {
751 30
        if ($url) {
752
            preg_replace_callback('/(\w+)\|([^\|]+)/', function ($match) use (&$var) {
753
                $var[$match[1]] = strip_tags($match[2]);
754
            }, $url);
755
        }
756 30
    }
757
758
    /**
759
     * 解析URL的pathinfo参数
760
     * @access public
761
     * @param  string $url URL地址
762
     * @return array
763
     */
764 15
    public function parseUrlPath(string $url): array
765
    {
766
        // 分隔符替换 确保路由定义使用统一的分隔符
767 15
        $url = str_replace('|', '/', $url);
768 15
        $url = trim($url, '/');
769
770 15
        if (strpos($url, '/')) {
771
            // [控制器/操作]
772 12
            $path = explode('/', $url);
773
        } else {
774 3
            $path = [$url];
775
        }
776
777 15
        return $path;
778
    }
779
780
    /**
781
     * 生成路由的正则规则
782
     * @access protected
783
     * @param  string $rule 路由规则
784
     * @param  array  $match 匹配的变量
785
     * @param  array  $pattern   路由变量规则
786
     * @param  array  $option    路由参数
787
     * @param  bool   $completeMatch   路由是否完全匹配
788
     * @param  string $suffix   路由正则变量后缀
789
     * @return string
790
     */
791 6
    protected function buildRuleRegex(string $rule, array $match, array $pattern = [], array $option = [], bool $completeMatch = false, string $suffix = ''): string
792
    {
793 6
        foreach ($match as $name) {
794 6
            $replace[] = $this->buildNameRegex($name, $pattern, $suffix);
795
        }
796
797
        // 是否区分 / 地址访问
798 6
        if ('/' != $rule) {
799 6
            if (!empty($option['remove_slash'])) {
800
                $rule = rtrim($rule, '/');
801 6
            } elseif (substr($rule, -1) == '/') {
802
                $rule     = rtrim($rule, '/');
803
                $hasSlash = true;
804
            }
805
        }
806
807 6
        $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 793. Are you sure the iterator is never empty, otherwise this variable is not defined?
Loading history...
808 6
        $regex = str_replace([')?/', ')/', ')?-', ')-', '\\\\/'], [')\/', ')\/', ')\-', ')\-', '\/'], $regex);
809
810 6
        if (isset($hasSlash)) {
811
            $regex .= '\/';
812
        }
813
814 6
        return $regex . ($completeMatch ? '$' : '');
815
    }
816
817
    /**
818
     * 生成路由变量的正则规则
819
     * @access protected
820
     * @param  string $name    路由变量
821
     * @param  array  $pattern 变量规则
822
     * @param  string $suffix  路由正则变量后缀
823
     * @return string
824
     */
825 6
    protected function buildNameRegex(string $name, array $pattern, string $suffix): string
826
    {
827 6
        $optional = '';
828 6
        $slash    = substr($name, 0, 1);
829
830 6
        if (in_array($slash, ['/', '-'])) {
831 6
            $prefix = '\\' . $slash;
832 6
            $name   = substr($name, 1);
833 6
            $slash  = substr($name, 0, 1);
834
        } else {
835
            $prefix = '';
836
        }
837
838 6
        if ('<' != $slash) {
839 6
            return $prefix . preg_quote($name, '/');
840
        }
841
842 6
        if (strpos($name, '?')) {
843
            $name     = substr($name, 1, -2);
844
            $optional = '?';
845 6
        } elseif (strpos($name, '>')) {
846 6
            $name = substr($name, 1, -1);
847
        }
848
849 6
        if (isset($pattern[$name])) {
850
            $nameRule = $pattern[$name];
851
            if (0 === strpos($nameRule, '/') && '/' == substr($nameRule, -1)) {
852
                $nameRule = substr($nameRule, 1, -1);
853
            }
854
        } else {
855 6
            $nameRule = $this->router->config('default_route_pattern');
856
        }
857
858 6
        return '(' . $prefix . '(?<' . $name . $suffix . '>' . $nameRule . '))' . $optional;
859
    }
860
861
    /**
862
     * 设置路由参数
863
     * @access public
864
     * @param  string $method 方法名
865
     * @param  array  $args   调用参数
866
     * @return $this
867
     */
868
    public function __call($method, $args)
869
    {
870
        if (count($args) > 1) {
871
            $args[0] = $args;
872
        }
873
        array_unshift($args, $method);
874
875
        return call_user_func_array([$this, 'setOption'], $args);
876
    }
877
878
    public function __sleep()
879
    {
880
        return ['name', 'rule', 'route', 'method', 'vars', 'option', 'pattern'];
881
    }
882
883
    public function __wakeup()
884
    {
885
        $this->router = Container::pull('route');
886
    }
887
888
    public function __debugInfo()
889
    {
890
        return [
891
            'name'    => $this->name,
892
            'rule'    => $this->rule,
893
            'route'   => $this->route,
894
            'method'  => $this->method,
895
            'vars'    => $this->vars,
896
            'option'  => $this->option,
897
            'pattern' => $this->pattern,
898
        ];
899
    }
900
}
901