Completed
Push — 6.0 ( c53317...47c10c )
by liu
06:05
created

Route::getRouteCacheKey()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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

465
        $this->ruleName->setRule($rule, /** @scrutinizer ignore-type */ $ruleItem);
Loading history...
466
    }
467
468
    /**
469
     * 读取路由
470
     * @access public
471
     * @param string $rule 路由规则
472
     * @return RuleItem[]
473
     */
474
    public function getRule(string $rule): array
475
    {
476
        return $this->ruleName->getRule($rule);
477
    }
478
479
    /**
480
     * 读取路由列表
481
     * @access public
482
     * @return array
483
     */
484
    public function getRuleList(): array
485
    {
486
        return $this->ruleName->getRuleList();
487
    }
488
489
    /**
490
     * 清空路由规则
491
     * @access public
492
     * @return void
493
     */
494
    public function clear(): void
495
    {
496
        $this->ruleName->clear();
497
498
        if ($this->group) {
499
            $this->group->clear();
500
        }
501
    }
502
503
    /**
504
     * 注册路由规则
505
     * @access public
506
     * @param string $rule   路由规则
507
     * @param mixed  $route  路由地址
508
     * @param string $method 请求类型
509
     * @return RuleItem
510
     */
511
    public function rule(string $rule, $route = null, string $method = '*'): RuleItem
512
    {
513
        return $this->group->addRule($rule, $route, $method);
514
    }
515
516
    /**
517
     * 设置跨域有效路由规则
518
     * @access public
519
     * @param Rule   $rule   路由规则
520
     * @param string $method 请求类型
521
     * @return $this
522
     */
523
    public function setCrossDomainRule(Rule $rule, string $method = '*')
524
    {
525
        if (!isset($this->cross)) {
526
            $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex);
527
        }
528
529
        $this->cross->addRuleItem($rule, $method);
530
531
        return $this;
532
    }
533
534
    /**
535
     * 注册路由分组
536
     * @access public
537
     * @param string|\Closure $name  分组名称或者参数
538
     * @param mixed           $route 分组路由
539
     * @return RuleGroup
540
     */
541
    public function group($name, $route = null): RuleGroup
542
    {
543
        if ($name instanceof Closure) {
544
            $route = $name;
545
            $name  = '';
546
        }
547
548
        return (new RuleGroup($this, $this->group, $name, $route))
549
            ->lazy($this->lazy)
550
            ->removeSlash($this->removeSlash)
551
            ->mergeRuleRegex($this->mergeRuleRegex);
552
    }
553
554
    /**
555
     * 注册路由
556
     * @access public
557
     * @param string $rule  路由规则
558
     * @param mixed  $route 路由地址
559
     * @return RuleItem
560
     */
561
    public function any(string $rule, $route): RuleItem
562
    {
563
        return $this->rule($rule, $route, '*');
564
    }
565
566
    /**
567
     * 注册GET路由
568
     * @access public
569
     * @param string $rule  路由规则
570
     * @param mixed  $route 路由地址
571
     * @return RuleItem
572
     */
573
    public function get(string $rule, $route): RuleItem
574
    {
575
        return $this->rule($rule, $route, 'GET');
576
    }
577
578
    /**
579
     * 注册POST路由
580
     * @access public
581
     * @param string $rule  路由规则
582
     * @param mixed  $route 路由地址
583
     * @return RuleItem
584
     */
585
    public function post(string $rule, $route): RuleItem
586
    {
587
        return $this->rule($rule, $route, 'POST');
588
    }
589
590
    /**
591
     * 注册PUT路由
592
     * @access public
593
     * @param string $rule  路由规则
594
     * @param mixed  $route 路由地址
595
     * @return RuleItem
596
     */
597
    public function put(string $rule, $route): RuleItem
598
    {
599
        return $this->rule($rule, $route, 'PUT');
600
    }
601
602
    /**
603
     * 注册DELETE路由
604
     * @access public
605
     * @param string $rule  路由规则
606
     * @param mixed  $route 路由地址
607
     * @return RuleItem
608
     */
609
    public function delete(string $rule, $route): RuleItem
610
    {
611
        return $this->rule($rule, $route, 'DELETE');
612
    }
613
614
    /**
615
     * 注册PATCH路由
616
     * @access public
617
     * @param string $rule  路由规则
618
     * @param mixed  $route 路由地址
619
     * @return RuleItem
620
     */
621
    public function patch(string $rule, $route): RuleItem
622
    {
623
        return $this->rule($rule, $route, 'PATCH');
624
    }
625
626
    /**
627
     * 注册OPTIONS路由
628
     * @access public
629
     * @param string $rule  路由规则
630
     * @param mixed  $route 路由地址
631
     * @return RuleItem
632
     */
633
    public function options(string $rule, $route): RuleItem
634
    {
635
        return $this->rule($rule, $route, 'OPTIONS');
636
    }
637
638
    /**
639
     * 注册资源路由
640
     * @access public
641
     * @param string $rule  路由规则
642
     * @param string $route 路由地址
643
     * @return Resource
644
     */
645
    public function resource(string $rule, string $route): Resource
646
    {
647
        return (new Resource($this, $this->group, $rule, $route, $this->rest))
648
            ->lazy($this->lazy);
649
    }
650
651
    /**
652
     * 注册视图路由
653
     * @access public
654
     * @param string $rule     路由规则
655
     * @param string $template 路由模板地址
656
     * @param array  $vars     模板变量
657
     * @return RuleItem
658
     */
659
    public function view(string $rule, string $template = '', array $vars = []): RuleItem
660
    {
661
        return $this->rule($rule, $template, 'GET')->view($vars);
662
    }
663
664
    /**
665
     * 注册重定向路由
666
     * @access public
667
     * @param string $rule   路由规则
668
     * @param string $route  路由地址
669
     * @param int    $status 状态码
670
     * @return RuleItem
671
     */
672
    public function redirect(string $rule, string $route = '', int $status = 301): RuleItem
673
    {
674
        return $this->rule($rule, $route, '*')->redirect()->status($status);
675
    }
676
677
    /**
678
     * rest方法定义和修改
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
679
     * @access public
680
     * @param string|array $name     方法名称
681
     * @param array|bool   $resource 资源
682
     * @return $this
683
     */
684
    public function rest($name, $resource = [])
685
    {
686
        if (is_array($name)) {
687
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
688
        } else {
689
            $this->rest[$name] = $resource;
690
        }
691
692
        return $this;
693
    }
694
695
    /**
696
     * 获取rest方法定义的参数
697
     * @access public
698
     * @param string $name 方法名称
699
     * @return array|null
700
     */
701
    public function getRest(string $name = null)
702
    {
703
        if (is_null($name)) {
704
            return $this->rest;
705
        }
706
707
        return $this->rest[$name] ?? null;
708
    }
709
710
    /**
711
     * 注册未匹配路由规则后的处理
712
     * @access public
713
     * @param string|Closure $route  路由地址
714
     * @param string         $method 请求类型
715
     * @return RuleItem
716
     */
717
    public function miss($route, string $method = '*'): RuleItem
718
    {
719
        return $this->group->miss($route, $method);
720
    }
721
722
    /**
723
     * 路由调度
724
     * @param Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
725
     * @param Closure $withRoute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
726
     * @return Response
727
     */
728
    public function dispatch(Request $request, $withRoute = null)
729
    {
730
        $this->request = $request;
731
        $this->host    = $this->request->host(true);
732
        $this->init();
733
734
        if ($withRoute) {
735
            //加载路由
736
            $withRoute();
737
            $dispatch = $this->check();
738
        } else {
739
            $dispatch = $this->url($this->path());
740
        }
741
742
        $dispatch->init($this->app);
743
744
        return $this->app->middleware->pipeline('route')
745
            ->send($request)
746
            ->then(function () use ($dispatch) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
747
                return $dispatch->run();
748
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
749
    }
750
751
    /**
752
     * 检测URL路由
753
     * @access public
754
     * @return Dispatch
755
     * @throws RouteNotFoundException
756
     */
757
    public function check(): Dispatch
758
    {
759
        // 自动检测域名路由
760
        $url = str_replace($this->config['pathinfo_depr'], '|', $this->path());
761
762
        $completeMatch = $this->config['route_complete_match'];
763
764
        $result = $this->checkDomain()->check($this->request, $url, $completeMatch);
765
766
        if (false === $result && !empty($this->cross)) {
767
            // 检测跨域路由
768
            $result = $this->cross->check($this->request, $url, $completeMatch);
769
        }
770
771
        if (false !== $result) {
772
            return $result;
773
        } elseif ($this->config['url_route_must']) {
774
            throw new RouteNotFoundException();
775
        }
776
777
        return $this->url($url);
778
    }
779
780
    /**
781
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
782
     * @access protected
783
     * @return string
784
     */
785
    protected function path(): string
786
    {
787
        $suffix   = $this->config['url_html_suffix'];
788
        $pathinfo = $this->request->pathinfo();
789
790
        if (false === $suffix) {
791
            // 禁止伪静态访问
792
            $path = $pathinfo;
793
        } elseif ($suffix) {
794
            // 去除正常的URL后缀
795
            $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
796
        } else {
797
            // 允许任何后缀访问
798
            $path = preg_replace('/\.' . $this->request->ext() . '$/i', '', $pathinfo);
799
        }
800
801
        return $path;
802
    }
803
804
    /**
805
     * 默认URL解析
806
     * @access public
807
     * @param string $url URL地址
808
     * @return Dispatch
809
     */
810
    public function url(string $url): UrlDispatch
811
    {
812
        return new UrlDispatch($this->request, $this->group, $url);
813
    }
814
815
    /**
816
     * 检测域名的路由规则
817
     * @access protected
818
     * @return Domain
819
     */
820
    protected function checkDomain(): Domain
821
    {
822
        $item = false;
823
824
        if (count($this->domains) > 1) {
825
            // 获取当前子域名
826
            $subDomain = $this->request->subDomain();
827
828
            $domain  = $subDomain ? explode('.', $subDomain) : [];
829
            $domain2 = $domain ? array_pop($domain) : '';
830
831
            if ($domain) {
832
                // 存在三级域名
833
                $domain3 = array_pop($domain);
834
            }
835
836
            if (isset($this->domains[$this->host])) {
837
                // 子域名配置
838
                $item = $this->domains[$this->host];
839
            } elseif (isset($this->domains[$subDomain])) {
840
                $item = $this->domains[$subDomain];
841
            } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
842
                // 泛三级域名
843
                $item      = $this->domains['*.' . $domain2];
844
                $panDomain = $domain3;
845
            } elseif (isset($this->domains['*']) && !empty($domain2)) {
846
                // 泛二级域名
847
                if ('www' != $domain2) {
848
                    $item      = $this->domains['*'];
849
                    $panDomain = $domain2;
850
                }
851
            }
852
853
            if (isset($panDomain)) {
854
                // 保存当前泛域名
855
                $this->request->setPanDomain($panDomain);
856
            }
857
        }
858
859
        if (false === $item) {
860
            // 检测全局域名规则
861
            $item = $this->domains['-'];
862
        }
863
864
        if (is_string($item)) {
865
            $item = $this->domains[$item];
866
        }
867
868
        return $item;
869
    }
870
871
    /**
872
     * URL生成 支持路由反射
873
     * @access public
874
     * @param string $url  路由地址
875
     * @param array  $vars 参数 ['a'=>'val1', 'b'=>'val2']
876
     * @return UrlBuild
877
     */
878
    public function buildUrl(string $url = '', array $vars = []): UrlBuild
879
    {
880
        return $this->app->make(UrlBuild::class, [$this, $this->app, $url, $vars], true);
881
    }
882
883
    /**
884
     * 设置全局的路由分组参数
885
     * @access public
886
     * @param string $method 方法名
887
     * @param array  $args   调用参数
888
     * @return RuleGroup
889
     */
890
    public function __call($method, $args)
891
    {
892
        return call_user_func_array([$this->group, $method], $args);
893
    }
894
}
895