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
|
|
|
/** |
16
|
|
|
* 路由标识管理类 |
17
|
|
|
*/ |
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 RuleItem $ruleItem 路由规则 |
43
|
|
|
* @param bool $first 是否优先 |
|
|
|
|
44
|
|
|
* @return void |
45
|
|
|
*/ |
46
|
4 |
|
public function setName(string $name, RuleItem $ruleItem, bool $first = false): void |
47
|
|
|
{ |
48
|
4 |
|
$name = strtolower($name); |
49
|
4 |
|
if ($first && isset($this->item[$name])) { |
50
|
|
|
array_unshift($this->item[$name], $ruleItem); |
51
|
|
|
} else { |
52
|
4 |
|
$this->item[$name][] = $ruleItem; |
53
|
|
|
} |
54
|
4 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 注册路由分组标识 |
58
|
|
|
* @access public |
59
|
|
|
* @param string $name 路由分组标识 |
60
|
|
|
* @param RuleGroup $group 路由分组 |
61
|
|
|
* @return void |
62
|
|
|
*/ |
63
|
1 |
|
public function setGroup(string $name, RuleGroup $group): void |
64
|
|
|
{ |
65
|
1 |
|
$this->group[strtolower($name)] = $group; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* 注册路由规则 |
70
|
|
|
* @access public |
71
|
|
|
* @param string $rule 路由规则 |
|
|
|
|
72
|
|
|
* @param RuleItem $ruleItem 路由 |
73
|
|
|
* @return void |
74
|
|
|
*/ |
75
|
6 |
|
public function setRule(string $rule, RuleItem $ruleItem): void |
76
|
|
|
{ |
77
|
6 |
|
$route = $ruleItem->getRoute(); |
78
|
|
|
|
79
|
6 |
|
if (is_string($route)) { |
80
|
4 |
|
$this->rule[$rule][$route] = $ruleItem; |
81
|
|
|
} else { |
82
|
3 |
|
$this->rule[$rule][] = $ruleItem; |
83
|
|
|
} |
84
|
6 |
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* 根据路由规则获取路由对象(列表) |
88
|
|
|
* @access public |
89
|
|
|
* @param string $rule 路由标识 |
|
|
|
|
90
|
|
|
* @return RuleItem[] |
91
|
|
|
*/ |
92
|
2 |
|
public function getRule(string $rule): array |
93
|
|
|
{ |
94
|
2 |
|
return $this->rule[$rule] ?? []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* 根据路由分组标识获取分组 |
99
|
|
|
* @access public |
100
|
|
|
* @param string $name 路由分组标识 |
101
|
|
|
* @return RuleGroup|null |
102
|
|
|
*/ |
103
|
|
|
public function getGroup(string $name) |
104
|
|
|
{ |
105
|
|
|
return $this->group[strtolower($name)] ?? null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* 清空路由规则 |
110
|
|
|
* @access public |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function clear(): void |
114
|
|
|
{ |
115
|
|
|
$this->item = []; |
116
|
|
|
$this->rule = []; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* 获取全部路由列表 |
121
|
|
|
* @access public |
122
|
|
|
* @return array |
123
|
|
|
*/ |
124
|
|
|
public function getRuleList(): array |
125
|
|
|
{ |
126
|
|
|
$list = []; |
127
|
|
|
|
128
|
|
|
foreach ($this->rule as $rule => $rules) { |
129
|
|
|
foreach ($rules as $item) { |
130
|
|
|
$val = []; |
131
|
|
|
|
132
|
|
|
foreach (['method', 'rule', 'name', 'route', 'domain', 'pattern', 'option'] as $param) { |
133
|
|
|
$call = 'get' . $param; |
134
|
|
|
$val[$param] = $item->$call(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
if ($item->isMiss()) { |
138
|
|
|
$val['rule'] .= '<MISS>'; |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
$list[] = $val; |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $list; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* 导入路由标识 |
150
|
|
|
* @access public |
151
|
|
|
* @param array $item 路由标识 |
152
|
|
|
* @return void |
153
|
|
|
*/ |
154
|
|
|
public function import(array $item): void |
155
|
|
|
{ |
156
|
|
|
$this->item = $item; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* 根据路由标识获取路由信息(用于URL生成) |
161
|
|
|
* @access public |
162
|
|
|
* @param string $name 路由标识 |
163
|
|
|
* @param string $domain 域名 |
164
|
|
|
* @param string $method 请求类型 |
165
|
|
|
* @return array |
166
|
|
|
*/ |
167
|
|
|
public function getName(string $name = null, string $domain = null, string $method = '*'): array |
168
|
|
|
{ |
169
|
|
|
if (is_null($name)) { |
170
|
|
|
return $this->item; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$name = strtolower($name); |
174
|
|
|
$method = strtolower($method); |
175
|
|
|
$result = []; |
176
|
|
|
|
177
|
|
|
if (isset($this->item[$name])) { |
178
|
|
|
if (is_null($domain)) { |
179
|
|
|
$result = $this->item[$name]; |
180
|
|
|
} else { |
181
|
|
|
foreach ($this->item[$name] as $item) { |
182
|
|
|
$itemDomain = $item->getDomain(); |
183
|
|
|
$itemMethod = $item->getMethod(); |
184
|
|
|
|
185
|
|
|
if (($itemDomain == $domain || '-' == $itemDomain) && ('*' == $itemMethod || '*' == $method || $method == $itemMethod)) { |
186
|
|
|
$result[] = $item; |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return $result; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
} |
196
|
|
|
|