Completed
Push — 6.0 ( cb907d...4e9554 )
by liu
03:34
created

RuleName::getGroup()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
ccs 0
cts 2
cp 0
crap 2
rs 10
c 0
b 0
f 0
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~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
/**
16
 * 路由标识管理类
17
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
18
class RuleName
19
{
20
    /**
21
     * 路由标识
22
     * @var array
23
     */
24
    protected $item = [];
25
26
    /**
27
     * 路由规则
28
     * @var array
29
     */
30
    protected $rule = [];
31
32
    /**
33
     * 路由分组
34
     * @var array
35
     */
36
    protected $group = [];
37
38
    /**
39
     * 注册路由标识
40
     * @access public
41
     * @param  string       $name  路由标识
42
     * @param  string|array $value 路由规则
43
     * @param  bool         $first 是否置顶
44
     * @return void
45
     */
46
    public function setName(string $name, $value, bool $first = false): void
47
    {
48
        $name = strtolower($name);
49
        if ($first && isset($this->item[$name])) {
50
            array_unshift($this->item[$name], $value);
51
        } else {
52
            $this->item[$name][] = $value;
53
        }
54
    }
55
56
    /**
57
     * 注册路由分组标识
58
     * @access public
59
     * @param  string    $name  路由分组标识
60
     * @param  RuleGroup $group 路由分组
61
     * @return void
62
     */
63
    public function setGroup(string $name, RuleGroup $group): void
64
    {
65
        $this->group[strtolower($name)] = $group;
66
    }
67
68
    /**
69
     * 注册路由规则
70
     * @access public
71
     * @param  string   $rule  路由规则
72
     * @param  RuleItem $route 路由
73
     * @return void
74
     */
75
    public function setRule(string $rule, RuleItem $route): void
76
    {
77
        $this->rule[$rule][$route->getRoute()] = $route;
78
    }
79
80
    /**
81
     * 根据路由规则获取路由对象(列表)
82
     * @access public
83
     * @param  string $rule   路由标识
0 ignored issues
show
Coding Style introduced by
Expected 1 spaces after parameter name; 3 found
Loading history...
84
     * @return RuleItem[]
85
     */
86
    public function getRule(string $rule): array
87
    {
88
        return $this->rule[$rule] ?? [];
89
    }
90
91
    /**
92
     * 根据路由分组标识获取分组
93
     * @access public
94
     * @param  string $name 路由分组标识
95
     * @return RuleGroup|null
96
     */
97
    public function getGroup(string $name)
98
    {
99
        return $this->group[strtolower($name)] ?? null;
100
    }
101
102
    /**
103
     * 清空路由规则
104
     * @access public
105
     * @return void
106
     */
107
    public function clear(): void
108
    {
109
        $this->item = [];
110
        $this->rule = [];
111
    }
112
113
    /**
114
     * 获取全部路由列表
115
     * @access public
116
     * @return array
117
     */
118
    public function getRuleList(): array
119
    {
120
        $list = [];
121
122
        foreach ($this->rule as $rule => $rules) {
123
            foreach ($rules as $item) {
124
                $val = [];
125
126
                foreach (['method', 'rule', 'name', 'route', 'domain', 'pattern', 'option'] as $param) {
127
                    $call        = 'get' . $param;
128
                    $val[$param] = $item->$call();
129
                }
130
131
                $list[] = $val;
132
            }
133
        }
134
135
        return $list;
136
    }
137
138
    /**
139
     * 导入路由标识
140
     * @access public
141
     * @param  array $item 路由标识
142
     * @return void
143
     */
144
    public function import(array $item): void
145
    {
146
        $this->item = $item;
147
    }
148
149
    /**
150
     * 根据路由标识获取路由信息(用于URL生成)
151
     * @access public
152
     * @param  string $name   路由标识
153
     * @param  string $domain 域名
154
     * @param  string $method 请求类型
155
     * @return array
156
     */
157
    public function getName(string $name = null, string $domain = null, string $method = '*'): array
158
    {
159
        if (is_null($name)) {
160
            return $this->item;
161
        }
162
163
        $name   = strtolower($name);
164
        $method = strtolower($method);
165
        $result = [];
166
167
        if (isset($this->item[$name])) {
168
            if (is_null($domain)) {
169
                $result = $this->item[$name];
170
            } else {
171
                foreach ($this->item[$name] as $item) {
172
                    if (($item[2] == $domain || '-' == $item[2]) && ('*' == $item[4] || '*' == $method || $method == $item[4])) {
173
                        $result[] = $item;
174
                    }
175
                }
176
            }
177
        }
178
179
        return $result;
180
    }
181
182
}
183