RuleItem.php
8.58 KB
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
<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
namespace think\route;
use think\Container;
use think\Exception;
use think\Route;
class RuleItem extends Rule
{
/**
* 架构函数
* @access public
* @param Route $router 路由实例
* @param RuleGroup $parent 上级对象
* @param string $name 路由标识
* @param string|array $rule 路由规则
* @param string|\Closure $route 路由地址
* @param string $method 请求类型
* @param array $option 路由参数
* @param array $pattern 变量规则
*/
public function __construct(Route $router, RuleGroup $parent, $name, $rule, $route, $method = '*', $option = [], $pattern = [])
{
$this->router = $router;
$this->parent = $parent;
$this->name = $name;
$this->route = $route;
$this->method = $method;
$this->option = $option;
$this->pattern = $pattern;
$this->setRule($rule);
if (!empty($option['cross_domain'])) {
$this->router->setCrossDomainRule($this, $method);
}
}
/**
* 路由规则预处理
* @access public
* @param string $rule 路由规则
* @return void
*/
public function setRule($rule)
{
if ('$' == substr($rule, -1, 1)) {
// 是否完整匹配
$rule = substr($rule, 0, -1);
$this->option['complete_match'] = true;
}
$rule = '/' != $rule ? ltrim($rule, '/') : '';
if ($this->parent && $prefix = $this->parent->getFullName()) {
$rule = $prefix . ($rule ? '/' . ltrim($rule, '/') : '');
}
if (false !== strpos($rule, ':')) {
$this->rule = preg_replace(['/\[\:(\w+)\]/', '/\:(\w+)/'], ['<\1?>', '<\1>'], $rule);
} else {
$this->rule = $rule;
}
// 生成路由标识的快捷访问
$this->setRuleName();
}
/**
* 检查后缀
* @access public
* @param string $ext
* @return $this
*/
public function ext($ext = '')
{
$this->option('ext', $ext);
$this->setRuleName(true);
return $this;
}
/**
* 设置别名
* @access public
* @param string $name
* @return $this
*/
public function name($name)
{
$this->name = $name;
$this->setRuleName(true);
return $this;
}
/**
* 设置路由标识 用于URL反解生成
* @access protected
* @param bool $first 是否插入开头
* @return void
*/
protected function setRuleName($first = false)
{
if ($this->name) {
$vars = $this->parseVar($this->rule);
$name = strtolower($this->name);
if (isset($this->option['ext'])) {
$suffix = $this->option['ext'];
} elseif ($this->parent->getOption('ext')) {
$suffix = $this->parent->getOption('ext');
} else {
$suffix = null;
}
$value = [$this->rule, $vars, $this->parent->getDomain(), $suffix, $this->method];
Container::get('rule_name')->set($name, $value, $first);
Container::get('rule_name')->setRule($this->rule, $this);
}
}
/**
* 检测路由
* @access public
* @param Request $request 请求对象
* @param string $url 访问地址
* @param array $match 匹配路由变量
* @param bool $completeMatch 路由是否完全匹配
* @return Dispatch|false
*/
public function checkRule($request, $url, $match = null, $completeMatch = false)
{
if ($dispatch = $this->checkCrossDomain($request)) {
// 允许跨域
return $dispatch;
}
// 检查参数有效性
if (!$this->checkOption($this->option, $request)) {
return false;
}
// 合并分组参数
$option = $this->mergeGroupOptions();
$url = $this->urlSuffixCheck($request, $url, $option);
if (is_null($match)) {
$match = $this->match($url, $option, $completeMatch);
}
if (false !== $match) {
// 检查前置行为
if (isset($option['before']) && false === $this->checkBefore($option['before'])) {
return false;
}
return $this->parseRule($request, $this->rule, $this->route, $url, $option, $match);
}
return false;
}
/**
* 检测路由(含路由匹配)
* @access public
* @param Request $request 请求对象
* @param string $url 访问地址
* @param string $depr 路径分隔符
* @param bool $completeMatch 路由是否完全匹配
* @return Dispatch|false
*/
public function check($request, $url, $completeMatch = false)
{
return $this->checkRule($request, $url, null, $completeMatch);
}
/**
* URL后缀及Slash检查
* @access protected
* @param Request $request 请求对象
* @param string $url 访问地址
* @param array $option 路由参数
* @return string
*/
protected function urlSuffixCheck($request, $url, $option = [])
{
// 是否区分 / 地址访问
if (!empty($option['remove_slash']) && '/' != $this->rule) {
$this->rule = rtrim($this->rule, '/');
$url = rtrim($url, '|');
}
if (isset($option['ext'])) {
// 路由ext参数 优先于系统配置的URL伪静态后缀参数
$url = preg_replace('/\.(' . $request->ext() . ')$/i', '', $url);
}
return $url;
}
/**
* 检测URL和规则路由是否匹配
* @access private
* @param string $url URL地址
* @param array $option 路由参数
* @param bool $completeMatch 路由是否完全匹配
* @return array|false
*/
private function match($url, $option, $completeMatch)
{
if (isset($option['complete_match'])) {
$completeMatch = $option['complete_match'];
}
$pattern = array_merge($this->parent->getPattern(), $this->pattern);
$depr = $this->router->config('pathinfo_depr');
// 检查完整规则定义
if (isset($pattern['__url__']) && !preg_match(0 === strpos($pattern['__url__'], '/') ? $pattern['__url__'] : '/^' . $pattern['__url__'] . '/', str_replace('|', $depr, $url))) {
return false;
}
$var = [];
$url = $depr . str_replace('|', $depr, $url);
$rule = $depr . str_replace('/', $depr, $this->rule);
if ($depr == $rule && $depr != $url) {
return false;
}
if (false === strpos($rule, '<')) {
if (0 === strcasecmp($rule, $url) || (!$completeMatch && 0 === strncasecmp($rule . $depr, $url . $depr, strlen($rule . $depr)))) {
return $var;
}
return false;
}
$slash = preg_quote('/-' . $depr, '/');
if ($matchRule = preg_split('/[' . $slash . ']?<\w+\??>/', $rule, 2)) {
if ($matchRule[0] && 0 !== strncasecmp($rule, $url, strlen($matchRule[0]))) {
return false;
}
}
if (preg_match_all('/[' . $slash . ']?<?\w+\??>?/', $rule, $matches)) {
$regex = $this->buildRuleRegex($rule, $matches[0], $pattern, $option, $completeMatch);
try {
if (!preg_match('/^' . $regex . ($completeMatch ? '$' : '') . '/u', $url, $match)) {
return false;
}
} catch (\Exception $e) {
throw new Exception('route pattern error');
}
foreach ($match as $key => $val) {
if (is_string($key)) {
$var[$key] = $val;
}
}
}
// 成功匹配后返回URL中的动态变量数组
return $var;
}
}