chengkun
2025-09-15 0cc7f61de2b106c9664033fc27d6426d072ea019
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2025 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
declare (strict_types = 1);
 
namespace think\route;
 
use Closure;
use think\Container;
use think\Exception;
use think\helper\Str;
use think\Request;
use think\Route;
use think\route\dispatch\Callback as CallbackDispatch;
use think\route\dispatch\Controller as ControllerDispatch;
 
/**
 * 路由分组类
 */
class RuleGroup extends Rule
{
    /**
     * 分组路由(包括子分组)
     * @var Rule[]
     */
    protected $rules = [];
 
    /**
     * MISS路由
     * @var RuleItem
     */
    protected $miss;
 
    /**
     * 完整名称
     * @var string
     */
    protected $fullName;
 
    /**
     * 分组别名
     * @var string
     */
    protected $alias;
 
    /**
     * 分组绑定
     * @var string
     */
    protected $bind;
 
    /**
     * 是否已经解析
     * @var bool
     */
    protected $hasParsed;
 
    /**
     * 架构函数
     * @access public
     * @param  Route     $router 路由对象
     * @param  RuleGroup $parent 上级对象
     * @param  string    $name   分组名称
     * @param  mixed     $rule   分组路由
     * @param  bool      $lazy   延迟解析
     */
    public function __construct(Route $router, ?RuleGroup $parent = null, string $name = '', $rule = null, bool $lazy = false)
    {
        $this->router = $router;
        $this->parent = $parent;
        $this->rule   = $rule;
        $this->name   = trim($name, '/');
 
        $this->setFullName();
 
        if ($this->parent) {
            $this->domain = $this->parent->getDomain();
            $this->parent->addRuleItem($this);
        }
 
        if (!$lazy) {
            $this->parseGroupRule($rule);
        }
    }
 
    /**
     * 设置分组的路由规则
     * @access public
     * @return void
     */
    protected function setFullName(): void
    {
        if (str_contains($this->name, ':')) {
            $this->name = preg_replace(['/\[\:(\w+)\]/', '/\:(\w+)/'], ['<\1?>', '<\1>'], $this->name);
        }
 
        if ($this->parent && $this->parent->getFullName()) {
            $this->fullName = $this->parent->getFullName() . ($this->name ? '/' . $this->name : '');
        } else {
            $this->fullName = $this->name;
        }
 
        if ($this->name) {
            $this->router->getRuleName()->setGroup($this->name, $this);
        }
    }
 
    /**
     * 获取所属域名
     * @access public
     * @return string
     */
    public function getDomain(): string
    {
        return $this->domain ?: '-';
    }
 
    /**
     * 获取分组别名
     * @access public
     * @return string
     */
    public function getAlias(): string
    {
        return $this->alias ?: '';
    }
 
    /**
     * 检测分组路由
     * @access public
     * @param  Request $request       请求对象
     * @param  string  $url           访问地址
     * @param  bool    $completeMatch 路由是否完全匹配
     * @return Dispatch|false
     */
    public function check(Request $request, string $url, bool $completeMatch = false)
    {
        // 检查分组有效性
        if (!$this->checkOption($this->option, $request) || !$this->checkUrl($url)) {
            return false;
        }
 
        // 解析分组路由
        if (!$this->hasParsed) {
            $this->parseGroupRule($this->rule);
        }
 
        // 获取当前路由规则
        $method = strtolower($request->method());
        $rules  = $this->getRules($method);
        $option = $this->getOption();
 
        if (isset($option['complete_match'])) {
            $completeMatch = $option['complete_match'];
        }
 
        if (!empty($option['merge_rule_regex'])) {
            // 路由合并检查
            $result = $this->checkMergeRuleRegex($request, $rules, $url, $completeMatch);
 
            if (false !== $result) {
                return $result;
            }
        } else {
            // 检查分组路由
            foreach ($rules as $item) {
                $result = $item->check($request, $url, $completeMatch);
 
                if (false !== $result) {
                    return $result;
                }
            }
        }
 
        if ($this->bind) {
            // 检查分组绑定
            return $this->checkBind($request, $url, $option);
        }
 
        if ($miss = $this->getMissRule($method)) {
            // MISS路由
            return $miss->parseRule($request, '', $miss->getRoute(), $url, $miss->getOption());
        }
 
        return false;
    }
 
    /**
     * 分组URL匹配检查
     * @access protected
     * @param  string $url URL
     * @return bool
     */
    protected function checkUrl(string $url): bool
    {
        if ($this->fullName) {
            $pos = strpos($this->fullName, '<');
 
            if (false !== $pos) {
                $str = substr($this->fullName, 0, $pos);
            } else {
                $str = $this->fullName;
            }
 
            if ($str && 0 !== stripos(str_replace('|', '/', $url), $str)) {
                return false;
            }
        }
 
        return true;
    }
 
    /**
     * 设置路由分组别名
     * @access public
     * @param  string $alias 路由分组别名
     * @return $this
     */
    public function alias(string $alias)
    {
        $this->alias = $alias;
        $this->router->getRuleName()->setGroup($alias, $this);
 
        return $this;
    }
 
    /**
     * 解析分组和域名的路由规则及绑定
     * @access public
     * @param  mixed $rule 路由规则
     * @return void
     */
    public function parseGroupRule($rule): void
    {
        if (is_string($rule) && is_subclass_of($rule, Dispatch::class)) {
            $this->dispatcher($rule);
            return;
        }
 
        $origin = $this->router->getGroup();
        $this->router->setGroup($this);
 
        if ($rule instanceof Closure) {
            Container::getInstance()->invokeFunction($rule);
        } elseif (is_string($rule) && $rule) {
            $this->bind($rule);
        }
 
        $this->router->setGroup($origin);
        $this->hasParsed = true;
    }
 
    /**
     * 检测分组路由
     * @access public
     * @param  Request $request       请求对象
     * @param  array   $rules         路由规则
     * @param  string  $url           访问地址
     * @param  bool    $completeMatch 路由是否完全匹配
     * @return Dispatch|false
     */
    protected function checkMergeRuleRegex(Request $request, array &$rules, string $url, bool $completeMatch)
    {
        $depr  = $this->config('pathinfo_depr');
        $url   = $depr . str_replace('|', $depr, $url);
        $regex = [];
        $items = [];
 
        foreach ($rules as $key => $item) {
            if ($item instanceof RuleItem) {
                $rule = $depr . str_replace('/', $depr, $item->getRule());
                if ($depr == $rule && $depr != $url) {
                    unset($rules[$key]);
                    continue;
                }
 
                $complete = $item->getOption('complete_match', $completeMatch);
 
                if (!str_contains($rule, '<')) {
                    if (0 === strcasecmp($rule, $url) || (!$complete && 0 === strncasecmp($rule, $url, strlen($rule)))) {
                        return $item->checkRule($request, $url, []);
                    }
 
                    unset($rules[$key]);
                    continue;
                }
 
                $slash = preg_quote('/-' . $depr, '/');
 
                if ($matchRule = preg_split('/[' . $slash . ']<\w+\??>/', $rule, 2)) {
                    if ($matchRule[0] && 0 !== strncasecmp($rule, $url, strlen($matchRule[0]))) {
                        unset($rules[$key]);
                        continue;
                    }
                }
 
                if (preg_match_all('/[' . $slash . ']?<?\w+\??>?/', $rule, $matches)) {
                    unset($rules[$key]);
                    $pattern = array_merge($this->getPattern(), $item->getPattern());
                    $option  = array_merge($this->getOption(), $item->getOption());
 
                    $regex[$key] = $this->buildRuleRegex($rule, $matches[0], $pattern, $option, $complete, '_THINK_' . $key);
                    $items[$key] = $item;
                }
            } elseif ($item instanceof RuleGroup) {
                $array = $item->getrules();
                return $this->checkMergeRuleRegex($request, $array, ltrim($url, $depr), $completeMatch);
            }
        }
 
        if (empty($regex)) {
            return false;
        }
 
        try {
            $result = preg_match('~^(?:' . implode('|', $regex) . ')~u', $url, $match);
        } catch (\Exception $e) {
            throw new Exception('route pattern error');
        }
 
        if ($result) {
            $var = [];
            foreach ($match as $key => $val) {
                if (is_string($key) && '' !== $val) {
                    [$name, $pos] = explode('_THINK_', $key);
 
                    $var[$name] = $val;
                }
            }
 
            if (!isset($pos)) {
                foreach ($regex as $key => $item) {
                    if (str_starts_with(str_replace(['\/', '\-', '\\' . $depr], ['/', '-', $depr], $item), $match[0])) {
                        $pos = $key;
                        break;
                    }
                }
            }
 
            $rule  = $items[$pos]->getRule();
            $array = $this->router->getRule($rule);
 
            foreach ($array as $item) {
                if (in_array($item->getMethod(), ['*', strtolower($request->method())])) {
                    $result = $item->checkRule($request, $url, $var);
 
                    if (false !== $result) {
                        return $result;
                    }
                }
            }
        }
 
        return false;
    }
 
    /**
     * 注册MISS路由
     * @access public
     * @param  string|Closure $route  路由地址
     * @param  string         $method 请求类型
     * @return RuleItem
     */
    public function miss(string | Closure $route, string $method = '*'): RuleItem
    {
        // 创建路由规则实例
        $method   = strtolower($method);
        $ruleItem = new RuleItem($this->router, $this, null, '', $route, $method);
 
        $this->miss[$method] = $ruleItem->setMiss();
 
        return $ruleItem;
    }
 
    /**
     * 获取分组下的MISS路由
     * @access public
     * @param  string $method 请求类型
     * @return RuleItem|null
     */
    public function getMissRule(string $method = '*'): ?RuleItem
    {
        if (isset($this->miss[$method])) {
            $miss = $this->miss[$method];
        } elseif (isset($this->miss['*'])) {
            $miss = $this->miss['*'];
        }
        return $miss ?? null;
    }
 
    /**
     * 分组自动URL调度  默认绑定到当前分组名所在的控制器分级
     * @access public
     * @param  string       $bind 绑定资源 绑定规则 class @controller :namespace /layer
     * @param  string|array $middleware 中间件
     * @return $this
     */
    public function auto(string $bind = '', string | array $middleware = '')
    {
        $this->bind = $bind ?: '/' . $this->getFullName();
        if ($middleware) {
            $this->middleware($middleware);
        }
 
        return $this;
    }
 
    /**
     * 分组绑定到类
     * @access public
     * @param  string $class
     * @param  bool   $prefix
     * @return $this
     */
    public function class(string $class, bool $prefix = true)
    {
        $this->bind = '\\' . $class;
        if ($prefix) {
            $this->prefix('\\' . $class . '@');
        }
        return $this;
    }
 
    /**
     * 分组绑定到控制器
     * @access public
     * @param  string $controller
     * @param  bool   $prefix
     * @return $this
     */
    public function controller(string $controller, bool $prefix = true)
    {
        $this->bind = '@' . $controller;
        if ($prefix) {
            $this->prefix($controller . '/');
        }
        return $this;
    }
 
    /**
     * 分组绑定到命名空间
     * @access public
     * @param  string $namespace
     * @param  bool   $prefix
     * @return $this
     */
    public function namespace(string $namespace, bool $prefix = true)
    {
        $this->bind = ':' . $namespace;
        if ($prefix) {
            $this->prefix($namespace . '\\');
        }
        return $this;
    }
 
    /**
     * 分组绑定到控制器分级
     * @access public
     * @param  string $namespace
     * @param  bool   $prefix
     * @return $this
     */
    public function layer(string $layer, bool $prefix = true)
    {
        $this->bind = '/' . $layer;
        if ($prefix) {
            $this->prefix($layer . '/');
        }
        return $this;
    }
 
    /**
     * 获取分组绑定信息
     * @access public
     * @return string
     */
    public function getBind()
    {
        return $this->bind ?? '';
    }
 
    /**
     * 检测URL绑定
     * @access private
     * @param  Request   $request
     * @param  string    $url URL地址
     * @param  array     $option 分组参数
     * @return Dispatch
     */
    public function checkBind(Request $request, string $url, array $option = []): Dispatch
    {
        [$bind, $param] = $this->parseBindAppendParam($this->bind);
 
        [$call, $bind] = match (substr($bind, 0, 1)) {
            '\\' => ['bindToClass', substr($bind, 1)],
            '@' => ['bindToController', substr($bind, 1)],
            '/' => ['bindToLayer', substr($bind, 1)],
            ':' => ['bindToNamespace', substr($bind, 1)],
            default => ['bindToClass', $bind],
        };
 
        $name = $this->getFullName();
        $url  = trim(substr(str_replace('|', '/', $url), strlen($name)), '/');
 
        return $this->$call($request, $url, $bind, $param, $option);
    }
 
    protected function parseBindAppendParam(string $bind)
    {
        $vars = [];
        if (str_contains($bind, '?')) {
            [$bind, $query] = explode('?', $bind);
            parse_str($query, $vars);
        }
        return [$bind, $vars];
    }
 
    /**
     * 绑定到类
     * @access protected
     * @param  Request   $request
     * @param  string    $url URL地址
     * @param  string    $class 类名(带命名空间)
     * @param  array     $param  路由变量
     * @param  array     $option 分组参数
     * @return CallbackDispatch
     */
    protected function bindToClass(Request $request, string $url, string $class, array $param = [], array $option = []): CallbackDispatch
    {
        $array  = explode('/', $url, 2);
        $action = !empty($array[0]) ? $array[0] : $this->config('default_action');
 
        if (!empty($array[1])) {
            $this->parseUrlParams($array[1], $param);
        }
 
        return new CallbackDispatch($request, $this, [$class, $action], $param, $option);
    }
 
    /**
     * 绑定到命名空间
     * @access protected
     * @param  Request   $request
     * @param  string    $url URL地址
     * @param  string    $namespace 命名空间
     * @param  array     $param  路由变量
     * @param  array     $option 分组参数
     * @return CallbackDispatch
     */
    protected function bindToNamespace(Request $request, string $url, string $namespace, array $param = [], array $option = []): CallbackDispatch
    {
        $array  = explode('/', $url, 3);
        $class  = !empty($array[0]) ? $array[0] : $this->config('default_controller');
        $method = !empty($array[1]) ? $array[1] : $this->config('default_action');
 
        if (!empty($array[2])) {
            $this->parseUrlParams($array[2], $param);
        }
 
        return new CallbackDispatch($request, $this, [trim($namespace, '\\') . '\\' . Str::studly($class), $method], $param, $option);
    }
 
    /**
     * 绑定到控制器
     * @access protected
     * @param  Request   $request
     * @param  string    $url URL地址
     * @param  string    $controller 控制器名
     * @param  array     $param  路由变量
     * @param  array     $option 分组参数
     * @return ControllerDispatch
     */
    protected function bindToController(Request $request, string $url, string $controller, array $param = [], array $option = []): ControllerDispatch
    {
        $array  = explode('/', $url, 2);
        $action = !empty($array[0]) ? $array[0] : $this->config('default_action');
 
        if (!empty($array[1])) {
            $this->parseUrlParams($array[1], $param);
        }
 
        return new ControllerDispatch($request, $this, [$controller, $action], $param, $option);
    }
 
    /**
     * 绑定到控制器分级
     * @access protected
     * @param  Request   $request
     * @param  string    $url URL地址
     * @param  string    $controller 控制器名
     * @param  array     $param  路由变量
     * @param  array     $option 分组参数
     * @return ControllerDispatch
     */
    protected function bindToLayer(Request $request, string $url, string $layer, array $param = [], array $option = []): ControllerDispatch
    {
        $array      = explode('/', $url, 3);
        $controller = !empty($array[0]) ? $array[0] : $this->config('default_controller');
        $action     = !empty($array[1]) ? $array[1] : $this->config('default_action');
 
        if (!empty($array[2])) {
            $this->parseUrlParams($array[2], $param);
        }
 
        return new ControllerDispatch($request, $this, [$layer, $controller, $action], $param, $option);
    }
 
    /**
     * 添加分组下的路由规则
     * @access public
     * @param  string $rule   路由规则
     * @param  mixed  $route  路由地址
     * @param  string $method 请求类型
     * @return RuleItem
     */
    public function addRule(string $rule, $route = null, string $method = '*'): RuleItem
    {
        // 读取路由标识
        if (is_string($route)) {
            $name = $route;
        } else {
            $name = null;
        }
 
        $method = strtolower($method);
 
        if ('' === $rule || '/' === $rule) {
            $rule .= '$';
        }
 
        // 创建路由规则实例
        $ruleItem = new RuleItem($this->router, $this, $name, $rule, $route, $method);
 
        $this->addRuleItem($ruleItem);
 
        return $ruleItem;
    }
 
    /**
     * 注册分组下的路由规则
     * @access public
     * @param  Rule   $rule   路由规则
     * @return $this
     */
    public function addRuleItem(Rule $rule)
    {
        $this->rules[] = $rule;
        return $this;
    }
 
    /**
     * 设置分组的路由前缀
     * @access public
     * @param  string $prefix 路由前缀
     * @return $this
     */
    public function prefix(string $prefix)
    {
        if ($this->parent && $this->parent->getOption('prefix')) {
            $prefix = $this->parent->getOption('prefix') . $prefix;
        }
 
        return $this->setOption('prefix', $prefix);
    }
 
    /**
     * 合并分组的路由规则正则
     * @access public
     * @param  bool $merge 是否合并
     * @return $this
     */
    public function mergeRuleRegex(bool $merge = true)
    {
        return $this->setOption('merge_rule_regex', $merge);
    }
 
    /**
     * 设置分组的Dispatch调度
     * @access public
     * @param  string $dispatch 调度类
     * @return $this
     */
    public function dispatcher(string $dispatch)
    {
        return $this->setOption('dispatcher', $dispatch);
    }
 
    /**
     * 获取完整分组Name
     * @access public
     * @return string
     */
    public function getFullName(): string
    {
        return $this->fullName ?: '';
    }
 
    /**
     * 获取分组的路由规则
     * @access public
     * @param  string $method 请求类型
     * @return array
     */
    public function getRules(string $method = ''): array
    {
        if ('' === $method) {
            return $this->rules;
        }
 
        return array_filter($this->rules, function ($item) use ($method) {
            $ruleMethod = $item->getMethod();
            return '*' == $ruleMethod || str_contains($ruleMethod, $method);
        });
    }
 
    /**
     * 清空分组下的路由规则
     * @access public
     * @return void
     */
    public function clear(): void
    {
        $this->rules = [];
    }
}