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 think\Route; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 资源路由类 |
19
|
|
|
*/ |
20
|
|
|
class Resource extends RuleGroup |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* 资源路由名称 |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $resource; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 资源路由地址 |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
protected $route; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* REST方法定义 |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $rest = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* 模型绑定 |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $model = []; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* 数据验证 |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
protected $validate = []; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* 中间件 |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
protected $middleware = []; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* 架构函数 |
60
|
|
|
* @access public |
61
|
|
|
* @param Route $router 路由对象 |
|
|
|
|
62
|
|
|
* @param RuleGroup $parent 上级对象 |
|
|
|
|
63
|
|
|
* @param string $name 资源名称 |
|
|
|
|
64
|
|
|
* @param string $route 路由地址 |
|
|
|
|
65
|
|
|
* @param array $rest 资源定义 |
|
|
|
|
66
|
|
|
*/ |
67
|
1 |
|
public function __construct(Route $router, RuleGroup $parent = null, string $name = '', string $route = '', array $rest = []) |
68
|
|
|
{ |
69
|
1 |
|
$name = ltrim($name, '/'); |
70
|
1 |
|
$this->router = $router; |
71
|
1 |
|
$this->parent = $parent; |
72
|
1 |
|
$this->resource = $name; |
73
|
1 |
|
$this->route = $route; |
74
|
1 |
|
$this->name = strpos($name, '.') ? strstr($name, '.', true) : $name; |
75
|
|
|
|
76
|
1 |
|
$this->setFullName(); |
77
|
|
|
|
78
|
|
|
// 资源路由默认为完整匹配 |
79
|
1 |
|
$this->option['complete_match'] = true; |
80
|
|
|
|
81
|
1 |
|
$this->rest = $rest; |
82
|
|
|
|
83
|
1 |
|
if ($this->parent) { |
84
|
1 |
|
$this->domain = $this->parent->getDomain(); |
85
|
1 |
|
$this->parent->addRuleItem($this); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
if ($router->isTest()) { |
89
|
|
|
$this->buildResourceRule(); |
90
|
|
|
} |
91
|
1 |
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* 生成资源路由规则 |
95
|
|
|
* @access protected |
96
|
|
|
* @return void |
97
|
|
|
*/ |
98
|
1 |
|
protected function buildResourceRule(): void |
99
|
|
|
{ |
100
|
1 |
|
$rule = $this->resource; |
101
|
1 |
|
$option = $this->option; |
102
|
1 |
|
$origin = $this->router->getGroup(); |
103
|
1 |
|
$this->router->setGroup($this); |
104
|
|
|
|
105
|
1 |
|
if (strpos($rule, '.')) { |
106
|
|
|
// 注册嵌套资源路由 |
107
|
|
|
$array = explode('.', $rule); |
108
|
|
|
$last = array_pop($array); |
109
|
|
|
$item = []; |
110
|
|
|
|
111
|
|
|
foreach ($array as $val) { |
112
|
|
|
$item[] = $val . '/<' . ($option['var'][$val] ?? $val . '_id') . '>'; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
$rule = implode('/', $item) . '/' . $last; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
$prefix = substr($rule, strlen($this->name) + 1); |
119
|
|
|
|
120
|
|
|
// 注册资源路由 |
121
|
1 |
|
foreach ($this->rest as $key => $val) { |
122
|
1 |
|
if ((isset($option['only']) && !in_array($key, $option['only'])) |
123
|
1 |
|
|| (isset($option['except']) && in_array($key, $option['except']))) { |
|
|
|
|
124
|
|
|
continue; |
125
|
|
|
} |
126
|
|
|
|
127
|
1 |
|
if (isset($last) && strpos($val[1], '<id>') && isset($option['var'][$last])) { |
128
|
|
|
$val[1] = str_replace('<id>', '<' . $option['var'][$last] . '>', $val[1]); |
129
|
1 |
|
} elseif (strpos($val[1], '<id>') && isset($option['var'][$rule])) { |
130
|
|
|
$val[1] = str_replace('<id>', '<' . $option['var'][$rule] . '>', $val[1]); |
131
|
|
|
} |
132
|
|
|
|
133
|
1 |
|
$ruleItem = $this->addRule(trim($prefix . $val[1], '/'), $this->route . '/' . $val[2], $val[0]); |
134
|
|
|
|
135
|
1 |
|
foreach (['model', 'validate', 'middleware'] as $name) { |
136
|
1 |
|
if (isset($this->$name[$key])) { |
137
|
|
|
call_user_func_array([$ruleItem, $name], (array) $this->$name[$key]); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
$this->router->setGroup($origin); |
144
|
1 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* 设置资源允许 |
148
|
|
|
* @access public |
149
|
|
|
* @param array $only 资源允许 |
150
|
|
|
* @return $this |
151
|
|
|
*/ |
152
|
|
|
public function only(array $only) |
153
|
|
|
{ |
154
|
|
|
return $this->setOption('only', $only); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* 设置资源排除 |
159
|
|
|
* @access public |
160
|
|
|
* @param array $except 排除资源 |
161
|
|
|
* @return $this |
162
|
|
|
*/ |
163
|
|
|
public function except(array $except) |
164
|
|
|
{ |
165
|
|
|
return $this->setOption('except', $except); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* 设置资源路由的变量 |
170
|
|
|
* @access public |
171
|
|
|
* @param array $vars 资源变量 |
172
|
|
|
* @return $this |
173
|
|
|
*/ |
174
|
|
|
public function vars(array $vars) |
175
|
|
|
{ |
176
|
|
|
return $this->setOption('var', $vars); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* 绑定资源验证 |
181
|
|
|
* @access public |
182
|
|
|
* @param array|string $name 资源类型或者验证信息 |
|
|
|
|
183
|
|
|
* @param array|string $validate 验证信息 |
184
|
|
|
* @return $this |
185
|
|
|
*/ |
186
|
|
|
public function withValidate($name, $validate = []) |
187
|
|
|
{ |
188
|
|
|
if (is_array($name)) { |
189
|
|
|
$this->validate = array_merge($this->validate, $name); |
190
|
|
|
} else { |
191
|
|
|
$this->validate[$name] = $validate; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* 绑定资源模型 |
199
|
|
|
* @access public |
200
|
|
|
* @param array|string $name 资源类型或者模型绑定 |
|
|
|
|
201
|
|
|
* @param array|string $model 模型绑定 |
202
|
|
|
* @return $this |
203
|
|
|
*/ |
204
|
|
|
public function withModel($name, $model = []) |
205
|
|
|
{ |
206
|
|
|
if (is_array($name)) { |
207
|
|
|
$this->model = array_merge($this->model, $name); |
208
|
|
|
} else { |
209
|
|
|
$this->model[$name] = $model; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
return $this; |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* 绑定资源模型 |
217
|
|
|
* @access public |
218
|
|
|
* @param array|string $name 资源类型或者中间件定义 |
|
|
|
|
219
|
|
|
* @param array|string $middleware 中间件定义 |
220
|
|
|
* @return $this |
221
|
|
|
*/ |
222
|
|
|
public function withMiddleware($name, $middleware = []) |
223
|
|
|
{ |
224
|
|
|
if (is_array($name)) { |
225
|
|
|
$this->middleware = array_merge($this->middleware, $name); |
226
|
|
|
} else { |
227
|
|
|
$this->middleware[$name] = $middleware; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
return $this; |
231
|
|
|
} |
232
|
|
|
|
233
|
|
|
/** |
234
|
|
|
* rest方法定义和修改 |
|
|
|
|
235
|
|
|
* @access public |
236
|
|
|
* @param array|string $name 方法名称 |
|
|
|
|
237
|
|
|
* @param array|bool $resource 资源 |
238
|
|
|
* @return $this |
239
|
|
|
*/ |
240
|
|
|
public function rest($name, $resource = []) |
241
|
|
|
{ |
242
|
|
|
if (is_array($name)) { |
243
|
|
|
$this->rest = $resource ? $name : array_merge($this->rest, $name); |
244
|
|
|
} else { |
245
|
|
|
$this->rest[$name] = $resource; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|