Completed
Push — 6.0 ( 783c5a...da788e )
by liu
22:04 queued 10s
created

RuleGroup::addGroupOptionsRule()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 11
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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