Passed
Push — 8.0 ( cd7e8a...0aac54 )
by liu
11:57 queued 09:21
created

Route::options()   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 2
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~2023 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\Domain;
19
use think\route\Resource;
20
use think\route\ResourceRegister;
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
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
        'url_case_sensitive'    => false,
60
        // 合并路由规则
61
        'route_rule_merge'      => false,
62
        // 路由是否完全匹配
63
        'route_complete_match'  => false,
64
        // 去除斜杠
65
        'remove_slash'          => false,
66
        // 使用注解路由
67
        'route_annotation'      => false,
68
        // 默认的路由变量规则
69
        'default_route_pattern' => '[\w\.]+',
70
        // URL伪静态后缀
71
        'url_html_suffix'       => 'html',
72
        // 访问控制器层名称
73
        'controller_layer'      => 'controller',
74
        // 空控制器名
75
        'empty_controller'      => 'Error',
76
        // 是否使用控制器后缀
77
        'controller_suffix'     => false,
78
        // 默认路由 [路由规则, 路由地址]
79
        'default_route'         => [],
80
        // 默认模块名
81
        'default_module'        => 'index',
82
        // 默认控制器名
83
        'default_controller'    => 'Index',
84
        // 默认操作名
85
        'default_action'        => 'index',
86
        // 操作方法后缀
87
        'action_suffix'         => '',
88
        // 非路由变量是否使用普通参数方式(用于URL生成)
89
        'url_common_param'      => true,
90
    ];
91
92
    /**
93
     * 请求对象
94
     * @var Request
95
     */
96
    protected $request;
97
98
    /**
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 Domain[]
118
     */
119
    protected $domains = [];
120
121
    /**
122
     * 跨域路由规则
123
     * @var RuleGroup
124
     */
125
    protected $cross;
126
127
    /**
128
     * 路由是否延迟解析
129
     * @var bool
130
     */
131
    protected $lazy = false;
132
133
    /**
134
     * (分组)路由规则是否合并解析
135
     * @var bool
136
     */
137
    protected $mergeRuleRegex = false;
138
139
    /**
140
     * 是否去除URL最后的斜线
141
     * @var bool
142
     */
143
    protected $removeSlash = false;
144
145 27
    public function __construct(protected App $app)
146
    {
147 27
        $this->ruleName = new RuleName();
148 27
        $this->setDefaultDomain();
149
150 27
        if (is_file($this->app->getRuntimePath() . 'route.php')) {
151
            // 读取路由映射文件
152
            $this->import(include $this->app->getRuntimePath() . 'route.php');
153
        }
154
155 27
        $this->config = array_merge($this->config, $this->app->config->get('route'));
0 ignored issues
show
Bug introduced by
It seems like $this->app->config->get('route') can also be of type null; however, parameter $arrays of array_merge() does only seem to accept array, 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

155
        $this->config = array_merge($this->config, /** @scrutinizer ignore-type */ $this->app->config->get('route'));
Loading history...
156
157 27
        $this->init();
158
    }
159
160 27
    protected function init()
161
    {
162 27
        if (!empty($this->config['middleware'])) {
163
            $this->app->middleware->import($this->config['middleware'], 'route');
164
        }
165
166 27
        $this->lazy($this->config['url_lazy_route']);
167 27
        $this->mergeRuleRegex = $this->config['route_rule_merge'];
168 27
        $this->removeSlash    = $this->config['remove_slash'];
169
170 27
        $this->group->removeSlash($this->removeSlash);
171
172
        // 注册全局MISS路由
173 27
        $this->miss(function () {
174 3
            return Response::create('', 'html', 204)->header(['Allow' => 'GET, POST, PUT, DELETE']);
175 27
        }, 'options');
176
    }
177
178 27
    public function config(?string $name = null)
179
    {
180 27
        if (is_null($name)) {
181
            return $this->config;
182
        }
183
184 27
        return $this->config[$name] ?? null;
185
    }
186
187
    /**
188
     * 设置路由域名及分组(包括资源路由)是否延迟解析
189
     * @access public
190
     * @param bool $lazy 路由是否延迟解析
191
     * @return $this
192
     */
193 27
    public function lazy(bool $lazy = true)
194
    {
195 27
        $this->lazy = $lazy;
196 27
        return $this;
197
    }
198
199
    /**
200
     * 设置路由域名及分组(包括资源路由)是否合并解析
201
     * @access public
202
     * @param bool $merge 路由是否合并解析
203
     * @return $this
204
     */
205
    public function mergeRuleRegex(bool $merge = true)
206
    {
207
        $this->mergeRuleRegex = $merge;
208
        $this->group->mergeRuleRegex($merge);
209
210
        return $this;
211
    }
212
213
    /**
214
     * 初始化默认域名
215
     * @access protected
216
     * @return void
217
     */
218 27
    protected function setDefaultDomain(): void
219
    {
220
        // 注册默认域名
221 27
        $domain = new Domain($this);
222
223 27
        $this->domains['-'] = $domain;
224
225
        // 默认分组
226 27
        $this->group = $domain;
227
    }
228
229
    /**
230
     * 设置当前分组
231
     * @access public
232
     * @param RuleGroup $group 域名
233
     * @return void
234
     */
235 27
    public function setGroup(RuleGroup $group): void
236
    {
237 27
        $this->group = $group;
238
    }
239
240
    /**
241
     * 获取指定标识的路由分组 不指定则获取当前分组
242
     * @access public
243
     * @param string $name 分组标识
244
     * @return RuleGroup
245
     */
246 27
    public function getGroup(?string $name = null)
247
    {
248 27
        return $name ? $this->ruleName->getGroup($name) : $this->group;
249
    }
250
251
    /**
252
     * 注册变量规则
253
     * @access public
254
     * @param array $pattern 变量规则
255
     * @return $this
256
     */
257
    public function pattern(array $pattern)
258
    {
259
        $this->group->pattern($pattern);
260
261
        return $this;
262
    }
263
264
    /**
265
     * 注册路由参数
266
     * @access public
267
     * @param array $option 参数
268
     * @return $this
269
     */
270
    public function option(array $option)
271
    {
272
        $this->group->option($option);
273
274
        return $this;
275
    }
276
277
    /**
278
     * 注册域名路由
279
     * @access public
280
     * @param string|array $name 子域名
281
     * @param mixed        $rule 路由规则
282
     * @return Domain
283
     */
284 3
    public function domain(string | array $name, $rule = null): Domain
285
    {
286
        // 支持多个域名使用相同路由规则
287 3
        $domainName = is_array($name) ? array_shift($name) : $name;
0 ignored issues
show
introduced by
The condition is_array($name) is always true.
Loading history...
288
289 3
        if (!isset($this->domains[$domainName])) {
290 3
            $domain = (new Domain($this, $domainName, $rule, $this->lazy))
291 3
                ->removeSlash($this->removeSlash)
292 3
                ->mergeRuleRegex($this->mergeRuleRegex);
293
294 3
            $this->domains[$domainName] = $domain;
295
        } else {
296
            $domain = $this->domains[$domainName];
297
            $domain->parseGroupRule($rule);
298
        }
299
300 3
        if (is_array($name) && !empty($name)) {
301
            foreach ($name as $item) {
302
                $this->domains[$item] = $domainName;
303
            }
304
        }
305
306
        // 返回域名对象
307 3
        return $domain;
308
    }
309
310
    /**
311
     * 获取域名
312
     * @access public
313
     * @return array
314
     */
315
    public function getDomains(): array
316
    {
317
        return $this->domains;
318
    }
319
320
    /**
321
     * 获取RuleName对象
322
     * @access public
323
     * @return RuleName
324
     */
325
    public function getRuleName(): RuleName
326
    {
327
        return $this->ruleName;
328
    }
329
330
    /**
331
     * 读取路由标识
332
     * @access public
333
     * @param string $name   路由标识
334
     * @param string $domain 域名
335
     * @param string $method 请求类型
336
     * @return array
337
     */
338
    public function getName(?string $name = null, ?string $domain = null, string $method = '*'): array
339
    {
340
        return $this->ruleName->getName($name, $domain, $method);
341
    }
342
343
    /**
344
     * 批量导入路由标识
345
     * @access public
346
     * @param array $name 路由标识
347
     * @return void
348
     */
349
    public function import(array $name): void
350
    {
351
        $this->ruleName->import($name);
352
    }
353
354
    /**
355
     * 注册路由标识
356
     * @access public
357
     * @param string   $name     路由标识
358
     * @param RuleItem $ruleItem 路由规则
359
     * @param bool     $first    是否优先
360
     * @return void
361
     */
362 9
    public function setName(string $name, RuleItem $ruleItem, bool $first = false): void
363
    {
364 9
        $this->ruleName->setName($name, $ruleItem, $first);
365
    }
366
367
    /**
368
     * 保存路由规则
369
     * @access public
370
     * @param string   $rule     路由规则
371
     * @param RuleItem $ruleItem RuleItem对象
372
     * @return void
373
     */
374 27
    public function setRule(string $rule, ?RuleItem $ruleItem = null): void
375
    {
376 27
        $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

376
        $this->ruleName->setRule($rule, /** @scrutinizer ignore-type */ $ruleItem);
Loading history...
377
    }
378
379
    /**
380
     * 读取路由
381
     * @access public
382
     * @param string $rule 路由规则
383
     * @return RuleItem[]
384
     */
385
    public function getRule(string $rule): array
386
    {
387
        return $this->ruleName->getRule($rule);
388
    }
389
390
    /**
391
     * 读取路由列表
392
     * @access public
393
     * @return array
394
     */
395
    public function getRuleList(): array
396
    {
397
        return $this->ruleName->getRuleList();
398
    }
399
400
    /**
401
     * 清空路由规则
402
     * @access public
403
     * @return void
404
     */
405
    public function clear(): void
406
    {
407
        $this->ruleName->clear();
408
409
        if ($this->group) {
410
            $this->group->clear();
411
        }
412
    }
413
414
    /**
415
     * 注册路由规则
416
     * @access public
417
     * @param string $rule   路由规则
418
     * @param mixed  $route  路由地址
419
     * @param string $method 请求类型
420
     * @return RuleItem
421
     */
422 24
    public function rule(string $rule, $route = null, string $method = '*'): RuleItem
423
    {
424 24
        return $this->group->addRule($rule, $route, $method);
425
    }
426
427
    /**
428
     * 设置路由规则全局有效
429
     * @access public
430
     * @param Rule   $rule   路由规则
431
     * @return $this
432
     */
433
    public function setCrossDomainRule(Rule $rule)
434
    {
435
        if (!isset($this->cross)) {
436
            $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex);
437
        }
438
439
        $this->cross->addRuleItem($rule);
440
441
        return $this;
442
    }
443
444
    /**
445
     * 注册路由分组
446
     * @access public
447
     * @param string|Closure $name  分组名称或者参数
448
     * @param mixed           $route 分组路由
449
     * @return RuleGroup
450
     */
451 3
    public function group(string | Closure $name, $route = null): RuleGroup
452
    {
453 3
        if ($name instanceof Closure) {
454 3
            $route = $name;
455 3
            $name  = '';
456
        }
457
458 3
        return (new RuleGroup($this, $this->group, $name, $route, $this->lazy))
459 3
            ->removeSlash($this->removeSlash)
460 3
            ->mergeRuleRegex($this->mergeRuleRegex);
461
    }
462
463
    /**
464
     * 注册路由
465
     * @access public
466
     * @param string $rule  路由规则
467
     * @param mixed  $route 路由地址
468
     * @return RuleItem
469
     */
470
    public function any(string $rule, $route): RuleItem
471
    {
472
        return $this->rule($rule, $route, '*');
473
    }
474
475
    /**
476
     * 注册GET路由
477
     * @access public
478
     * @param string $rule  路由规则
479
     * @param mixed  $route 路由地址
480
     * @return RuleItem
481
     */
482 18
    public function get(string $rule, $route): RuleItem
483
    {
484 18
        return $this->rule($rule, $route, 'GET');
485
    }
486
487
    /**
488
     * 注册POST路由
489
     * @access public
490
     * @param string $rule  路由规则
491
     * @param mixed  $route 路由地址
492
     * @return RuleItem
493
     */
494 3
    public function post(string $rule, $route): RuleItem
495
    {
496 3
        return $this->rule($rule, $route, 'POST');
497
    }
498
499
    /**
500
     * 注册PUT路由
501
     * @access public
502
     * @param string $rule  路由规则
503
     * @param mixed  $route 路由地址
504
     * @return RuleItem
505
     */
506 3
    public function put(string $rule, $route): RuleItem
507
    {
508 3
        return $this->rule($rule, $route, 'PUT');
509
    }
510
511
    /**
512
     * 注册DELETE路由
513
     * @access public
514
     * @param string $rule  路由规则
515
     * @param mixed  $route 路由地址
516
     * @return RuleItem
517
     */
518
    public function delete(string $rule, $route): RuleItem
519
    {
520
        return $this->rule($rule, $route, 'DELETE');
521
    }
522
523
    /**
524
     * 注册PATCH路由
525
     * @access public
526
     * @param string $rule  路由规则
527
     * @param mixed  $route 路由地址
528
     * @return RuleItem
529
     */
530
    public function patch(string $rule, $route): RuleItem
531
    {
532
        return $this->rule($rule, $route, 'PATCH');
533
    }
534
535
    /**
536
     * 注册HEAD路由
537
     * @access public
538
     * @param string $rule  路由规则
539
     * @param mixed  $route 路由地址
540
     * @return RuleItem
541
     */
542
    public function head(string $rule, $route): RuleItem
543
    {
544
        return $this->rule($rule, $route, 'HEAD');
545
    }
546
547
    /**
548
     * 注册OPTIONS路由
549
     * @access public
550
     * @param string $rule  路由规则
551
     * @param mixed  $route 路由地址
552
     * @return RuleItem
553
     */
554
    public function options(string $rule, $route): RuleItem
555
    {
556
        return $this->rule($rule, $route, 'OPTIONS');
557
    }
558
559
    /**
560
     * 注册资源路由
561
     * @access public
562
     * @param string $rule  路由规则
563
     * @param string $route 路由地址
564
     * @return Resource|ResourceRegister
565
     */
566
    public function resource(string $rule, string $route)
567
    {
568
        $resource = new Resource($this, $this->group, $rule, $route, $this->rest);
569
570
        if (!$this->lazy) {
571
            return new ResourceRegister($resource);
572
        }
573
574
        return $resource;
575
    }
576
577
    /**
578
     * 注册视图路由
579
     * @access public
580
     * @param string $rule     路由规则
581
     * @param string $template 路由模板地址
582
     * @param array  $vars     模板变量
583
     * @return RuleItem
584
     */
585 3
    public function view(string $rule, string $template = '', array $vars = []): RuleItem
586
    {
587 3
        return $this->rule($rule, function () use ($vars, $template) {
588 3
            return Response::create($template, 'view')->assign($vars);
0 ignored issues
show
Bug introduced by
The method assign() does not exist on think\Response. It seems like you code against a sub-type of think\Response such as think\response\View. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

588
            return Response::create($template, 'view')->/** @scrutinizer ignore-call */ assign($vars);
Loading history...
589 3
        }, 'GET');
590
    }
591
592
    /**
593
     * 注册重定向路由
594
     * @access public
595
     * @param string $rule   路由规则
596
     * @param string $route  路由地址
597
     * @param int    $status 状态码
598
     * @return RuleItem
599
     */
600 3
    public function redirect(string $rule, string $route = '', int $status = 301): RuleItem
601
    {
602 3
        return $this->rule($rule, function (Request $request) use ($status, $route) {
603 3
            $search  = $replace  = [];
604 3
            $matches = $request->rule()->getVars();
605
606 3
            foreach ($matches as $key => $value) {
607
                $search[]  = '<' . $key . '>';
608
                $replace[] = $value;
609
                $search[]  = '{' . $key . '}';
610
                $replace[] = $value;
611
                $search[]  = ':' . $key;
612
                $replace[] = $value;
613
            }
614
615 3
            $route = str_replace($search, $replace, $route);
616 3
            return Response::create($route, 'redirect')->code($status);
617 3
        }, '*');
618
    }
619
620
    /**
621
     * rest方法定义和修改
622
     * @access public
623
     * @param string|array $name     方法名称
624
     * @param array|bool   $resource 资源
625
     * @return $this
626
     */
627
    public function rest(string | array $name, array | bool $resource = [])
628
    {
629
        if (is_array($name)) {
0 ignored issues
show
introduced by
The condition is_array($name) is always true.
Loading history...
630
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
631
        } else {
632
            $this->rest[$name] = $resource;
633
        }
634
635
        return $this;
636
    }
637
638
    /**
639
     * 获取rest方法定义的参数
640
     * @access public
641
     * @param string $name 方法名称
642
     * @return array|null
643
     */
644
    public function getRest(?string $name = null)
645
    {
646
        if (is_null($name)) {
647
            return $this->rest;
648
        }
649
650
        return $this->rest[$name] ?? null;
651
    }
652
653
    /**
654
     * 注册未匹配路由规则后的处理
655
     * @access public
656
     * @param string|Closure $route  路由地址
657
     * @param string         $method 请求类型
658
     * @return RuleItem
659
     */
660 27
    public function miss(string | Closure $route, string $method = '*'): RuleItem
661
    {
662 27
        return $this->group->miss($route, $method);
663
    }
664
665
    /**
666
     * 路由调度
667
     * @param Request $request
668
     * @param Closure|bool $withRoute
669
     * @return Response
670
     */
671 27
    public function dispatch(Request $request, Closure | bool $withRoute = true)
672
    {
673 27
        $this->request = $request;
674 27
        $this->host    = $this->request->host(true);
675 27
        $completeMatch = (bool) $this->config['route_complete_match'];
676 27
        $url           = str_replace($this->config['pathinfo_depr'], '|', $this->path());
677
678 27
        if ($withRoute) {
679 27
            if ($withRoute instanceof Closure) {
680
                $withRoute();
681
            }
682
            // 路由检测
683 27
            $dispatch = $this->check($url, $completeMatch);
684
        }
685
686 27
        if (empty($dispatch)) {
687
            // 默认URL调度
688 3
            $dispatch = $this->checkUrlDispatch($url);
689
        }
690
691 27
        $dispatch->init($this->app);
692
693 27
        return $this->app->middleware->pipeline('route')
694 27
            ->send($request)
695 27
            ->then(function () use ($dispatch) {
696 27
                return $dispatch->run();
697 27
            });
698
    }
699
700
    /**
701
     * 检测URL路由
702
     * @access public
703
     * @param  bool $completeMatch
704
     * @return Dispatch|false
705
     * @throws RouteNotFoundException
706
     */
707 27
    public function check(string $url, bool $completeMatch = false)
708
    {
709
        // 检测域名路由
710 27
        $result = $this->checkDomain()->check($this->request, $url, $completeMatch);
711
712 27
        if (false === $result && !empty($this->cross)) {
713
            // 检测跨域路由
714
            $result = $this->cross->check($this->request, $url, $completeMatch);
715
        }
716
717 27
        if (false === $result && $this->config['url_route_must']) {
718
            // 开启强制路由
719
            throw new RouteNotFoundException();
720
        }
721
722 27
        return $result;
723
    }
724
725
    /**
726
     * 获取当前请求URL的pathinfo信息(不含URL后缀)
727
     * @access protected
728
     * @return string
729
     */
730 27
    protected function path(): string
731
    {
732 27
        $suffix   = $this->config['url_html_suffix'];
733 27
        $pathinfo = $this->request->pathinfo();
734
735 27
        if (false === $suffix) {
736
            // 禁止伪静态访问
737
            $path = $pathinfo;
738 27
        } elseif ($suffix) {
739
            // 去除正常的URL后缀
740 27
            $path = preg_replace('/\.(' . ltrim($suffix, '.') . ')$/i', '', $pathinfo);
741
        } else {
742
            // 允许任何后缀访问
743
            $path = preg_replace('/\.' . $this->request->ext() . '$/i', '', $pathinfo);
744
        }
745
746 27
        return $path;
747
    }
748
749
    /**
750
     * 自动多模块URL路由 如使用多模块在路由定义文件最后定义
751
     * @access public
752
     * @param  string $rule    路由规则
753
     * @param  mixed  $route   路由地址
754
     * @return RuleItem
755
     */
756
    public function auto(string $rule = '[:module]/[:controller]/[:action]', $route = ':module/:controller/:action'): RuleItem
757
    {
758
        return $this->rule($rule, $route)
759
            ->pattern([
760
                'module'     => '[A-Za-z0-9\.\_]+',
761
                'controller' => '[A-Za-z0-9\.\_]+',
762
                'action'     => '[A-Za-z0-9\_]+',
763
            ])->default([
764
                'module'     => $this->config['default_module'],
765
                'controller' => $this->config['default_controller'],
766
                'action'     => $this->config['default_action'],
767
            ]);
768
    }
769
770
    /**
771
     * 检测默认URL解析路由
772
     * @access public
773
     * @param  string   $url URL
774
     * @return Dispatch
775
     */
776 3
    protected function checkUrlDispatch(string $url): Dispatch
777
    {
778 3
        return $this->group->bind()->checkBind($this->request, $url);
779
    }
780
781
    /**
782
     * 检测域名的路由规则
783
     * @access protected
784
     * @return Domain
785
     */
786 27
    protected function checkDomain(): Domain
787
    {
788 27
        $item = false;
789
790 27
        if (count($this->domains) > 1) {
791
            // 获取当前子域名
792 3
            $subDomain = $this->request->subDomain();
793 3
            $domain    = $subDomain ? explode('.', $subDomain) : [];
794 3
            $domain2   = $domain ? array_pop($domain) : '';
795
796 3
            if ($domain) {
797
                // 存在三级域名
798
                $domain3 = array_pop($domain);
799
            }
800
801 3
            if (isset($this->domains[$this->host])) {
802
                // 子域名配置
803
                $item = $this->domains[$this->host];
804 3
            } elseif (isset($this->domains[$subDomain])) {
805 3
                $item = $this->domains[$subDomain];
806
            } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
807
                // 泛三级域名
808
                $item      = $this->domains['*.' . $domain2];
809
                $panDomain = $domain3;
810
            } elseif (isset($this->domains['*']) && !empty($domain2)) {
811
                // 泛二级域名
812
                if ('www' != $domain2) {
813
                    $item      = $this->domains['*'];
814
                    $panDomain = $domain2;
815
                }
816
            }
817
818 3
            if (isset($panDomain)) {
819
                // 保存当前泛域名
820
                $this->request->setPanDomain($panDomain);
821
            }
822
        }
823
824 27
        if (false === $item) {
825
            // 检测全局域名规则
826 24
            $item = $this->domains['-'];
827
        }
828
829 27
        if (is_string($item)) {
830
            $item = $this->domains[$item];
831
        }
832
833 27
        return $item;
834
    }
835
836
    /**
837
     * URL生成 支持路由反射
838
     * @access public
839
     * @param string $url  路由地址
840
     * @param array  $vars 参数 ['a'=>'val1', 'b'=>'val2']
841
     * @return UrlBuild
842
     */
843
    public function buildUrl(string $url = '', array $vars = []): UrlBuild
844
    {
845
        return $this->app->make(UrlBuild::class, [$this, $this->app, $url, $vars], true);
846
    }
847
848
    /**
849
     * 设置全局的路由分组参数
850
     * @access public
851
     * @param string $method 方法名
852
     * @param array  $args   调用参数
853
     * @return RuleGroup
854
     */
855
    public function __call($method, $args)
856
    {
857
        return call_user_func_array([$this->group, $method], $args);
858
    }
859
}
860