Completed
Push — 6.0 ( d30585...7d6f90 )
by yun
02:09
created

Route::setTestMode()   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 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
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~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;
14
15
use Closure;
16
use think\exception\RouteNotFoundException;
17
use think\route\Dispatch;
18
use think\route\dispatch\Callback;
19
use think\route\dispatch\Url as UrlDispatch;
20
use think\route\Domain;
21
use think\route\Resource;
22
use think\route\Rule;
23
use think\route\RuleGroup;
24
use think\route\RuleItem;
25
use think\route\RuleName;
26
use think\route\Url as UrlBuild;
27
28
/**
29
 * 路由管理类
30
 * @package think
31
 */
32
class Route
33
{
34
    /**
35
     * REST定义
36
     * @var array
37
     */
38
    protected $rest = [
39
        'index'  => ['get', '', 'index'],
40
        'create' => ['get', '/create', 'create'],
41
        'edit'   => ['get', '/<id>/edit', 'edit'],
42
        'read'   => ['get', '/<id>', 'read'],
43
        'save'   => ['post', '', 'save'],
44
        'update' => ['put', '/<id>', 'update'],
45
        'delete' => ['delete', '/<id>', 'delete'],
46
    ];
47
48
    /**
49
     * 配置参数
50
     * @var array
51
     */
52
    protected $config = [
53
        // pathinfo分隔符
54
        'pathinfo_depr'         => '/',
55
        // 是否开启路由延迟解析
56
        'url_lazy_route'        => false,
57
        // 是否强制使用路由
58
        'url_route_must'        => false,
59
        // 合并路由规则
60
        'route_rule_merge'      => false,
61
        // 路由是否完全匹配
62
        'route_complete_match'  => false,
63
        // 去除斜杠
64
        'remove_slash'          => false,
65
        // 使用注解路由
66
        'route_annotation'      => false,
67
        // 默认的路由变量规则
68
        'default_route_pattern' => '[\w\.]+',
69
        // URL伪静态后缀
70
        'url_html_suffix'       => 'html',
71
        // 访问控制器层名称
72
        'controller_layer'      => 'controller',
73
        // 空控制器名
74
        'empty_controller'      => 'Error',
75
        // 是否使用控制器后缀
76
        'controller_suffix'     => false,
77
        // 默认控制器名
78
        'default_controller'    => 'Index',
79
        // 默认操作名
80
        'default_action'        => 'index',
81
        // 操作方法后缀
82
        'action_suffix'         => '',
83
        // 非路由变量是否使用普通参数方式(用于URL生成)
84
        'url_common_param'      => true,
85
    ];
86
87
    /**
88
     * 当前应用
89
     * @var App
90
     */
91
    protected $app;
92
93
    /**
94
     * 请求对象
95
     * @var Request
96
     */
97
    protected $request;
98
99
    /**
100
     * @var RuleName
101
     */
102
    protected $ruleName;
103
104
    /**
105
     * 当前HOST
106
     * @var string
107
     */
108
    protected $host;
109
110
    /**
111
     * 当前分组对象
112
     * @var RuleGroup
113
     */
114
    protected $group;
115
116
    /**
117
     * 路由绑定
118
     * @var array
119
     */
120
    protected $bind = [];
121
122
    /**
123
     * 域名对象
124
     * @var array
125
     */
126
    protected $domains = [];
127
128
    /**
129
     * 跨域路由规则
130
     * @var RuleGroup
131
     */
132
    protected $cross;
133
134
    /**
135
     * 路由是否延迟解析
136
     * @var bool
137
     */
138
    protected $lazy = false;
139
140
    /**
141
     * 路由是否测试模式
142
     * @var bool
143
     */
144
    protected $isTest = false;
145
146
    /**
147
     * (分组)路由规则是否合并解析
148
     * @var bool
149
     */
150
    protected $mergeRuleRegex = false;
151
152
    /**
153
     * 是否去除URL最后的斜线
154
     * @var bool
155
     */
156
    protected $removeSlash = false;
157
158 21
    public function __construct(App $app)
159
    {
160 21
        $this->app      = $app;
161 21
        $this->ruleName = new RuleName();
162 21
        $this->setDefaultDomain();
163
164 21
        if (is_file($this->app->getRuntimePath() . 'route.php')) {
165
            // 读取路由映射文件
166
            $this->import(include $this->app->getRuntimePath() . 'route.php');
167
        }
168 21
    }
169
170 21
    protected function init()
171
    {
172 21
        $this->config = array_merge($this->config, $this->app->config->get('route'));
173
174 21
        if (!empty($this->config['middleware'])) {
175
            $this->app->middleware->import($this->config['middleware'], 'route');
176
        }
177
178 21
        $this->lazy($this->config['url_lazy_route']);
179 21
        $this->mergeRuleRegex = $this->config['route_rule_merge'];
180 21
        $this->removeSlash    = $this->config['remove_slash'];
181
182 21
        $this->group->removeSlash($this->removeSlash);
183 21
    }
184
185 21
    public function config(string $name = null)
186
    {
187 21
        if (is_null($name)) {
188
            return $this->config;
189
        }
190
191 21
        return $this->config[$name] ?? null;
192
    }
193
194
    /**
195
     * 设置路由域名及分组(包括资源路由)是否延迟解析
196
     * @access public
197
     * @param bool $lazy 路由是否延迟解析
198
     * @return $this
199
     */
200 21
    public function lazy(bool $lazy = true)
201
    {
202 21
        $this->lazy = $lazy;
203 21
        return $this;
204
    }
205
206
    /**
207
     * 设置路由为测试模式
208
     * @access public
209
     * @param bool $test 路由是否测试模式
210
     * @return void
211
     */
212
    public function setTestMode(bool $test): void
213
    {
214
        $this->isTest = $test;
215
    }
216
217
    /**
218
     * 检查路由是否为测试模式
219
     * @access public
220
     * @return bool
221
     */
222 6
    public function isTest(): bool
223
    {
224 6
        return $this->isTest;
225
    }
226
227
    /**
228
     * 设置路由域名及分组(包括资源路由)是否合并解析
229
     * @access public
230
     * @param bool $merge 路由是否合并解析
231
     * @return $this
232
     */
233
    public function mergeRuleRegex(bool $merge = true)
234
    {
235
        $this->mergeRuleRegex = $merge;
236
        $this->group->mergeRuleRegex($merge);
237
238
        return $this;
239
    }
240
241
    /**
242
     * 初始化默认域名
243
     * @access protected
244
     * @return void
245
     */
246 21
    protected function setDefaultDomain(): void
247
    {
248
        // 注册默认域名
249 21
        $domain = new Domain($this);
250
251 21
        $this->domains['-'] = $domain;
252
253
        // 默认分组
254 21
        $this->group = $domain;
255 21
    }
256
257
    /**
258
     * 设置当前分组
259
     * @access public
260
     * @param RuleGroup $group 域名
261
     * @return void
262
     */
263 21
    public function setGroup(RuleGroup $group): void
264
    {
265 21
        $this->group = $group;
266 21
    }
267
268
    /**
269
     * 获取指定标识的路由分组 不指定则获取当前分组
270
     * @access public
271
     * @param string $name 分组标识
272
     * @return RuleGroup
273
     */
274 21
    public function getGroup(string $name = null)
275
    {
276 21
        return $name ? $this->ruleName->getGroup($name) : $this->group;
277
    }
278
279
    /**
280
     * 注册变量规则
281
     * @access public
282
     * @param array $pattern 变量规则
283
     * @return $this
284
     */
285
    public function pattern(array $pattern)
286
    {
287
        $this->group->pattern($pattern);
288
289
        return $this;
290
    }
291
292
    /**
293
     * 注册路由参数
294
     * @access public
295
     * @param array $option 参数
296
     * @return $this
297
     */
298
    public function option(array $option)
299
    {
300
        $this->group->option($option);
301
302
        return $this;
303
    }
304
305
    /**
306
     * 注册域名路由
307
     * @access public
308
     * @param string|array $name 子域名
309
     * @param mixed        $rule 路由规则
310
     * @return Domain
311
     */
312
    public function domain($name, $rule = null): Domain
313
    {
314
        // 支持多个域名使用相同路由规则
315
        $domainName = is_array($name) ? array_shift($name) : $name;
316
317
        if (!isset($this->domains[$domainName])) {
318
            $domain = (new Domain($this, $domainName, $rule))
319
                ->lazy($this->lazy)
320
                ->removeSlash($this->removeSlash)
321
                ->mergeRuleRegex($this->mergeRuleRegex);
322
323
            $this->domains[$domainName] = $domain;
324
        } else {
325
            $domain = $this->domains[$domainName];
326
            $domain->parseGroupRule($rule);
327
        }
328
329
        if (is_array($name) && !empty($name)) {
330
            foreach ($name as $item) {
331
                $this->domains[$item] = $domainName;
332
            }
333
        }
334
335
        // 返回域名对象
336
        return $domain;
337
    }
338
339
    /**
340
     * 获取域名
341
     * @access public
342
     * @return array
343
     */
344
    public function getDomains(): array
345
    {
346
        return $this->domains;
347
    }
348
349
    /**
350
     * 获取RuleName对象
351
     * @access public
352
     * @return RuleName
353
     */
354 3
    public function getRuleName(): RuleName
355
    {
356 3
        return $this->ruleName;
357
    }
358
359
    /**
360
     * 设置路由绑定
361
     * @access public
362
     * @param string $bind   绑定信息
363
     * @param string $domain 域名
364
     * @return $this
365
     */
366
    public function bind(string $bind, string $domain = null)
367
    {
368
        $domain = is_null($domain) ? '-' : $domain;
369
370
        $this->bind[$domain] = $bind;
371
372
        return $this;
373
    }
374
375
    /**
376
     * 读取路由绑定信息
377
     * @access public
378
     * @return array
379
     */
380
    public function getBind(): array
381
    {
382
        return $this->bind;
383
    }
384
385
    /**
386
     * 读取路由绑定
387
     * @access public
388
     * @param string $domain 域名
389
     * @return string|null
390
     */
391 21
    public function getDomainBind(string $domain = null)
392
    {
393 21
        if (is_null($domain)) {
394 21
            $domain = $this->host;
395
        } elseif (false === strpos($domain, '.') && $this->request) {
396
            $domain .= '.' . $this->request->rootDomain();
397
        }
398
399 21
        if ($this->request) {
400 21
            $subDomain = $this->request->subDomain();
401
402 21
            if (strpos($subDomain, '.')) {
403
                $name = '*' . strstr($subDomain, '.');
404
            }
405
        }
406
407 21
        if (isset($this->bind[$domain])) {
408
            $result = $this->bind[$domain];
409 21
        } elseif (isset($name) && isset($this->bind[$name])) {
410
            $result = $this->bind[$name];
411 21
        } elseif (!empty($subDomain) && isset($this->bind['*'])) {
412
            $result = $this->bind['*'];
413
        } else {
414 21
            $result = null;
415
        }
416
417 21
        return $result;
418
    }
419
420
    /**
421
     * 读取路由标识
422
     * @access public
423
     * @param string $name   路由标识
424
     * @param string $domain 域名
425
     * @param string $method 请求类型
426
     * @return RuleItem[]
427
     */
428 3
    public function getName(string $name = null, string $domain = null, string $method = '*'): array
429
    {
430 3
        return $this->ruleName->getName($name, $domain, $method);
431
    }
432
433
    /**
434
     * 批量导入路由标识
435
     * @access public
436
     * @param array $name 路由标识
437
     * @return $this
438
     */
439
    public function import(array $name): void
440
    {
441
        $this->ruleName->import($name);
442
    }
443
444
    /**
445
     * 注册路由标识
446
     * @access public
447
     * @param string   $name     路由标识
448
     * @param RuleItem $ruleItem 路由规则
449
     * @param bool     $first    是否优先
450
     * @return void
451
     */
452 12
    public function setName(string $name, RuleItem $ruleItem, bool $first = false): void
453
    {
454 12
        $this->ruleName->setName($name, $ruleItem, $first);
455 12
    }
456
457
    /**
458
     * 保存路由规则
459
     * @access public
460
     * @param string   $rule     路由规则
461
     * @param RuleItem $ruleItem RuleItem对象
462
     * @return void
463
     */
464 18
    public function setRule(string $rule, RuleItem $ruleItem = null): void
465
    {
466 18
        $this->ruleName->setRule($rule, $ruleItem);
0 ignored issues
show
Bug introduced by
It seems like $ruleItem can also be of type null; however, parameter $ruleItem of think\route\RuleName::setRule() does only seem to accept think\route\RuleItem, 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

466
        $this->ruleName->setRule($rule, /** @scrutinizer ignore-type */ $ruleItem);
Loading history...
467 18
    }
468
469
    /**
470
     * 读取路由
471
     * @access public
472
     * @param string $rule 路由规则
473
     * @return RuleItem[]
474
     */
475 3
    public function getRule(string $rule): array
476
    {
477 3
        return $this->ruleName->getRule($rule);
478
    }
479
480
    /**
481
     * 读取路由列表
482
     * @access public
483
     * @return array
484
     */
485
    public function getRuleList(): array
486
    {
487
        return $this->ruleName->getRuleList();
488
    }
489
490
    /**
491
     * 清空路由规则
492
     * @access public
493
     * @return void
494
     */
495
    public function clear(): void
496
    {
497
        $this->ruleName->clear();
498
499
        if ($this->group) {
500
            $this->group->clear();
501
        }
502
    }
503
504
    /**
505
     * 注册路由规则
506
     * @access public
507
     * @param string $rule   路由规则
508
     * @param mixed  $route  路由地址
509
     * @param string $method 请求类型
510
     * @return RuleItem
511
     */
512 18
    public function rule(string $rule, $route = null, string $method = '*'): RuleItem
513
    {
514 18
        return $this->group->addRule($rule, $route, $method);
515
    }
516
517
    /**
518
     * 设置跨域有效路由规则
519
     * @access public
520
     * @param Rule   $rule   路由规则
521
     * @param string $method 请求类型
522
     * @return $this
523
     */
524
    public function setCrossDomainRule(Rule $rule, string $method = '*')
525
    {
526
        if (!isset($this->cross)) {
527
            $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex);
528
        }
529
530
        $this->cross->addRuleItem($rule, $method);
531
532
        return $this;
533
    }
534
535
    /**
536
     * 注册路由分组
537
     * @access public
538
     * @param string|\Closure $name  分组名称或者参数
539
     * @param mixed           $route 分组路由
540
     * @return RuleGroup
541
     */
542 6
    public function group($name, $route = null): RuleGroup
543
    {
544 6
        if ($name instanceof Closure) {
545 6
            $route = $name;
546 6
            $name  = '';
547
        }
548
549 6
        return (new RuleGroup($this, $this->group, $name, $route))
550 6
            ->lazy($this->lazy)
551 6
            ->removeSlash($this->removeSlash)
552 6
            ->mergeRuleRegex($this->mergeRuleRegex);
553
    }
554
555
    /**
556
     * 注册路由
557
     * @access public
558
     * @param string $rule  路由规则
559
     * @param mixed  $route 路由地址
560
     * @return RuleItem
561
     */
562
    public function any(string $rule, $route): RuleItem
563
    {
564
        return $this->rule($rule, $route, '*');
565
    }
566
567
    /**
568
     * 注册GET路由
569
     * @access public
570
     * @param string $rule  路由规则
571
     * @param mixed  $route 路由地址
572
     * @return RuleItem
573
     */
574 18
    public function get(string $rule, $route): RuleItem
575
    {
576 18
        return $this->rule($rule, $route, 'GET');
577
    }
578
579
    /**
580
     * 注册POST路由
581
     * @access public
582
     * @param string $rule  路由规则
583
     * @param mixed  $route 路由地址
584
     * @return RuleItem
585
     */
586 6
    public function post(string $rule, $route): RuleItem
587
    {
588 6
        return $this->rule($rule, $route, 'POST');
589
    }
590
591
    /**
592
     * 注册PUT路由
593
     * @access public
594
     * @param string $rule  路由规则
595
     * @param mixed  $route 路由地址
596
     * @return RuleItem
597
     */
598 6
    public function put(string $rule, $route): RuleItem
599
    {
600 6
        return $this->rule($rule, $route, 'PUT');
601
    }
602
603
    /**
604
     * 注册DELETE路由
605
     * @access public
606
     * @param string $rule  路由规则
607
     * @param mixed  $route 路由地址
608
     * @return RuleItem
609
     */
610
    public function delete(string $rule, $route): RuleItem
611
    {
612
        return $this->rule($rule, $route, 'DELETE');
613
    }
614
615
    /**
616
     * 注册PATCH路由
617
     * @access public
618
     * @param string $rule  路由规则
619
     * @param mixed  $route 路由地址
620
     * @return RuleItem
621
     */
622
    public function patch(string $rule, $route): RuleItem
623
    {
624
        return $this->rule($rule, $route, 'PATCH');
625
    }
626
627
    /**
628
     * 注册OPTIONS路由
629
     * @access public
630
     * @param string $rule  路由规则
631
     * @param mixed  $route 路由地址
632
     * @return RuleItem
633
     */
634
    public function options(string $rule, $route): RuleItem
635
    {
636
        return $this->rule($rule, $route, 'OPTIONS');
637
    }
638
639
    /**
640
     * 注册资源路由
641
     * @access public
642
     * @param string $rule  路由规则
643
     * @param string $route 路由地址
644
     * @return Resource
645
     */
646 3
    public function resource(string $rule, string $route): Resource
647
    {
648 3
        return (new Resource($this, $this->group, $rule, $route, $this->rest))
649 3
            ->lazy($this->lazy);
650
    }
651
652
    /**
653
     * 注册视图路由
654
     * @access public
655
     * @param string $rule     路由规则
656
     * @param string $template 路由模板地址
657
     * @param array  $vars     模板变量
658
     * @return RuleItem
659
     */
660
    public function view(string $rule, string $template = '', array $vars = []): RuleItem
661
    {
662
        return $this->rule($rule, $template, 'GET')->view($vars);
663
    }
664
665
    /**
666
     * 注册重定向路由
667
     * @access public
668
     * @param string $rule   路由规则
669
     * @param string $route  路由地址
670
     * @param int    $status 状态码
671
     * @return RuleItem
672
     */
673
    public function redirect(string $rule, string $route = '', int $status = 301): RuleItem
674
    {
675
        return $this->rule($rule, $route, '*')->redirect()->status($status);
676
    }
677
678
    /**
679
     * rest方法定义和修改
680
     * @access public
681
     * @param string|array $name     方法名称
682
     * @param array|bool   $resource 资源
683
     * @return $this
684
     */
685
    public function rest($name, $resource = [])
686
    {
687
        if (is_array($name)) {
688
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
689
        } else {
690
            $this->rest[$name] = $resource;
691
        }
692
693
        return $this;
694
    }
695
696
    /**
697
     * 获取rest方法定义的参数
698
     * @access public
699
     * @param string $name 方法名称
700
     * @return array|null
701
     */
702
    public function getRest(string $name = null)
703
    {
704
        if (is_null($name)) {
705
            return $this->rest;
706
        }
707
708
        return $this->rest[$name] ?? null;
709
    }
710
711
    /**
712
     * 注册未匹配路由规则后的处理
713
     * @access public
714
     * @param string|Closure $route  路由地址
715
     * @param string         $method 请求类型
716
     * @return RuleItem
717
     */
718
    public function miss($route, string $method = '*'): RuleItem
719
    {
720
        return $this->group->miss($route, $method);
721
    }
722
723
    /**
724
     * 路由调度
725
     * @param Request $request
726
     * @param Closure|bool $withRoute
727
     * @return Response
728
     */
729 21
    public function dispatch(Request $request, $withRoute = true)
730
    {
731 21
        $this->request = $request;
732 21
        $this->host    = $this->request->host(true);
733 21
        $this->init();
734
735 21
        if ($withRoute) {
736
            //加载路由
737 21
            if ($withRoute instanceof Closure) {
738
                $withRoute();
739
            }
740 21
            $dispatch = $this->check();
741
        } else {
742
            $dispatch = $this->url($this->path());
743
        }
744
745 21
        $dispatch->init($this->app);
746
747 21
        return $this->app->middleware->pipeline('route')
748 21
            ->send($request)
749
            ->then(function () use ($dispatch) {
750 21
                return $dispatch->run();
751 21
            });
752
    }
753
754
    /**
755
     * 检测URL路由
756
     * @access public
757
     * @return Dispatch|false
758
     * @throws RouteNotFoundException
759
     */
760 21
    public function check()
761
    {
762
        // 自动检测域名路由
763 21
        $url = str_replace($this->config['pathinfo_depr'], '|', $this->path());
764
765 21
        $completeMatch = $this->config['route_complete_match'];
766
767 21
        $result = $this->checkDomain()->check($this->request, $url, $completeMatch);
768
769 21
        if (false === $result && !empty($this->cross)) {
770
            // 检测跨域路由
771
            $result = $this->cross->check($this->request, $url, $completeMatch);
772
        }
773
774 21
        if (false !== $result) {
775 18
            return $result;
776 9
        } elseif ($this->config['url_route_must']) {
777
            throw new RouteNotFoundException();
778
        }
779
780 9
        return $this->url($url);
781
    }
782
783
    /**
784
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
785
     * @access protected
786
     * @return string
787
     */
788 21
    protected function path(): string
789
    {
790 21
        $suffix   = $this->config['url_html_suffix'];
791 21
        $pathinfo = $this->request->pathinfo();
792
793 21
        if (false === $suffix) {
794
            // 禁止伪静态访问
795
            $path = $pathinfo;
796 21
        } elseif ($suffix) {
797
            // 去除正常的URL后缀
798 21
            $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
799
        } else {
800
            // 允许任何后缀访问
801
            $path = preg_replace('/\.' . $this->request->ext() . '$/i', '', $pathinfo);
802
        }
803
804 21
        return $path;
805
    }
806
807
    /**
808
     * 默认URL解析
809
     * @access public
810
     * @param string $url URL地址
811
     * @return Dispatch
812
     */
813 9
    public function url(string $url): Dispatch
814
    {
815 9
        if ($this->request->method() == 'OPTIONS') {
816
            // 自动响应options请求
817
            return new Callback($this->request, $this->group, function () {
818 6
                return Response::create('', 'html', 204)->header(['Allow' => 'GET, POST, PUT, DELETE']);
819 6
            });
820
        }
821
822 3
        return new UrlDispatch($this->request, $this->group, $url);
823
    }
824
825
    /**
826
     * 检测域名的路由规则
827
     * @access protected
828
     * @return Domain
829
     */
830 21
    protected function checkDomain(): Domain
831
    {
832 21
        $item = false;
833
834 21
        if (count($this->domains) > 1) {
835
            // 获取当前子域名
836
            $subDomain = $this->request->subDomain();
837
838
            $domain  = $subDomain ? explode('.', $subDomain) : [];
839
            $domain2 = $domain ? array_pop($domain) : '';
840
841
            if ($domain) {
842
                // 存在三级域名
843
                $domain3 = array_pop($domain);
844
            }
845
846
            if (isset($this->domains[$this->host])) {
847
                // 子域名配置
848
                $item = $this->domains[$this->host];
849
            } elseif (isset($this->domains[$subDomain])) {
850
                $item = $this->domains[$subDomain];
851
            } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
852
                // 泛三级域名
853
                $item      = $this->domains['*.' . $domain2];
854
                $panDomain = $domain3;
855
            } elseif (isset($this->domains['*']) && !empty($domain2)) {
856
                // 泛二级域名
857
                if ('www' != $domain2) {
858
                    $item      = $this->domains['*'];
859
                    $panDomain = $domain2;
860
                }
861
            }
862
863
            if (isset($panDomain)) {
864
                // 保存当前泛域名
865
                $this->request->setPanDomain($panDomain);
866
            }
867
        }
868
869 21
        if (false === $item) {
870
            // 检测全局域名规则
871 21
            $item = $this->domains['-'];
872
        }
873
874 21
        if (is_string($item)) {
875
            $item = $this->domains[$item];
876
        }
877
878 21
        return $item;
879
    }
880
881
    /**
882
     * URL生成 支持路由反射
883
     * @access public
884
     * @param string $url  路由地址
885
     * @param array  $vars 参数 ['a'=>'val1', 'b'=>'val2']
886
     * @return UrlBuild
887
     */
888
    public function buildUrl(string $url = '', array $vars = []): UrlBuild
889
    {
890
        return $this->app->make(UrlBuild::class, [$this, $this->app, $url, $vars], true);
891
    }
892
893
    /**
894
     * 设置全局的路由分组参数
895
     * @access public
896
     * @param string $method 方法名
897
     * @param array  $args   调用参数
898
     * @return RuleGroup
899
     */
900
    public function __call($method, $args)
901
    {
902
        return call_user_func_array([$this->group, $method], $args);
903
    }
904
}
905