Passed
Push — 8.0 ( f1ecf8...279f5c )
by liu
13:10
created

RuleGroup::setFullName()   A

Complexity

Conditions 6
Paths 12

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 7.8984

Importance

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

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