Passed
Push — 5.2 ( e702c1...648d84 )
by liu
04:06 queued 39s
created

Route::auto()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
// +----------------------------------------------------------------------
1 ignored issue
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
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\cache\Driver;
17
use think\exception\HttpResponseException;
18
use think\exception\RouteNotFoundException;
19
use think\route\Dispatch;
20
use think\route\dispatch\Url as UrlDispatch;
21
use think\route\Domain;
22
use think\route\Resource;
23
use think\route\Rule;
24
use think\route\RuleGroup;
25
use think\route\RuleItem;
26
use think\route\RuleName;
27
28
class Route
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
29
{
30
    /**
31
     * REST定义
32
     * @var array
33
     */
34
    protected $rest = [
35
        'index'  => ['get', '', 'index'],
36
        'create' => ['get', '/create', 'create'],
37
        'edit'   => ['get', '/<id>/edit', 'edit'],
38
        'read'   => ['get', '/<id>', 'read'],
39
        'save'   => ['post', '', 'save'],
40
        'update' => ['put', '/<id>', 'update'],
41
        'delete' => ['delete', '/<id>', 'delete'],
42
    ];
43
44
    /**
45
     * 配置参数
46
     * @var array
47
     */
48
    protected $config = [
49
        // pathinfo分隔符
50
        'pathinfo_depr'         => '/',
51
        // 是否开启路由延迟解析
52
        'url_lazy_route'        => false,
53
        // 是否强制使用路由
54
        'url_route_must'        => false,
55
        // 合并路由规则
56
        'route_rule_merge'      => false,
57
        // 路由是否完全匹配
58
        'route_complete_match'  => false,
59
        // 使用注解路由
60
        'route_annotation'      => false,
61
        // 是否自动转换URL中的控制器和操作名
62
        'url_convert'           => true,
63
        // 默认的路由变量规则
64
        'default_route_pattern' => '[\w\.]+',
65
        // URL伪静态后缀
66
        'url_html_suffix'       => 'html',
67
        // 默认控制器名
68
        'default_controller'    => 'Index',
69
        // 默认操作名
70
        'default_action'        => 'index',
71
        // 操作方法后缀
72
        'action_suffix'         => '',
73
        // 是否开启路由检测缓存
74
        'route_check_cache'     => false,
75
    ];
76
77
    /**
78
     * 当前应用
79
     * @var App
80
     */
81
    protected $app;
82
83
    /**
84
     * 请求对象
85
     * @var Request
86
     */
87
    protected $request;
88
89
    /**
90
     * 缓存
91
     * @var Driver
92
     */
93
    protected $cache;
94
95
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
96
     * @var RuleName
97
     */
98
    protected $ruleName;
99
100
    /**
101
     * 当前HOST
102
     * @var string
103
     */
104
    protected $host;
105
106
    /**
107
     * 当前域名
108
     * @var string
109
     */
110
    protected $domain;
111
112
    /**
113
     * 当前分组对象
114
     * @var RuleGroup
115
     */
116
    protected $group;
117
118
    /**
119
     * 路由绑定
120
     * @var array
121
     */
122
    protected $bind = [];
123
124
    /**
125
     * 域名对象
126
     * @var array
127
     */
128
    protected $domains = [];
129
130
    /**
131
     * 跨域路由规则
132
     * @var RuleGroup
133
     */
134
    protected $cross;
135
136
    /**
137
     * 路由是否延迟解析
138
     * @var bool
139
     */
140
    protected $lazy = true;
141
142
    /**
143
     * 路由是否测试模式
144
     * @var bool
145
     */
146
    protected $isTest = false;
147
148
    /**
149
     * (分组)路由规则是否合并解析
150
     * @var bool
151
     */
152
    protected $mergeRuleRegex = true;
153
154
    public function __construct(App $app, Cache $cache)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
155
    {
156
        $this->app    = $app;
157
        $this->config = array_merge($this->config, $app->config->get('route'));
158
159
        if (!empty($this->config['route_cache_option'])) {
160
            $this->cache = $cache->connect($this->config['route_cache_option']);
161
        } else {
162
            $this->cache = $cache->init();
163
        }
164
165
        $this->ruleName = new RuleName();
166
    }
167
168
    public function config(string $name = null)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
169
    {
170
        if (is_null($name)) {
171
            return $this->config;
172
        }
173
174
        return $this->config[$name] ?? null;
175
    }
176
177
    /**
178
     * 设置路由域名及分组(包括资源路由)是否延迟解析
179
     * @access public
180
     * @param  bool $lazy 路由是否延迟解析
181
     * @return $this
182
     */
183
    public function lazy(bool $lazy = true)
184
    {
185
        $this->lazy = $lazy;
186
        return $this;
187
    }
188
189
    /**
190
     * 设置路由为测试模式
191
     * @access public
192
     * @param  bool $test 路由是否测试模式
193
     * @return void
194
     */
195
    public function setTestMode(bool $test): void
196
    {
197
        $this->isTest = $test;
198
    }
199
200
    /**
201
     * 检查路由是否为测试模式
202
     * @access public
203
     * @return bool
204
     */
205
    public function isTest(): bool
206
    {
207
        return $this->isTest;
208
    }
209
210
    /**
211
     * 设置路由域名及分组(包括资源路由)是否合并解析
212
     * @access public
213
     * @param  bool $merge 路由是否合并解析
214
     * @return $this
215
     */
216
    public function mergeRuleRegex(bool $merge = true)
217
    {
218
        $this->mergeRuleRegex = $merge;
219
        $this->group->mergeRuleRegex($merge);
220
221
        return $this;
222
    }
223
224
    /**
225
     * 初始化默认域名
226
     * @access protected
227
     * @return void
228
     */
229
    protected function setDefaultDomain(): void
230
    {
231
        // 默认域名
232
        $this->domain = $this->host;
233
234
        // 注册默认域名
235
        $domain = new Domain($this, $this->host);
236
237
        $this->domains[$this->host] = $domain;
238
239
        // 默认分组
240
        $this->group = $domain;
241
    }
242
243
    /**
244
     * 设置当前域名
245
     * @access public
246
     * @param  RuleGroup $group 域名
247
     * @return void
248
     */
249
    public function setGroup(RuleGroup $group): void
250
    {
251
        $this->group = $group;
252
    }
253
254
    /**
255
     * 获取当前分组
256
     * @access public
257
     * @return RuleGroup
258
     */
259
    public function getGroup(): RuleGroup
260
    {
261
        return $this->group;
262
    }
263
264
    /**
265
     * 注册变量规则
266
     * @access public
267
     * @param  array $pattern 变量规则
268
     * @return $this
269
     */
270
    public function pattern(array $pattern)
271
    {
272
        $this->group->pattern($pattern);
273
274
        return $this;
275
    }
276
277
    /**
278
     * 注册路由参数
279
     * @access public
280
     * @param  array $option 参数
281
     * @return $this
282
     */
283
    public function option(array $option)
284
    {
285
        $this->group->option($option);
286
287
        return $this;
288
    }
289
290
    /**
291
     * 注册域名路由
292
     * @access public
293
     * @param  string|array $name 子域名
294
     * @param  mixed        $rule 路由规则
295
     * @return Domain
296
     */
297
    public function domain($name, $rule = null): Domain
298
    {
299
        // 支持多个域名使用相同路由规则
300
        $domainName = is_array($name) ? array_shift($name) : $name;
301
302
        if ('*' != $domainName && false === strpos($domainName, '.')) {
303
            $domainName .= '.' . $this->request->rootDomain();
304
        }
305
306
        if (!isset($this->domains[$domainName])) {
307
            $domain = (new Domain($this, $domainName, $rule))
308
                ->lazy($this->lazy)
309
                ->mergeRuleRegex($this->mergeRuleRegex);
310
311
            $this->domains[$domainName] = $domain;
312
        } else {
313
            $domain = $this->domains[$domainName];
314
            $domain->parseGroupRule($rule);
315
        }
316
317
        if (is_array($name) && !empty($name)) {
318
            $root = $this->request->rootDomain();
319
            foreach ($name as $item) {
320
                if (false === strpos($item, '.')) {
321
                    $item .= '.' . $root;
322
                }
323
324
                $this->domains[$item] = $domainName;
325
            }
326
        }
327
328
        // 返回域名对象
329
        return $domain;
330
    }
331
332
    /**
333
     * 获取域名
334
     * @access public
335
     * @return array
336
     */
337
    public function getDomains(): array
338
    {
339
        return $this->domains;
340
    }
341
342
    /**
343
     * 获取域名
344
     * @access public
345
     * @return array
346
     */
347
    public function getRuleName(): RuleName
348
    {
349
        return $this->ruleName;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->ruleName returns the type think\route\RuleName which is incompatible with the documented return type array.
Loading history...
350
    }
351
352
    /**
353
     * 设置路由绑定
354
     * @access public
355
     * @param  string $bind   绑定信息
356
     * @param  string $domain 域名
357
     * @return $this
358
     */
359
    public function bind(string $bind, string $domain = null)
360
    {
361
        $domain = is_null($domain) ? $this->domain : $domain;
362
363
        $this->bind[$domain] = $bind;
364
365
        return $this;
366
    }
367
368
    /**
369
     * 读取路由绑定信息
370
     * @access public
371
     * @return array
372
     */
373
    public function getBind(): array
374
    {
375
        return $this->bind;
376
    }
377
378
    /**
379
     * 读取路由绑定
380
     * @access public
381
     * @param  string $domain 域名
382
     * @return string|null
383
     */
384
    public function getDomainBind(string $domain = null)
385
    {
386
        if (is_null($domain)) {
387
            $domain = $this->domain;
388
        } elseif (false === strpos($domain, '.')) {
389
            $domain .= '.' . $this->request->rootDomain();
390
        }
391
392
        $subDomain = $this->request->subDomain();
393
394
        if (strpos($subDomain, '.')) {
395
            $name = '*' . strstr($subDomain, '.');
396
        }
397
398
        if (isset($this->bind[$domain])) {
399
            $result = $this->bind[$domain];
400
        } elseif (isset($name) && isset($this->bind[$name])) {
401
            $result = $this->bind[$name];
402
        } elseif (!empty($subDomain) && isset($this->bind['*'])) {
403
            $result = $this->bind['*'];
404
        } else {
405
            $result = null;
406
        }
407
408
        return $result;
409
    }
410
411
    /**
412
     * 读取路由标识
413
     * @access public
414
     * @param  string $name   路由标识
415
     * @param  string $domain 域名
416
     * @param  string $method 请求类型
417
     * @return RuleItem[]
418
     */
419
    public function getName(string $name = null, string $domain = null, string $method = '*'): array
420
    {
421
        return $this->ruleName->getName($name, $domain, $method);
422
    }
423
424
    /**
425
     * 批量导入路由标识
426
     * @access public
427
     * @param  array $name 路由标识
428
     * @return $this
429
     */
430
    public function import(array $name)
431
    {
432
        $this->ruleName->import($name);
433
        return $this;
434
    }
435
436
    /**
437
     * 注册路由标识
438
     * @access public
439
     * @param  string       $name  路由标识
440
     * @param  string|array $value 路由规则
441
     * @param  bool         $first 是否置顶
442
     * @return Route
443
     */
444
    public function setName(string $name, $value, bool $first = false): Route
445
    {
446
        $this->ruleName->setName($name, $value, $first);
447
        return $this;
448
    }
449
450
    /**
451
     * 读取路由
452
     * @access public
453
     * @param  string $rule   路由规则
454
     * @param  string $domain 域名
455
     * @return RuleItem[]
456
     */
457
    public function getRule(string $rule, string $domain = null): array
458
    {
459
        if (is_null($domain)) {
460
            $domain = $this->domain;
461
        }
462
463
        return $this->ruleName->getRule($rule, $domain);
464
    }
465
466
    /**
467
     * 读取路由列表
468
     * @access public
469
     * @param  string $domain 域名
470
     * @return array
471
     */
472
    public function getRuleList(string $domain = null): array
473
    {
474
        return $this->ruleName->getRuleList($domain);
475
    }
476
477
    /**
478
     * 清空路由规则
479
     * @access public
480
     * @return void
481
     */
482
    public function clear(): void
483
    {
484
        $this->ruleName->clear();
485
        $this->group->clear();
486
    }
487
488
    /**
489
     * 注册路由规则
490
     * @access public
491
     * @param  string $rule   路由规则
492
     * @param  mixed  $route  路由地址
493
     * @param  string $method 请求类型
494
     * @return RuleItem
495
     */
496
    public function rule(string $rule, $route = null, string $method = '*'): RuleItem
497
    {
498
        return $this->group->addRule($rule, $route, $method);
499
    }
500
501
    /**
502
     * 设置跨域有效路由规则
503
     * @access public
504
     * @param  Rule   $rule   路由规则
505
     * @param  string $method 请求类型
506
     * @return $this
507
     */
508
    public function setCrossDomainRule(Rule $rule, string $method = '*')
509
    {
510
        if (!isset($this->cross)) {
511
            $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex);
512
        }
513
514
        $this->cross->addRuleItem($rule, $method);
515
516
        return $this;
517
    }
518
519
    /**
520
     * 注册路由分组
521
     * @access public
522
     * @param  string|\Closure $name  分组名称或者参数
523
     * @param  mixed           $route 分组路由
524
     * @return RuleGroup
525
     */
526
    public function group($name, $route = null): RuleGroup
527
    {
528
        if ($name instanceof \Closure) {
529
            $route = $name;
530
            $name  = '';
531
        }
532
533
        return (new RuleGroup($this, $this->group, $name, $route))
534
            ->lazy($this->lazy)
535
            ->mergeRuleRegex($this->mergeRuleRegex);
536
    }
537
538
    /**
539
     * 注册路由
540
     * @access public
541
     * @param  string $rule  路由规则
542
     * @param  mixed  $route 路由地址
543
     * @return RuleItem
544
     */
545
    public function any(string $rule, $route): RuleItem
546
    {
547
        return $this->rule($rule, $route, '*');
548
    }
549
550
    /**
551
     * 注册GET路由
552
     * @access public
553
     * @param  string $rule  路由规则
554
     * @param  mixed  $route 路由地址
555
     * @return RuleItem
556
     */
557
    public function get(string $rule, $route): RuleItem
558
    {
559
        return $this->rule($rule, $route, 'GET');
560
    }
561
562
    /**
563
     * 注册POST路由
564
     * @access public
565
     * @param  string $rule  路由规则
566
     * @param  mixed  $route 路由地址
567
     * @return RuleItem
568
     */
569
    public function post(string $rule, $route): RuleItem
570
    {
571
        return $this->rule($rule, $route, 'POST');
572
    }
573
574
    /**
575
     * 注册PUT路由
576
     * @access public
577
     * @param  string $rule  路由规则
578
     * @param  mixed  $route 路由地址
579
     * @return RuleItem
580
     */
581
    public function put(string $rule, $route): RuleItem
582
    {
583
        return $this->rule($rule, $route, 'PUT');
584
    }
585
586
    /**
587
     * 注册DELETE路由
588
     * @access public
589
     * @param  string $rule  路由规则
590
     * @param  mixed  $route 路由地址
591
     * @return RuleItem
592
     */
593
    public function delete(string $rule, $route): RuleItem
594
    {
595
        return $this->rule($rule, $route, 'DELETE');
596
    }
597
598
    /**
599
     * 注册PATCH路由
600
     * @access public
601
     * @param  string $rule  路由规则
602
     * @param  mixed  $route 路由地址
603
     * @return RuleItem
604
     */
605
    public function patch(string $rule, $route): RuleItem
606
    {
607
        return $this->rule($rule, $route, 'PATCH');
608
    }
609
610
    /**
611
     * 注册资源路由
612
     * @access public
613
     * @param  string $rule  路由规则
614
     * @param  string $route 路由地址
615
     * @return Resource
616
     */
617
    public function resource(string $rule, string $route): Resource
618
    {
619
        return (new Resource($this, $this->group, $rule, $route, $this->rest))
620
            ->lazy($this->lazy);
621
    }
622
623
    /**
624
     * 注册视图路由
625
     * @access public
626
     * @param  string|array $rule     路由规则
627
     * @param  string       $template 路由模板地址
628
     * @param  array        $vars     模板变量
629
     * @return RuleItem
630
     */
631
    public function view(string $rule, string $template = '', array $vars = []): RuleItem
632
    {
633
        return $this->rule($rule, $template, 'GET')->view($vars);
634
    }
635
636
    /**
637
     * 注册重定向路由
638
     * @access public
639
     * @param  string|array $rule   路由规则
640
     * @param  string       $route  路由地址
641
     * @param  int          $status 状态码
642
     * @return RuleItem
643
     */
644
    public function redirect(string $rule, string $route = '', int $status = 301): RuleItem
645
    {
646
        return $this->rule($rule, $route, '*')->redirect()->status($status);
647
    }
648
649
    /**
650
     * rest方法定义和修改
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
651
     * @access public
652
     * @param  string|array $name     方法名称
653
     * @param  array|bool   $resource 资源
654
     * @return $this
655
     */
656
    public function rest($name, $resource = [])
657
    {
658
        if (is_array($name)) {
659
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
660
        } else {
661
            $this->rest[$name] = $resource;
662
        }
663
664
        return $this;
665
    }
666
667
    /**
668
     * 获取rest方法定义的参数
669
     * @access public
670
     * @param  string $name 方法名称
671
     * @return array|null
672
     */
673
    public function getRest(string $name = null)
674
    {
675
        if (is_null($name)) {
676
            return $this->rest;
677
        }
678
679
        return $this->rest[$name] ?? null;
680
    }
681
682
    /**
683
     * 注册未匹配路由规则后的处理
684
     * @access public
685
     * @param  string|Closure $route  路由地址
686
     * @param  string         $method 请求类型
687
     * @return RuleItem
688
     */
689
    public function miss($route, string $method = '*'): RuleItem
690
    {
691
        return $this->group->miss($route, $method);
692
    }
693
694
    /**
695
     * 路由调度
696
     * @param  Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
697
     * @param  Closure $withRoute
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
698
     * @return Response
699
     */
700
    public function dispatch(Request $request, $withRoute = null)
701
    {
702
        $this->request = $request;
703
        $this->host    = $this->request->host(true);
704
705
        $this->setDefaultDomain();
706
707
        try {
708
            if ($withRoute) {
709
                $checkCallback = function () use ($request, $withRoute) {
0 ignored issues
show
Unused Code introduced by
The import $request is not used and could be removed.

This check looks for imports that have been defined, but are not used in the scope.

Loading history...
710
                    //加载路由
711
                    $withRoute();
712
                    return $this->check();
713
                };
714
715
                if ($this->config['route_check_cache']) {
716
                    $dispatch = $this->cache
717
                        ->tag('route_cache')
718
                        ->remember($this->getRouteCacheKey($request), $checkCallback);
719
                } else {
720
                    $dispatch = $checkCallback();
721
                }
722
            } else {
723
                $dispatch = $this->url($request->path());
724
            }
725
726
            $response = $dispatch->run();
727
        } catch (HttpResponseException $exception) {
728
            $response = $exception->getResponse();
729
        }
730
731
        return $response;
732
    }
733
734
    /**
735
     * 获取路由缓存Key
736
     * @access protected
737
     * @param  Request $request
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
738
     * @return string
739
     */
740
    protected function getRouteCacheKey(Request $request): string
741
    {
742
        if (!empty($this->config['route_check_cache_key'])) {
743
            $closure  = $this->config['route_check_cache_key'];
744
            $routeKey = $closure($request);
745
        } else {
746
            $routeKey = md5($request->baseUrl(true) . ':' . $request->method());
747
        }
748
749
        return $routeKey;
750
    }
751
752
    /**
753
     * 检测URL路由
754
     * @access public
755
     * @return Dispatch
756
     * @throws RouteNotFoundException
757
     */
758
    public function check(): Dispatch
759
    {
760
        // 自动检测域名路由
761
        $url = str_replace($this->config['pathinfo_depr'], '|', $this->request->path());
762
763
        $completeMatch = $this->config['route_complete_match'];
764
765
        $result = $this->checkDomain()->check($this->request, $url, $completeMatch);
766
767
        if (false === $result && !empty($this->cross)) {
768
            // 检测跨域路由
769
            $result = $this->cross->check($this->request, $url, $completeMatch);
770
        }
771
772
        if (false !== $result) {
773
            return $result;
774
        } elseif ($this->config['url_route_must']) {
775
            throw new RouteNotFoundException();
776
        }
777
778
        return $this->url($url);
779
    }
780
781
    /**
782
     * 默认URL解析
783
     * @access public
784
     * @param  string $url URL地址
785
     * @return Dispatch
786
     */
787
    public function url(string $url): UrlDispatch
788
    {
789
        return new UrlDispatch($this->request, $this->group, $url);
790
    }
791
792
    /**
793
     * 检测域名的路由规则
794
     * @access protected
795
     * @return Domain
796
     */
797
    protected function checkDomain(): Domain
798
    {
799
        // 获取当前子域名
800
        $subDomain = $this->request->subDomain();
801
802
        $item = false;
803
804
        if ($subDomain && count($this->domains) > 1) {
805
            $domain  = explode('.', $subDomain);
806
            $domain2 = array_pop($domain);
807
808
            if ($domain) {
809
                // 存在三级域名
810
                $domain3 = array_pop($domain);
811
            }
812
813
            if ($subDomain && isset($this->domains[$subDomain])) {
814
                // 子域名配置
815
                $item = $this->domains[$subDomain];
816
            } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
817
                // 泛三级域名
818
                $item      = $this->domains['*.' . $domain2];
819
                $panDomain = $domain3;
820
            } elseif (isset($this->domains['*']) && !empty($domain2)) {
821
                // 泛二级域名
822
                if ('www' != $domain2) {
823
                    $item      = $this->domains['*'];
824
                    $panDomain = $domain2;
825
                }
826
            }
827
828
            if (isset($panDomain)) {
829
                // 保存当前泛域名
830
                $this->request->setPanDomain($panDomain);
831
            }
832
        }
833
834
        if (false === $item) {
835
            // 检测当前完整域名
836
            $item = $this->domains[$this->host];
837
        }
838
839
        if (is_string($item)) {
840
            $item = $this->domains[$item];
841
        }
842
843
        return $item;
844
    }
845
846
    /**
847
     * 设置全局的路由分组参数
848
     * @access public
849
     * @param  string $method 方法名
850
     * @param  array  $args   调用参数
851
     * @return RuleGroup
852
     */
853
    public function __call($method, $args)
854
    {
855
        return call_user_func_array([$this->group, $method], $args);
856
    }
857
}
858