Passed
Pull Request — 5.1 (#1748)
by guanguans
09:27
created

Route   F

Complexity

Total Complexity 116

Size/Duplication

Total Lines 967
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 228
dl 0
loc 967
rs 2
c 0
b 0
f 0
wmc 116

51 Methods

Rating   Name   Duplication   Size   Complexity  
A getRuleList() 0 3 1
A isTest() 0 3 1
A setRequest() 0 3 1
A __make() 0 10 1
C checkDomain() 0 47 14
A option() 0 5 1
A getDomains() 0 3 1
A get() 0 3 1
C import() 0 49 15
A getMethodPrefix() 0 5 2
A group() 0 10 3
A setName() 0 4 1
A rest() 0 9 3
A bind() 0 7 2
A __construct() 0 9 2
A setDefaultDomain() 0 12 1
A getRest() 0 7 3
A getGroup() 0 3 1
A resource() 0 4 1
A alias() 0 7 1
A put() 0 3 1
A patch() 0 3 1
A rules() 0 3 1
A lazy() 0 4 1
A controller() 0 9 2
A getAlias() 0 7 3
B getBind() 0 27 10
A getRule() 0 7 2
A redirect() 0 3 1
A any() 0 3 1
A post() 0 3 1
A miss() 0 3 1
A delete() 0 3 1
A setGroup() 0 3 1
A clear() 0 4 1
A check() 0 26 5
B domain() 0 33 9
A setMethodPrefix() 0 9 2
A view() 0 3 1
A mergeRuleRegex() 0 6 1
A rule() 0 3 1
A getName() 0 3 1
A __call() 0 3 1
A auto() 0 3 1
A setConfig() 0 3 1
A autoSearchController() 0 4 1
A setCrossDomainRule() 0 9 2
A setTestMode() 0 3 1
A __debugInfo() 0 6 1
A config() 0 7 3
A pattern() 0 5 1

How to fix   Complexity   

Complex Class

Complex classes like Route often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use Route, and based on these observations, apply Extract Interface, too.

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~2018 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
12
namespace think;
13
14
use think\exception\RouteNotFoundException;
15
use think\route\AliasRule;
16
use think\route\dispatch\Url as UrlDispatch;
17
use think\route\Domain;
18
use think\route\Resource;
19
use think\route\RuleGroup;
20
use think\route\RuleItem;
21
22
class Route
1 ignored issue
show
Coding Style introduced by
Missing class doc comment
Loading history...
23
{
24
    /**
25
     * REST定义
26
     * @var array
27
     */
28
    protected $rest = [
29
        'index'  => ['get', '', 'index'],
30
        'create' => ['get', '/create', 'create'],
31
        'edit'   => ['get', '/<id>/edit', 'edit'],
32
        'read'   => ['get', '/<id>', 'read'],
33
        'save'   => ['post', '', 'save'],
34
        'update' => ['put', '/<id>', 'update'],
35
        'delete' => ['delete', '/<id>', 'delete'],
36
    ];
37
38
    /**
39
     * 请求方法前缀定义
40
     * @var array
41
     */
42
    protected $methodPrefix = [
43
        'get'    => 'get',
44
        'post'   => 'post',
45
        'put'    => 'put',
46
        'delete' => 'delete',
47
        'patch'  => 'patch',
48
    ];
49
50
    /**
51
     * 应用对象
52
     * @var App
53
     */
54
    protected $app;
55
56
    /**
57
     * 请求对象
58
     * @var Request
59
     */
60
    protected $request;
61
62
    /**
63
     * 当前HOST
64
     * @var string
65
     */
66
    protected $host;
67
68
    /**
69
     * 当前域名
70
     * @var string
71
     */
72
    protected $domain;
73
74
    /**
75
     * 当前分组对象
76
     * @var RuleGroup
77
     */
78
    protected $group;
79
80
    /**
81
     * 配置参数
82
     * @var array
83
     */
84
    protected $config = [];
85
86
    /**
87
     * 路由绑定
88
     * @var array
89
     */
90
    protected $bind = [];
91
92
    /**
93
     * 域名对象
94
     * @var array
95
     */
96
    protected $domains = [];
97
98
    /**
99
     * 跨域路由规则
100
     * @var RuleGroup
101
     */
102
    protected $cross;
103
104
    /**
105
     * 路由别名
106
     * @var array
107
     */
108
    protected $alias = [];
109
110
    /**
111
     * 路由是否延迟解析
112
     * @var bool
113
     */
114
    protected $lazy = true;
115
116
    /**
117
     * 路由是否测试模式
118
     * @var bool
119
     */
120
    protected $isTest;
121
122
    /**
123
     * (分组)路由规则是否合并解析
124
     * @var bool
125
     */
126
    protected $mergeRuleRegex = true;
127
128
    /**
129
     * 路由解析自动搜索多级控制器
130
     * @var bool
131
     */
132
    protected $autoSearchController = true;
133
134
    public function __construct(App $app, array $config = [])
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
135
    {
136
        $this->app     = $app;
137
        $this->request = $app['request'];
138
        $this->config  = $config;
139
140
        $this->host = $this->request->host(true) ?: $config['app_host'];
141
142
        $this->setDefaultDomain();
143
    }
144
145
    public function config($name = null)
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
146
    {
147
        if (is_null($name)) {
148
            return $this->config;
149
        }
150
151
        return isset($this->config[$name]) ? $this->config[$name] : null;
152
    }
153
154
    /**
155
     * 配置
156
     * @access public
157
     * @param  array $config
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
158
     * @return void
159
     */
160
    public function setConfig(array $config = [])
161
    {
162
        $this->config = array_merge($this->config, array_change_key_case($config));
163
    }
164
165
    public static function __make(App $app, Config $config)
1 ignored issue
show
Coding Style introduced by
Method name "Route::__make" is invalid; only PHP magic methods should be prefixed with a double underscore
Loading history...
Coding Style introduced by
Missing function doc comment
Loading history...
166
    {
167
        $config = $config->pull('app');
168
        $route  = new static($app, $config);
169
170
        $route->lazy($config['url_lazy_route'])
171
            ->autoSearchController($config['controller_auto_search'])
172
            ->mergeRuleRegex($config['route_rule_merge']);
173
174
        return $route;
175
    }
176
177
    /**
178
     * 设置路由的请求对象实例
179
     * @access public
180
     * @param  Request     $request   请求对象实例
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
181
     * @return void
182
     */
183
    public function setRequest($request)
184
    {
185
        $this->request = $request;
186
    }
187
188
    /**
189
     * 设置路由域名及分组(包括资源路由)是否延迟解析
190
     * @access public
191
     * @param  bool     $lazy   路由是否延迟解析
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
192
     * @return $this
193
     */
194
    public function lazy($lazy = true)
195
    {
196
        $this->lazy = $lazy;
197
        return $this;
198
    }
199
200
    /**
201
     * 设置路由为测试模式
202
     * @access public
203
     * @param  bool     $test   路由是否测试模式
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
204
     * @return void
205
     */
206
    public function setTestMode($test)
207
    {
208
        $this->isTest = $test;
209
    }
210
211
    /**
212
     * 检查路由是否为测试模式
213
     * @access public
214
     * @return bool
215
     */
216
    public function isTest()
217
    {
218
        return $this->isTest;
219
    }
220
221
    /**
222
     * 设置路由域名及分组(包括资源路由)是否合并解析
223
     * @access public
224
     * @param  bool     $merge   路由是否合并解析
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
225
     * @return $this
226
     */
227
    public function mergeRuleRegex($merge = true)
228
    {
229
        $this->mergeRuleRegex = $merge;
230
        $this->group->mergeRuleRegex($merge);
231
232
        return $this;
233
    }
234
235
    /**
236
     * 设置路由自动解析是否搜索多级控制器
237
     * @access public
238
     * @param  bool     $auto   是否自动搜索多级控制器
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
239
     * @return $this
240
     */
241
    public function autoSearchController($auto = true)
242
    {
243
        $this->autoSearchController = $auto;
244
        return $this;
245
    }
246
247
    /**
248
     * 初始化默认域名
249
     * @access protected
250
     * @return void
251
     */
252
    protected function setDefaultDomain()
253
    {
254
        // 默认域名
255
        $this->domain = $this->host;
256
257
        // 注册默认域名
258
        $domain = new Domain($this, $this->host);
259
260
        $this->domains[$this->host] = $domain;
261
262
        // 默认分组
263
        $this->group = $domain;
264
    }
265
266
    /**
267
     * 设置当前域名
268
     * @access public
269
     * @param  RuleGroup    $group 域名
270
     * @return void
271
     */
272
    public function setGroup(RuleGroup $group)
273
    {
274
        $this->group = $group;
275
    }
276
277
    /**
278
     * 获取当前分组
279
     * @access public
280
     * @return RuleGroup
281
     */
282
    public function getGroup()
283
    {
284
        return $this->group;
285
    }
286
287
    /**
288
     * 注册变量规则
289
     * @access public
290
     * @param  string|array  $name 变量名
291
     * @param  string        $rule 变量规则
292
     * @return $this
293
     */
294
    public function pattern($name, $rule = '')
295
    {
296
        $this->group->pattern($name, $rule);
297
298
        return $this;
299
    }
300
301
    /**
302
     * 注册路由参数
303
     * @access public
304
     * @param  string|array  $name  参数名
305
     * @param  mixed         $value 值
306
     * @return $this
307
     */
308
    public function option($name, $value = '')
309
    {
310
        $this->group->option($name, $value);
311
312
        return $this;
313
    }
314
315
    /**
316
     * 注册域名路由
317
     * @access public
318
     * @param  string|array  $name 子域名
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
319
     * @param  mixed         $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
320
     * @param  array         $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
321
     * @param  array         $pattern 变量规则
322
     * @return Domain
323
     */
324
    public function domain($name, $rule = '', $option = [], $pattern = [])
325
    {
326
        // 支持多个域名使用相同路由规则
327
        $domainName = is_array($name) ? array_shift($name) : $name;
328
329
        if ('*' != $domainName && false === strpos($domainName, '.')) {
330
            $domainName .= '.' . $this->request->rootDomain();
331
        }
332
333
        if (!isset($this->domains[$domainName])) {
334
            $domain = (new Domain($this, $domainName, $rule, $option, $pattern))
335
                ->lazy($this->lazy)
336
                ->mergeRuleRegex($this->mergeRuleRegex);
337
338
            $this->domains[$domainName] = $domain;
339
        } else {
340
            $domain = $this->domains[$domainName];
341
            $domain->parseGroupRule($rule);
342
        }
343
344
        if (is_array($name) && !empty($name)) {
345
            $root = $this->request->rootDomain();
346
            foreach ($name as $item) {
347
                if (false === strpos($item, '.')) {
348
                    $item .= '.' . $root;
349
                }
350
351
                $this->domains[$item] = $domainName;
352
            }
353
        }
354
355
        // 返回域名对象
356
        return $domain;
357
    }
358
359
    /**
360
     * 获取域名
361
     * @access public
362
     * @return array
363
     */
364
    public function getDomains()
365
    {
366
        return $this->domains;
367
    }
368
369
    /**
370
     * 设置路由绑定
371
     * @access public
372
     * @param  string     $bind 绑定信息
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
373
     * @param  string     $domain 域名
374
     * @return $this
375
     */
376
    public function bind($bind, $domain = null)
377
    {
378
        $domain = is_null($domain) ? $this->domain : $domain;
379
380
        $this->bind[$domain] = $bind;
381
382
        return $this;
383
    }
384
385
    /**
386
     * 读取路由绑定
387
     * @access public
388
     * @param  string    $domain 域名
389
     * @return string|null
390
     */
391
    public function getBind($domain = null)
392
    {
393
        if (is_null($domain)) {
394
            $domain = $this->domain;
395
        } elseif (true === $domain) {
0 ignored issues
show
introduced by
The condition true === $domain is always false.
Loading history...
396
            return $this->bind;
397
        } elseif (false === strpos($domain, '.')) {
398
            $domain .= '.' . $this->request->rootDomain();
399
        }
400
401
        $subDomain = $this->request->subDomain();
402
403
        if (strpos($subDomain, '.')) {
404
            $name = '*' . strstr($subDomain, '.');
405
        }
406
407
        if (isset($this->bind[$domain])) {
408
            $result = $this->bind[$domain];
409
        } elseif (isset($name) && isset($this->bind[$name])) {
410
            $result = $this->bind[$name];
411
        } elseif (!empty($subDomain) && isset($this->bind['*'])) {
412
            $result = $this->bind['*'];
413
        } else {
414
            $result = null;
415
        }
416
417
        return $result;
418
    }
419
420
    /**
0 ignored issues
show
Coding Style introduced by
Parameter $method should have a doc-comment as per coding-style.
Loading history...
421
     * 读取路由标识
422
     * @access public
423
     * @param  string    $name 路由标识
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
424
     * @param  string    $domain 域名
425
     * @return mixed
426
     */
427
    public function getName($name = null, $domain = null, $method = '*')
428
    {
429
        return $this->app['rule_name']->get($name, $domain, $method);
430
    }
431
432
    /**
433
     * 读取路由
434
     * @access public
435
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
436
     * @param  string    $domain 域名
437
     * @return array
438
     */
439
    public function getRule($rule, $domain = null)
440
    {
441
        if (is_null($domain)) {
442
            $domain = $this->domain;
443
        }
444
445
        return $this->app['rule_name']->getRule($rule, $domain);
446
    }
447
448
    /**
449
     * 读取路由
450
     * @access public
451
     * @param  string    $domain 域名
452
     * @return array
453
     */
454
    public function getRuleList($domain = null)
455
    {
456
        return $this->app['rule_name']->getRuleList($domain);
457
    }
458
459
    /**
460
     * 批量导入路由标识
461
     * @access public
462
     * @param  array    $name 路由标识
463
     * @return $this
464
     */
465
    public function setName($name)
466
    {
467
        $this->app['rule_name']->import($name);
468
        return $this;
469
    }
470
471
    /**
472
     * 导入配置文件的路由规则
473
     * @access public
474
     * @param  array     $rules 路由规则
475
     * @param  string    $type  请求类型
476
     * @return void
477
     */
478
    public function import(array $rules, $type = '*')
479
    {
480
        // 检查域名部署
481
        if (isset($rules['__domain__'])) {
482
            foreach ($rules['__domain__'] as $key => $rule) {
483
                $this->domain($key, $rule);
484
            }
485
            unset($rules['__domain__']);
486
        }
487
488
        // 检查变量规则
489
        if (isset($rules['__pattern__'])) {
490
            $this->pattern($rules['__pattern__']);
491
            unset($rules['__pattern__']);
492
        }
493
494
        // 检查路由别名
495
        if (isset($rules['__alias__'])) {
496
            foreach ($rules['__alias__'] as $key => $val) {
497
                $this->alias($key, $val);
498
            }
499
            unset($rules['__alias__']);
500
        }
501
502
        // 检查资源路由
503
        if (isset($rules['__rest__'])) {
504
            foreach ($rules['__rest__'] as $key => $rule) {
505
                $this->resource($key, $rule);
506
            }
507
            unset($rules['__rest__']);
508
        }
509
510
        // 检查路由规则(包含分组)
511
        foreach ($rules as $key => $val) {
512
            if (is_numeric($key)) {
513
                $key = array_shift($val);
514
            }
515
516
            if (empty($val)) {
517
                continue;
518
            }
519
520
            if (is_string($key) && 0 === strpos($key, '[')) {
521
                $key = substr($key, 1, -1);
522
                $this->group($key, $val);
523
            } elseif (is_array($val)) {
524
                $this->rule($key, $val[0], $type, $val[1], isset($val[2]) ? $val[2] : []);
525
            } else {
526
                $this->rule($key, $val, $type);
527
            }
528
        }
529
    }
530
531
    /**
532
     * 注册路由规则
533
     * @access public
534
     * @param  string    $rule       路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 7 found
Loading history...
535
     * @param  mixed     $route      路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
536
     * @param  string    $method     请求类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
537
     * @param  array     $option     路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
538
     * @param  array     $pattern    变量规则
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
539
     * @return RuleItem
540
     */
541
    public function rule($rule, $route, $method = '*', array $option = [], array $pattern = [])
542
    {
543
        return $this->group->addRule($rule, $route, $method, $option, $pattern);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->group->add...hod, $option, $pattern) returns the type think\route\RuleGroup which is incompatible with the documented return type think\route\RuleItem.
Loading history...
544
    }
545
546
    /**
547
     * 设置跨域有效路由规则
548
     * @access public
549
     * @param  Rule      $rule      路由规则
0 ignored issues
show
Bug introduced by
The type think\Rule was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
550
     * @param  string    $method    请求类型
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
551
     * @return $this
552
     */
553
    public function setCrossDomainRule($rule, $method = '*')
554
    {
555
        if (!isset($this->cross)) {
556
            $this->cross = (new RuleGroup($this))->mergeRuleRegex($this->mergeRuleRegex);
557
        }
558
559
        $this->cross->addRuleItem($rule, $method);
560
561
        return $this;
562
    }
563
564
    /**
565
     * 批量注册路由规则
566
     * @access public
567
     * @param  array     $rules      路由规则
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
568
     * @param  string    $method     请求类型
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
569
     * @param  array     $option     路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
570
     * @param  array     $pattern    变量规则
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
571
     * @return void
572
     */
573
    public function rules($rules, $method = '*', array $option = [], array $pattern = [])
574
    {
575
        $this->group->addRules($rules, $method, $option, $pattern);
576
    }
577
578
    /**
579
     * 注册路由分组
580
     * @access public
581
     * @param  string|array      $name       分组名称或者参数
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 7 found
Loading history...
582
     * @param  array|\Closure    $route      分组路由
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 6 found
Loading history...
583
     * @param  array             $option     路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 5 found
Loading history...
584
     * @param  array             $pattern    变量规则
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 4 found
Loading history...
585
     * @return RuleGroup
586
     */
587
    public function group($name, $route, array $option = [], array $pattern = [])
588
    {
589
        if (is_array($name)) {
590
            $option = $name;
591
            $name   = isset($option['name']) ? $option['name'] : '';
592
        }
593
594
        return (new RuleGroup($this, $this->group, $name, $route, $option, $pattern))
595
            ->lazy($this->lazy)
596
            ->mergeRuleRegex($this->mergeRuleRegex);
597
    }
598
599
    /**
600
     * 注册路由
601
     * @access public
602
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
603
     * @param  mixed     $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
604
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
605
     * @param  array     $pattern 变量规则
606
     * @return RuleItem
607
     */
608
    public function any($rule, $route = '', array $option = [], array $pattern = [])
609
    {
610
        return $this->rule($rule, $route, '*', $option, $pattern);
611
    }
612
613
    /**
614
     * 注册GET路由
615
     * @access public
616
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
617
     * @param  mixed     $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
618
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
619
     * @param  array     $pattern 变量规则
620
     * @return RuleItem
621
     */
622
    public function get($rule, $route = '', array $option = [], array $pattern = [])
623
    {
624
        return $this->rule($rule, $route, 'GET', $option, $pattern);
625
    }
626
627
    /**
628
     * 注册POST路由
629
     * @access public
630
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
631
     * @param  mixed     $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
632
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
633
     * @param  array     $pattern 变量规则
634
     * @return RuleItem
635
     */
636
    public function post($rule, $route = '', array $option = [], array $pattern = [])
637
    {
638
        return $this->rule($rule, $route, 'POST', $option, $pattern);
639
    }
640
641
    /**
642
     * 注册PUT路由
643
     * @access public
644
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
645
     * @param  mixed     $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
646
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
647
     * @param  array     $pattern 变量规则
648
     * @return RuleItem
649
     */
650
    public function put($rule, $route = '', array $option = [], array $pattern = [])
651
    {
652
        return $this->rule($rule, $route, 'PUT', $option, $pattern);
653
    }
654
655
    /**
656
     * 注册DELETE路由
657
     * @access public
658
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
659
     * @param  mixed     $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
660
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
661
     * @param  array     $pattern 变量规则
662
     * @return RuleItem
663
     */
664
    public function delete($rule, $route = '', array $option = [], array $pattern = [])
665
    {
666
        return $this->rule($rule, $route, 'DELETE', $option, $pattern);
667
    }
668
669
    /**
670
     * 注册PATCH路由
671
     * @access public
672
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
673
     * @param  mixed     $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
674
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
675
     * @param  array     $pattern 变量规则
676
     * @return RuleItem
677
     */
678
    public function patch($rule, $route = '', array $option = [], array $pattern = [])
679
    {
680
        return $this->rule($rule, $route, 'PATCH', $option, $pattern);
681
    }
682
683
    /**
684
     * 注册资源路由
685
     * @access public
686
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
687
     * @param  string    $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
688
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
689
     * @param  array     $pattern 变量规则
690
     * @return Resource
691
     */
692
    public function resource($rule, $route = '', array $option = [], array $pattern = [])
693
    {
694
        return (new Resource($this, $this->group, $rule, $route, $option, $pattern, $this->rest))
695
            ->lazy($this->lazy);
696
    }
697
698
    /**
699
     * 注册控制器路由 操作方法对应不同的请求前缀
700
     * @access public
701
     * @param  string    $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
702
     * @param  string    $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
703
     * @param  array     $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
704
     * @param  array     $pattern 变量规则
705
     * @return RuleGroup
706
     */
707
    public function controller($rule, $route = '', array $option = [], array $pattern = [])
708
    {
709
        $group = new RuleGroup($this, $this->group, $rule, null, $option, $pattern);
710
711
        foreach ($this->methodPrefix as $type => $val) {
712
            $group->addRule('<action>', $val . '<action>', $type);
713
        }
714
715
        return $group->prefix($route . '/');
716
    }
717
718
    /**
719
     * 注册视图路由
720
     * @access public
721
     * @param  string|array $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
722
     * @param  string       $template 路由模板地址
723
     * @param  array        $vars 模板变量
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
724
     * @param  array        $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
725
     * @param  array        $pattern 变量规则
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
726
     * @return RuleItem
727
     */
728
    public function view($rule, $template = '', array $vars = [], array $option = [], array $pattern = [])
729
    {
730
        return $this->rule($rule, $template, 'GET', $option, $pattern)->view($vars);
0 ignored issues
show
Bug introduced by
It seems like $rule can also be of type array; however, parameter $rule of think\Route::rule() does only seem to accept string, 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

730
        return $this->rule(/** @scrutinizer ignore-type */ $rule, $template, 'GET', $option, $pattern)->view($vars);
Loading history...
731
    }
732
733
    /**
734
     * 注册重定向路由
735
     * @access public
736
     * @param  string|array $rule 路由规则
0 ignored issues
show
Coding Style introduced by
Expected 4 spaces after parameter name; 1 found
Loading history...
737
     * @param  string       $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
738
     * @param  array        $status 状态码
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
739
     * @param  array        $option 路由参数
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
740
     * @param  array        $pattern 变量规则
741
     * @return RuleItem
742
     */
743
    public function redirect($rule, $route = '', $status = 301, array $option = [], array $pattern = [])
744
    {
745
        return $this->rule($rule, $route, '*', $option, $pattern)->redirect()->status($status);
0 ignored issues
show
Bug introduced by
It seems like $rule can also be of type array; however, parameter $rule of think\Route::rule() does only seem to accept string, 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

745
        return $this->rule(/** @scrutinizer ignore-type */ $rule, $route, '*', $option, $pattern)->redirect()->status($status);
Loading history...
Bug introduced by
The method status() does not exist on think\route\RuleItem. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

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

745
        return $this->rule($rule, $route, '*', $option, $pattern)->redirect()->/** @scrutinizer ignore-call */ status($status);
Loading history...
746
    }
747
748
    /**
749
     * 注册别名路由
750
     * @access public
751
     * @param  string  $rule 路由别名
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 1 found
Loading history...
752
     * @param  string  $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
753
     * @param  array   $option 路由参数
754
     * @return AliasRule
755
     */
756
    public function alias($rule, $route, array $option = [])
757
    {
758
        $aliasRule = new AliasRule($this, $this->group, $rule, $route, $option);
759
760
        $this->alias[$rule] = $aliasRule;
761
762
        return $aliasRule;
763
    }
764
765
    /**
766
     * 获取别名路由定义
767
     * @access public
768
     * @param  string    $name 路由别名
769
     * @return string|array|null
770
     */
771
    public function getAlias($name = null)
772
    {
773
        if (is_null($name)) {
774
            return $this->alias;
775
        }
776
777
        return isset($this->alias[$name]) ? $this->alias[$name] : null;
778
    }
779
780
    /**
781
     * 设置不同请求类型下面的方法前缀
782
     * @access public
783
     * @param  string|array  $method 请求类型
784
     * @param  string        $prefix 类型前缀
785
     * @return $this
786
     */
787
    public function setMethodPrefix($method, $prefix = '')
788
    {
789
        if (is_array($method)) {
790
            $this->methodPrefix = array_merge($this->methodPrefix, array_change_key_case($method));
791
        } else {
792
            $this->methodPrefix[strtolower($method)] = $prefix;
793
        }
794
795
        return $this;
796
    }
797
798
    /**
799
     * 获取请求类型的方法前缀
800
     * @access public
801
     * @param  string    $method 请求类型
802
     * @param  string    $prefix 类型前缀
0 ignored issues
show
Coding Style introduced by
Superfluous parameter comment
Loading history...
803
     * @return string|null
804
     */
805
    public function getMethodPrefix($method)
806
    {
807
        $method = strtolower($method);
808
809
        return isset($this->methodPrefix[$method]) ? $this->methodPrefix[$method] : null;
810
    }
811
812
    /**
813
     * rest方法定义和修改
0 ignored issues
show
Coding Style introduced by
Doc comment short description must start with a capital letter
Loading history...
814
     * @access public
815
     * @param  string        $name 方法名称
0 ignored issues
show
Coding Style introduced by
Expected 5 spaces after parameter name; 1 found
Loading history...
816
     * @param  array|bool    $resource 资源
817
     * @return $this
818
     */
819
    public function rest($name, $resource = [])
820
    {
821
        if (is_array($name)) {
0 ignored issues
show
introduced by
The condition is_array($name) is always false.
Loading history...
822
            $this->rest = $resource ? $name : array_merge($this->rest, $name);
823
        } else {
824
            $this->rest[$name] = $resource;
825
        }
826
827
        return $this;
828
    }
829
830
    /**
831
     * 获取rest方法定义的参数
832
     * @access public
833
     * @param  string        $name 方法名称
834
     * @return array|null
835
     */
836
    public function getRest($name = null)
837
    {
838
        if (is_null($name)) {
839
            return $this->rest;
840
        }
841
842
        return isset($this->rest[$name]) ? $this->rest[$name] : null;
843
    }
844
845
    /**
846
     * 注册未匹配路由规则后的处理
847
     * @access public
848
     * @param  string    $route 路由地址
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
849
     * @param  string    $method 请求类型
850
     * @param  array     $option 路由参数
851
     * @return RuleItem
852
     */
853
    public function miss($route, $method = '*', array $option = [])
854
    {
855
        return $this->group->addMissRule($route, $method, $option);
856
    }
857
858
    /**
859
     * 注册一个自动解析的URL路由
860
     * @access public
861
     * @param  string    $route 路由地址
862
     * @return RuleItem
863
     */
864
    public function auto($route)
865
    {
866
        return $this->group->addAutoRule($route);
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->group->addAutoRule($route) targeting think\route\RuleGroup::addAutoRule() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Bug Best Practice introduced by
The expression return $this->group->addAutoRule($route) returns the type void which is incompatible with the documented return type think\route\RuleItem.
Loading history...
867
    }
868
869
    /**
870
     * 检测URL路由
871
     * @access public
872
     * @param  string    $url URL地址
0 ignored issues
show
Coding Style introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
873
     * @param  bool      $must 是否强制路由
874
     * @return Dispatch
0 ignored issues
show
Bug introduced by
The type think\Dispatch was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
875
     * @throws RouteNotFoundException
876
     */
877
    public function check($url, $must = false)
878
    {
879
        // 自动检测域名路由
880
        $domain = $this->checkDomain();
881
        $url    = str_replace($this->config['pathinfo_depr'], '|', $url);
882
883
        $completeMatch = $this->config['route_complete_match'];
884
885
        $result = $domain->check($this->request, $url, $completeMatch);
886
887
        if (false === $result && !empty($this->cross)) {
888
            // 检测跨域路由
889
            $result = $this->cross->check($this->request, $url, $completeMatch);
890
        }
891
892
        if (false !== $result) {
893
            // 路由匹配
894
            return $result;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $result returns the type think\route\Dispatch which is incompatible with the documented return type think\Dispatch.
Loading history...
895
        } elseif ($must) {
896
            // 强制路由不匹配则抛出异常
897
            throw new RouteNotFoundException();
898
        }
899
900
        // 默认路由解析
901
        return new UrlDispatch($this->request, $this->group, $url, [
0 ignored issues
show
Bug Best Practice introduced by
The expression return new think\route\d...>autoSearchController)) returns the type think\route\dispatch\Url which is incompatible with the documented return type think\Dispatch.
Loading history...
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
902
            'auto_search' => $this->autoSearchController,
903
        ]);
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...
904
    }
905
906
    /**
907
     * 检测域名的路由规则
908
     * @access protected
909
     * @return Domain
910
     */
911
    protected function checkDomain()
912
    {
913
        // 获取当前子域名
914
        $subDomain = $this->request->subDomain();
915
916
        $item = false;
917
918
        if ($subDomain && count($this->domains) > 1) {
919
            $domain  = explode('.', $subDomain);
920
            $domain2 = array_pop($domain);
921
922
            if ($domain) {
923
                // 存在三级域名
924
                $domain3 = array_pop($domain);
925
            }
926
927
            if ($subDomain && isset($this->domains[$subDomain])) {
928
                // 子域名配置
929
                $item = $this->domains[$subDomain];
930
            } elseif (isset($this->domains['*.' . $domain2]) && !empty($domain3)) {
931
                // 泛三级域名
932
                $item      = $this->domains['*.' . $domain2];
933
                $panDomain = $domain3;
934
            } elseif (isset($this->domains['*']) && !empty($domain2)) {
935
                // 泛二级域名
936
                if ('www' != $domain2) {
937
                    $item      = $this->domains['*'];
938
                    $panDomain = $domain2;
939
                }
940
            }
941
942
            if (isset($panDomain)) {
943
                // 保存当前泛域名
944
                $this->request->setPanDomain($panDomain);
945
            }
946
        }
947
948
        if (false === $item) {
949
            // 检测当前完整域名
950
            $item = $this->domains[$this->host];
951
        }
952
953
        if (is_string($item)) {
954
            $item = $this->domains[$item];
955
        }
956
957
        return $item;
958
    }
959
960
    /**
961
     * 清空路由规则
962
     * @access public
963
     * @return void
964
     */
965
    public function clear()
966
    {
967
        $this->app['rule_name']->clear();
968
        $this->group->clear();
969
    }
970
971
    /**
972
     * 设置全局的路由分组参数
973
     * @access public
974
     * @param  string    $method     方法名
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 5 found
Loading history...
975
     * @param  array     $args       调用参数
0 ignored issues
show
Coding Style introduced by
Expected 3 spaces after parameter name; 7 found
Loading history...
976
     * @return RuleGroup
977
     */
978
    public function __call($method, $args)
979
    {
980
        return call_user_func_array([$this->group, $method], $args);
981
    }
982
983
    public function __debugInfo()
0 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
984
    {
985
        $data = get_object_vars($this);
986
        unset($data['app'], $data['request']);
987
988
        return $data;
989
    }
990
}
991