Passed
Push — 8.0 ( cbd9ac...f31d17 )
by liu
02:30
created

RuleGroup::getMissRule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.0416

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 8
ccs 5
cts 6
cp 0.8333
crap 3.0416
rs 10
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\route;
14
15
use Closure;
16
use think\Container;
17
use think\Exception;
18
use think\helper\Str;
19
use think\Request;
20
use think\Route;
21
use think\route\dispatch\Callback as CallbackDispatch;
22
use think\route\dispatch\Controller as ControllerDispatch;
23
24
/**
25
 * 路由分组类
26
 */
27
class RuleGroup extends Rule
28
{
29
    /**
30
     * 分组路由(包括子分组)
31
     * @var Rule[]
32
     */
33
    protected $rules = [];
34
35
    /**
36
     * MISS路由
37
     * @var RuleItem
38
     */
39
    protected $miss;
40
41
    /**
42
     * 完整名称
43
     * @var string
44
     */
45
    protected $fullName;
46
47
    /**
48
     * 分组别名
49
     * @var string
50
     */
51
    protected $alias;
52
53
    /**
54
     * 分组绑定
55
     * @var string
56
     */
57
    protected $bind;
58
59
    /**
60
     * 是否已经解析
61
     * @var bool
62
     */
63
    protected $hasParsed;
64
65
    /**
66
     * 架构函数
67
     * @access public
68
     * @param  Route     $router 路由对象
69
     * @param  RuleGroup $parent 上级对象
70
     * @param  string    $name   分组名称
71
     * @param  mixed     $rule   分组路由
72
     * @param  bool      $lazy   延迟解析
73
     */
74 3
    public function __construct(Route $router, ?RuleGroup $parent = null, string $name = '', $rule = null, bool $lazy = false)
75
    {
76 3
        $this->router = $router;
77 3
        $this->parent = $parent;
78 3
        $this->rule   = $rule;
79 3
        $this->name   = trim($name, '/');
80
81 3
        $this->setFullName();
82
83 3
        if ($this->parent) {
84 3
            $this->domain = $this->parent->getDomain();
85 3
            $this->parent->addRuleItem($this);
86
        }
87
88 3
        if (!$lazy) {
89 3
            $this->parseGroupRule($rule);
90
        }
91
    }
92
93
    /**
94
     * 设置分组的路由规则
95
     * @access public
96
     * @return void
97
     */
98 3
    protected function setFullName(): void
99
    {
100 3
        if (str_contains($this->name, ':')) {
101
            $this->name = preg_replace(['/\[\:(\w+)\]/', '/\:(\w+)/'], ['<\1?>', '<\1>'], $this->name);
102
        }
103
104 3
        if ($this->parent && $this->parent->getFullName()) {
105
            $this->fullName = $this->parent->getFullName() . ($this->name ? '/' . $this->name : '');
106
        } else {
107 3
            $this->fullName = $this->name;
108
        }
109
110 3
        if ($this->name) {
111
            $this->router->getRuleName()->setGroup($this->name, $this);
112
        }
113
    }
114
115
    /**
116
     * 获取所属域名
117
     * @access public
118
     * @return string
119
     */
120 12
    public function getDomain(): string
121
    {
122 12
        return $this->domain ?: '-';
123
    }
124
125
    /**
126
     * 获取分组别名
127
     * @access public
128
     * @return string
129
     */
130
    public function getAlias(): string
131
    {
132
        return $this->alias ?: '';
133
    }
134
135
    /**
136
     * 检测分组路由
137
     * @access public
138
     * @param  Request $request       请求对象
139
     * @param  string  $url           访问地址
140
     * @param  bool    $completeMatch 路由是否完全匹配
141
     * @return Dispatch|false
142
     */
143 27
    public function check(Request $request, string $url, bool $completeMatch = false)
144
    {
145
        // 检查分组有效性
146 27
        if (!$this->checkOption($this->option, $request) || !$this->checkUrl($url)) {
147
            return false;
148
        }
149
150
        // 解析分组路由
151 27
        if (!$this->hasParsed) {
152 24
            $this->parseGroupRule($this->rule);
153
        }
154
155
        // 获取当前路由规则
156 27
        $method = strtolower($request->method());
157 27
        $rules  = $this->getRules($method);
158 27
        $option = $this->getOption();
159
160 27
        if (isset($option['complete_match'])) {
161
            $completeMatch = $option['complete_match'];
162
        }
163
164 27
        if (!empty($option['merge_rule_regex'])) {
165
            // 路由合并检查
166
            $result = $this->checkMergeRuleRegex($request, $rules, $url, $completeMatch);
167
168
            if (false !== $result) {
169
                return $result;
170
            }
171
        } else {
172
            // 检查分组路由
173 27
            foreach ($rules as $item) {
174 24
                $result = $item->check($request, $url, $completeMatch);
175
176 24
                if (false !== $result) {
177 24
                    return $result;
178
                }
179
            }
180
        }
181
182 6
        if ($this->bind) {
183
            // 检查分组绑定
184
            return $this->checkBind($request, $url, $option);
185
        }
186
187 6
        if ($miss = $this->getMissRule($method)) {
188
            // MISS路由
189 3
            return $miss->parseRule($request, '', $miss->getRoute(), $url, $miss->getOption());
190
        }
191
192 3
        return false;
193
    }
194
195
    /**
196
     * 分组URL匹配检查
197
     * @access protected
198
     * @param  string $url URL
199
     * @return bool
200
     */
201 27
    protected function checkUrl(string $url): bool
202
    {
203 27
        if ($this->fullName) {
204
            $pos = strpos($this->fullName, '<');
205
206
            if (false !== $pos) {
207
                $str = substr($this->fullName, 0, $pos);
208
            } else {
209
                $str = $this->fullName;
210
            }
211
212
            if ($str && 0 !== stripos(str_replace('|', '/', $url), $str)) {
213
                return false;
214
            }
215
        }
216
217 27
        return true;
218
    }
219
220
    /**
221
     * 设置路由分组别名
222
     * @access public
223
     * @param  string $alias 路由分组别名
224
     * @return $this
225
     */
226
    public function alias(string $alias)
227
    {
228
        $this->alias = $alias;
229
        $this->router->getRuleName()->setGroup($alias, $this);
230
231
        return $this;
232
    }
233
234
    /**
235
     * 解析分组和域名的路由规则及绑定
236
     * @access public
237
     * @param  mixed $rule 路由规则
238
     * @return void
239
     */
240 27
    public function parseGroupRule($rule): void
241
    {
242 27
        if (is_string($rule) && is_subclass_of($rule, Dispatch::class)) {
243
            $this->dispatcher($rule);
244
            return;
245
        }
246
247 27
        $origin = $this->router->getGroup();
248 27
        $this->router->setGroup($this);
249
250 27
        if ($rule instanceof Closure) {
251 6
            Container::getInstance()->invokeFunction($rule);
252 24
        } elseif (is_string($rule) && $rule) {
253
            $this->bind($rule);
254
        }
255
256 27
        $this->router->setGroup($origin);
257 27
        $this->hasParsed = true;
258
    }
259
260
    /**
261
     * 检测分组路由
262
     * @access public
263
     * @param  Request $request       请求对象
264
     * @param  array   $rules         路由规则
265
     * @param  string  $url           访问地址
266
     * @param  bool    $completeMatch 路由是否完全匹配
267
     * @return Dispatch|false
268
     */
269
    protected function checkMergeRuleRegex(Request $request, array &$rules, string $url, bool $completeMatch)
270
    {
271
        $depr  = $this->config('pathinfo_depr');
272
        $url   = $depr . str_replace('|', $depr, $url);
273
        $regex = [];
274
        $items = [];
275
276
        foreach ($rules as $key => $item) {
277
            if ($item instanceof RuleItem) {
278
                $rule = $depr . str_replace('/', $depr, $item->getRule());
279
                if ($depr == $rule && $depr != $url) {
280
                    unset($rules[$key]);
281
                    continue;
282
                }
283
284
                $complete = $item->getOption('complete_match', $completeMatch);
285
286
                if (!str_contains($rule, '<')) {
287
                    if (0 === strcasecmp($rule, $url) || (!$complete && 0 === strncasecmp($rule, $url, strlen($rule)))) {
288
                        return $item->checkRule($request, $url, []);
289
                    }
290
291
                    unset($rules[$key]);
292
                    continue;
293
                }
294
295
                $slash = preg_quote('/-' . $depr, '/');
296
297
                if ($matchRule = preg_split('/[' . $slash . ']<\w+\??>/', $rule, 2)) {
298
                    if ($matchRule[0] && 0 !== strncasecmp($rule, $url, strlen($matchRule[0]))) {
299
                        unset($rules[$key]);
300
                        continue;
301
                    }
302
                }
303
304
                if (preg_match_all('/[' . $slash . ']?<?\w+\??>?/', $rule, $matches)) {
305
                    unset($rules[$key]);
306
                    $pattern = array_merge($this->getPattern(), $item->getPattern());
307
                    $option  = array_merge($this->getOption(), $item->getOption());
308
309
                    $regex[$key] = $this->buildRuleRegex($rule, $matches[0], $pattern, $option, $complete, '_THINK_' . $key);
310
                    $items[$key] = $item;
311
                }
312
            }
313
        }
314
315
        if (empty($regex)) {
316
            return false;
317
        }
318
319
        try {
320
            $result = preg_match('~^(?:' . implode('|', $regex) . ')~u', $url, $match);
321
        } catch (\Exception $e) {
322
            throw new Exception('route pattern error');
323
        }
324
325
        if ($result) {
326
            $var = [];
327
            foreach ($match as $key => $val) {
328
                if (is_string($key) && '' !== $val) {
329
                    [$name, $pos] = explode('_THINK_', $key);
330
331
                    $var[$name] = $val;
332
                }
333
            }
334
335
            if (!isset($pos)) {
336
                foreach ($regex as $key => $item) {
337
                    if (str_starts_with(str_replace(['\/', '\-', '\\' . $depr], ['/', '-', $depr], $item), $match[0])) {
338
                        $pos = $key;
339
                        break;
340
                    }
341
                }
342
            }
343
344
            $rule  = $items[$pos]->getRule();
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $pos does not seem to be defined for all execution paths leading up to this point.
Loading history...
345
            $array = $this->router->getRule($rule);
346
347
            foreach ($array as $item) {
348
                if (in_array($item->getMethod(), ['*', strtolower($request->method())])) {
349
                    $result = $item->checkRule($request, $url, $var);
350
351
                    if (false !== $result) {
352
                        return $result;
353
                    }
354
                }
355
            }
356
        }
357
358
        return false;
359
    }
360
361
    /**
362
     * 注册MISS路由
363
     * @access public
364
     * @param  string|Closure $route  路由地址
365
     * @param  string         $method 请求类型
366
     * @return RuleItem
367
     */
368 27
    public function miss(string | Closure $route, string $method = '*'): RuleItem
369
    {
370
        // 创建路由规则实例
371 27
        $method   = strtolower($method);
372 27
        $ruleItem = new RuleItem($this->router, $this, null, '', $route, $method);
373
374 27
        $this->miss[$method] = $ruleItem->setMiss();
375
376 27
        return $ruleItem;
377
    }
378
379
    /**
380
     * 获取分组下的MISS路由
381
     * @access public
382
     * @param  string $method 请求类型
383
     * @return RuleItem|null
384
     */
385 6
    public function getMissRule(string $method = '*'): ?RuleItem
386
    {
387 6
        if (isset($this->miss[$method])) {
388 3
            $miss = $this->miss[$method];
389 3
        } elseif (isset($this->miss['*'])) {
390
            $miss = $this->miss['*'];
391
        }
392 6
        return $miss ?? null;
393
    }
394
395
    /**
396
     * 分组自动URL调度
397
     * @access public
398
     * @param  string $bind 绑定资源
399
     * @return $this
400
     */
401 3
    public function auto(string $bind = '')
402
    {
403 3
        $this->bind = $bind ?: '/' . $this->getFullName();
404 3
        return $this;
405
    }
406
407
    /**
408
     * 分组绑定 默认绑定到当前分组名所在的控制器分级
409
     * 绑定规则 class @controller :namespace /layer
410
     * @access public
411
     * @param  string $bind 绑定资源
412
     * @return $this
413
     */
414
    public function bind(string $bind)
415
    {
416
        $this->bind = $bind;
417
        return $this;
418
    }
419
420
    /**
421
     * 分组绑定到类
422
     * @access public
423
     * @param  string $class
424
     * @return $this
425
     */
426
    public function class (string $class)
427
    {
428
        $this->bind = '\\' . $class;
429
        return $this;
430
    }
431
432
    /**
433
     * 分组绑定到控制器
434
     * @access public
435
     * @param  string $controller
436
     * @return $this
437
     */
438
    public function controller(string $controller)
439
    {
440
        $this->bind = '@' . $controller;
441
        return $this;
442
    }
443
444
    /**
445
     * 分组绑定到命名空间
446
     * @access public
447
     * @param  string $namespace
448
     * @return $this
449
     */
450
    public function namespace(string $namespace)
451
    {
452
        $this->bind = ':' . $namespace;
453
        return $this;
454
    }
455
456
    /**
457
     * 分组绑定到控制器分级
458
     * @access public
459
     * @param  string $namespace
460
     * @return $this
461
     */
462
    public function layer(string $layer)
463
    {
464
        $this->bind = '/' . $layer;
465
        return $this;
466
    }
467
468
    /**
469
     * 检测URL绑定
470
     * @access private
471
     * @param  Request   $request
472
     * @param  string    $url URL地址
473
     * @param  array     $option 分组参数
474
     * @return Dispatch
475
     */
476 3
    public function checkBind(Request $request, string $url, array $option = []): Dispatch
477
    {
478 3
        [$bind, $param] = $this->parseBindAppendParam($this->bind);
479
480 3
        [$call, $bind]  = match (substr($bind, 0, 1)) {
481 3
            '\\'    => ['bindToClass', substr($bind, 1)],
482 3
            '@'     => ['bindToController', substr($bind, 1)],
483 3
            '/'     => ['bindToLayer', substr($bind, 1)],
484 3
            ':'     => ['bindToNamespace', substr($bind, 1)],
485 3
            default => ['bindToClass', $bind],
486 3
        };
487
488 3
        $name = $this->getFullName();
489 3
        $url  = trim(substr(str_replace('|', '/', $url), strlen($name)), '/');
490
491 3
        return $this->$call($request, $url, $bind, $param, $option);
492
    }
493
494 3
    protected function parseBindAppendParam(string $bind)
495
    {
496 3
        $vars = [];
497 3
        if (str_contains($bind, '?')) {
498
            [$bind, $query] = explode('?', $bind);
499
            parse_str($query, $vars);
500
        }
501 3
        return [$bind, $vars];
502
    }
503
504
    /**
505
     * 绑定到类
506
     * @access protected
507
     * @param  Request   $request
508
     * @param  string    $url URL地址
509
     * @param  string    $class 类名(带命名空间)
510
     * @param  array     $param  路由变量
511
     * @param  array     $option 分组参数
512
     * @return CallbackDispatch
513
     */
514
    protected function bindToClass(Request $request, string $url, string $class, array $param = [], array $option = []): CallbackDispatch
515
    {
516
        $array  = explode('/', $url, 2);
517
        $action = !empty($array[0]) ? $array[0] : $this->config('default_action');
518
519
        if (!empty($array[1])) {
520
            $this->parseUrlParams($array[1], $param);
521
        }
522
523
        return new CallbackDispatch($request, $this, [$class, $action], $param, $option);
524
    }
525
526
    /**
527
     * 绑定到命名空间
528
     * @access protected
529
     * @param  Request   $request
530
     * @param  string    $url URL地址
531
     * @param  string    $namespace 命名空间
532
     * @param  array     $param  路由变量
533
     * @param  array     $option 分组参数
534
     * @return CallbackDispatch
535
     */
536
    protected function bindToNamespace(Request $request, string $url, string $namespace, array $param = [], array $option = []): CallbackDispatch
537
    {
538
        $array  = explode('/', $url, 3);
539
        $class  = !empty($array[0]) ? $array[0] : $this->config('default_controller');
540
        $method = !empty($array[1]) ? $array[1] : $this->config('default_action');
541
542
        if (!empty($array[2])) {
543
            $this->parseUrlParams($array[2], $param);
544
        }
545
546
        return new CallbackDispatch($request, $this, [trim($namespace, '\\') . '\\' . Str::studly($class), $method], $param, $option);
0 ignored issues
show
Bug introduced by
It seems like $class can also be of type null; however, parameter $value of think\helper\Str::studly() 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

546
        return new CallbackDispatch($request, $this, [trim($namespace, '\\') . '\\' . Str::studly(/** @scrutinizer ignore-type */ $class), $method], $param, $option);
Loading history...
547
    }
548
549
    /**
550
     * 绑定到控制器
551
     * @access protected
552
     * @param  Request   $request
553
     * @param  string    $url URL地址
554
     * @param  string    $controller 控制器名
555
     * @param  array     $param  路由变量
556
     * @param  array     $option 分组参数
557
     * @return ControllerDispatch
558
     */
559
    protected function bindToController(Request $request, string $url, string $controller, array $param = [], array $option = []): ControllerDispatch
560
    {
561
        $array  = explode('/', $url, 2);
562
        $action = !empty($array[0]) ? $array[0] : $this->config('default_action');
563
564
        if (!empty($array[1])) {
565
            $this->parseUrlParams($array[1], $param);
566
        }
567
568
        return new ControllerDispatch($request, $this, [$controller, $action], $param, $option);
569
    }
570
571
    /**
572
     * 绑定到控制器分级
573
     * @access protected
574
     * @param  Request   $request
575
     * @param  string    $url URL地址
576
     * @param  string    $controller 控制器名
577
     * @param  array     $param  路由变量
578
     * @param  array     $option 分组参数
579
     * @return ControllerDispatch
580
     */
581 3
    protected function bindToLayer(Request $request, string $url, string $layer, array $param = [], array $option = []): ControllerDispatch
582
    {
583 3
        $array      = explode('/', $url, 3);
584 3
        $controller = !empty($array[0]) ? $array[0] : $this->config('default_controller');
585 3
        $action     = !empty($array[1]) ? $array[1] : $this->config('default_action');
586
587 3
        if (!empty($array[2])) {
588
            $this->parseUrlParams($array[2], $param);
589
        }
590
591 3
        return new ControllerDispatch($request, $this, [$layer, $controller, $action], $param, $option);
592
    }
593
594
    /**
595
     * 添加分组下的路由规则
596
     * @access public
597
     * @param  string $rule   路由规则
598
     * @param  mixed  $route  路由地址
599
     * @param  string $method 请求类型
600
     * @return RuleItem
601
     */
602 24
    public function addRule(string $rule, $route = null, string $method = '*'): RuleItem
603
    {
604
        // 读取路由标识
605 24
        if (is_string($route)) {
606 9
            $name = $route;
607
        } else {
608 15
            $name = null;
609
        }
610
611 24
        $method = strtolower($method);
612
613 24
        if ('' === $rule || '/' === $rule) {
614 3
            $rule .= '$';
615
        }
616
617
        // 创建路由规则实例
618 24
        $ruleItem = new RuleItem($this->router, $this, $name, $rule, $route, $method);
619
620 24
        $this->addRuleItem($ruleItem);
621
622 24
        return $ruleItem;
623
    }
624
625
    /**
626
     * 注册分组下的路由规则
627
     * @access public
628
     * @param  Rule   $rule   路由规则
629
     * @return $this
630
     */
631 24
    public function addRuleItem(Rule $rule)
632
    {
633 24
        $this->rules[] = $rule;
634 24
        return $this;
635
    }
636
637
    /**
638
     * 设置分组的路由前缀
639
     * @access public
640
     * @param  string $prefix 路由前缀
641
     * @return $this
642
     */
643
    public function prefix(string $prefix)
644
    {
645
        if ($this->parent && $this->parent->getOption('prefix')) {
646
            $prefix = $this->parent->getOption('prefix') . $prefix;
647
        }
648
649
        return $this->setOption('prefix', $prefix);
650
    }
651
652
    /**
653
     * 合并分组的路由规则正则
654
     * @access public
655
     * @param  bool $merge 是否合并
656
     * @return $this
657
     */
658 6
    public function mergeRuleRegex(bool $merge = true)
659
    {
660 6
        return $this->setOption('merge_rule_regex', $merge);
661
    }
662
663
    /**
664
     * 设置分组的Dispatch调度
665
     * @access public
666
     * @param  string $dispatch 调度类
667
     * @return $this
668
     */
669
    public function dispatcher(string $dispatch)
670
    {
671
        return $this->setOption('dispatcher', $dispatch);
672
    }
673
674
    /**
675
     * 获取完整分组Name
676
     * @access public
677
     * @return string
678
     */
679 27
    public function getFullName(): string
680
    {
681 27
        return $this->fullName ?: '';
682
    }
683
684
    /**
685
     * 获取分组的路由规则
686
     * @access public
687
     * @param  string $method 请求类型
688
     * @return array
689
     */
690 27
    public function getRules(string $method = ''): array
691
    {
692 27
        if ('' === $method) {
693
            return $this->rules;
694
        }
695
696 27
        return array_filter($this->rules, function ($item) use ($method) {
697 24
            $ruleMethod = $item->getMethod();
698 24
            return '*' == $ruleMethod || str_contains($ruleMethod, $method);
699 27
        });
700
    }
701
702
    /**
703
     * 清空分组下的路由规则
704
     * @access public
705
     * @return void
706
     */
707
    public function clear(): void
708
    {
709
        $this->rules = [];
710
    }
711
}
712