Completed
Branch 6.0 (d30585)
by yun
06:27
created

Route::import()   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\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 = false;
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 6
    public function __construct(App $app)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function __construct()
Loading history...
158
    {
159 6
        $this->app      = $app;
160 6
        $this->ruleName = new RuleName();
161 6
        $this->setDefaultDomain();
162
163 6
        if (is_file($this->app->getRuntimePath() . 'route.php')) {
164
            // 读取路由映射文件
165
            $this->import(include $this->app->getRuntimePath() . 'route.php');
166
        }
167 6
    }
168
169 6
    protected function init()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function init()
Loading history...
170
    {
171 6
        $this->config = array_merge($this->config, $this->app->config->get('route'));
172
173 6
        if (!empty($this->config['middleware'])) {
174
            $this->app->middleware->import($this->config['middleware'], 'route');
175
        }
176
177 6
        $this->lazy($this->config['url_lazy_route']);
178 6
        $this->mergeRuleRegex = $this->config['route_rule_merge'];
179 6
        $this->removeSlash    = $this->config['remove_slash'];
180
181 6
        $this->group->removeSlash($this->removeSlash);
182 6
    }
183
184 6
    public function config(string $name = null)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function config()
Loading history...
185
    {
186 6
        if (is_null($name)) {
187
            return $this->config;
188
        }
189
190 6
        return $this->config[$name] ?? null;
191
    }
192
193
    /**
194
     * 设置路由域名及分组(包括资源路由)是否延迟解析
195
     * @access public
196
     * @param bool $lazy 路由是否延迟解析
197
     * @return $this
198
     */
199 6
    public function lazy(bool $lazy = true)
200
    {
201 6
        $this->lazy = $lazy;
202 6
        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 2
    public function isTest(): bool
222
    {
223 2
        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 6
    protected function setDefaultDomain(): void
246
    {
247
        // 注册默认域名
248 6
        $domain = new Domain($this);
249
250 6
        $this->domains['-'] = $domain;
251
252
        // 默认分组
253 6
        $this->group = $domain;
254 6
    }
255
256
    /**
257
     * 设置当前分组
258
     * @access public
259
     * @param RuleGroup $group 域名
260
     * @return void
261
     */
262 6
    public function setGroup(RuleGroup $group): void
263
    {
264 6
        $this->group = $group;
265 6
    }
266
267
    /**
268
     * 获取指定标识的路由分组 不指定则获取当前分组
269
     * @access public
270
     * @param string $name 分组标识
271
     * @return RuleGroup
272
     */
273 6
    public function getGroup(string $name = null)
274
    {
275 6
        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 1
    public function getRuleName(): RuleName
354
    {
355 1
        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 6
    public function getDomainBind(string $domain = null)
391
    {
392 6
        if (is_null($domain)) {
393 6
            $domain = $this->host;
394
        } elseif (false === strpos($domain, '.') && $this->request) {
395
            $domain .= '.' . $this->request->rootDomain();
396
        }
397
398 6
        if ($this->request) {
399 6
            $subDomain = $this->request->subDomain();
400
401 6
            if (strpos($subDomain, '.')) {
402
                $name = '*' . strstr($subDomain, '.');
403
            }
404
        }
405
406 6
        if (isset($this->bind[$domain])) {
407
            $result = $this->bind[$domain];
408 6
        } elseif (isset($name) && isset($this->bind[$name])) {
409
            $result = $this->bind[$name];
410 6
        } elseif (!empty($subDomain) && isset($this->bind['*'])) {
411
            $result = $this->bind['*'];
412
        } else {
413 6
            $result = null;
414
        }
415
416 6
        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 4
    public function setName(string $name, RuleItem $ruleItem, bool $first = false): void
452
    {
453 4
        $this->ruleName->setName($name, $ruleItem, $first);
454 4
    }
455
456
    /**
457
     * 保存路由规则
458
     * @access public
459
     * @param string   $rule     路由规则
460
     * @param RuleItem $ruleItem RuleItem对象
461
     * @return void
462
     */
463 6
    public function setRule(string $rule, RuleItem $ruleItem = null): void
464
    {
465 6
        $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 6
    }
467
468
    /**
469
     * 读取路由
470
     * @access public
471
     * @param string $rule 路由规则
472
     * @return RuleItem[]
473
     */
474 2
    public function getRule(string $rule): array
475
    {
476 2
        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 6
    public function rule(string $rule, $route = null, string $method = '*'): RuleItem
512
    {
513 6
        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 2
    public function group($name, $route = null): RuleGroup
542
    {
543 2
        if ($name instanceof Closure) {
544 2
            $route = $name;
545 2
            $name  = '';
546
        }
547
548 2
        return (new RuleGroup($this, $this->group, $name, $route))
549 2
            ->lazy($this->lazy)
550 2
            ->removeSlash($this->removeSlash)
551 2
            ->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 6
    public function get(string $rule, $route): RuleItem
574
    {
575 6
        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 2
    public function post(string $rule, $route): RuleItem
586
    {
587 2
        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 2
    public function put(string $rule, $route): RuleItem
598
    {
599 2
        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 1
    public function resource(string $rule, string $route): Resource
646
    {
647 1
        return (new Resource($this, $this->group, $rule, $route, $this->rest))
648 1
            ->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|bool $withRoute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
726
     * @return Response
727
     */
728 6
    public function dispatch(Request $request, $withRoute = true)
729
    {
730 6
        $this->request = $request;
731 6
        $this->host    = $this->request->host(true);
732 6
        $this->init();
733
734 6
        if ($withRoute) {
735
            //加载路由
736 6
            if ($withRoute instanceof Closure) {
737
                $withRoute();
738
            }
739 6
            $dispatch = $this->check();
740
        } else {
741
            $dispatch = $this->url($this->path());
742
        }
743
744 6
        $dispatch->init($this->app);
745
746 6
        return $this->app->middleware->pipeline('route')
747 6
            ->send($request)
748
            ->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...
749 6
                return $dispatch->run();
750 6
            });
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...
751
    }
752
753
    /**
754
     * 检测URL路由
755
     * @access public
756
     * @return Dispatch
757
     * @throws RouteNotFoundException
758
     */
759 6
    public function check(): Dispatch
760
    {
761
        // 自动检测域名路由
762 6
        $url = str_replace($this->config['pathinfo_depr'], '|', $this->path());
763
764 6
        $completeMatch = $this->config['route_complete_match'];
765
766 6
        $result = $this->checkDomain()->check($this->request, $url, $completeMatch);
767
768 6
        if (false === $result && !empty($this->cross)) {
769
            // 检测跨域路由
770
            $result = $this->cross->check($this->request, $url, $completeMatch);
771
        }
772
773 6
        if (false !== $result) {
774 6
            return $result;
775
        } elseif ($this->config['url_route_must']) {
776
            throw new RouteNotFoundException();
777
        }
778
779
        return $this->url($url);
780
    }
781
782
    /**
783
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
784
     * @access protected
785
     * @return string
786
     */
787 6
    protected function path(): string
788
    {
789 6
        $suffix   = $this->config['url_html_suffix'];
790 6
        $pathinfo = $this->request->pathinfo();
791
792 6
        if (false === $suffix) {
793
            // 禁止伪静态访问
794
            $path = $pathinfo;
795 6
        } elseif ($suffix) {
796
            // 去除正常的URL后缀
797 6
            $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
798
        } else {
799
            // 允许任何后缀访问
800
            $path = preg_replace('/\.' . $this->request->ext() . '$/i', '', $pathinfo);
801
        }
802
803 6
        return $path;
804
    }
805
806
    /**
807
     * 默认URL解析
808
     * @access public
809
     * @param string $url URL地址
810
     * @return Dispatch
811
     */
812
    public function url(string $url): UrlDispatch
813
    {
814
        return new UrlDispatch($this->request, $this->group, $url);
815
    }
816
817
    /**
818
     * 检测域名的路由规则
819
     * @access protected
820
     * @return Domain
821
     */
822 6
    protected function checkDomain(): Domain
823
    {
824 6
        $item = false;
825
826 6
        if (count($this->domains) > 1) {
827
            // 获取当前子域名
828
            $subDomain = $this->request->subDomain();
829
830
            $domain  = $subDomain ? explode('.', $subDomain) : [];
831
            $domain2 = $domain ? array_pop($domain) : '';
832
833
            if ($domain) {
834
                // 存在三级域名
835
                $domain3 = array_pop($domain);
836
            }
837
838
            if (isset($this->domains[$this->host])) {
839
                // 子域名配置
840
                $item = $this->domains[$this->host];
841
            } elseif (isset($this->domains[$subDomain])) {
842
                $item = $this->domains[$subDomain];
843
            } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
844
                // 泛三级域名
845
                $item      = $this->domains['*.' . $domain2];
846
                $panDomain = $domain3;
847
            } elseif (isset($this->domains['*']) && !empty($domain2)) {
848
                // 泛二级域名
849
                if ('www' != $domain2) {
850
                    $item      = $this->domains['*'];
851
                    $panDomain = $domain2;
852
                }
853
            }
854
855
            if (isset($panDomain)) {
856
                // 保存当前泛域名
857
                $this->request->setPanDomain($panDomain);
858
            }
859
        }
860
861 6
        if (false === $item) {
862
            // 检测全局域名规则
863 6
            $item = $this->domains['-'];
864
        }
865
866 6
        if (is_string($item)) {
867
            $item = $this->domains[$item];
868
        }
869
870 6
        return $item;
871
    }
872
873
    /**
874
     * URL生成 支持路由反射
875
     * @access public
876
     * @param string $url  路由地址
877
     * @param array  $vars 参数 ['a'=>'val1', 'b'=>'val2']
878
     * @return UrlBuild
879
     */
880
    public function buildUrl(string $url = '', array $vars = []): UrlBuild
881
    {
882
        return $this->app->make(UrlBuild::class, [$this, $this->app, $url, $vars], true);
883
    }
884
885
    /**
886
     * 设置全局的路由分组参数
887
     * @access public
888
     * @param string $method 方法名
889
     * @param array  $args   调用参数
890
     * @return RuleGroup
891
     */
892
    public function __call($method, $args)
893
    {
894
        return call_user_func_array([$this->group, $method], $args);
895
    }
896
}
897