1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link http://www.yiiframework.com/ |
4
|
|
|
* @copyright Copyright (c) 2008 Yii Software LLC |
5
|
|
|
* @license http://www.yiiframework.com/license/ |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace yii\rest; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\InvalidConfigException; |
12
|
|
|
use yii\helpers\Inflector; |
13
|
|
|
use yii\web\CompositeUrlRule; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* UrlRule is provided to simplify the creation of URL rules for RESTful API support. |
17
|
|
|
* |
18
|
|
|
* The simplest usage of UrlRule is to declare a rule like the following in the application configuration, |
19
|
|
|
* |
20
|
|
|
* ```php |
21
|
|
|
* [ |
22
|
|
|
* 'class' => 'yii\rest\UrlRule', |
23
|
|
|
* 'controller' => 'user', |
24
|
|
|
* ] |
25
|
|
|
* ``` |
26
|
|
|
* |
27
|
|
|
* The above code will create a whole set of URL rules supporting the following RESTful API endpoints: |
28
|
|
|
* |
29
|
|
|
* - `'PUT,PATCH users/<id>' => 'user/update'`: update a user |
30
|
|
|
* - `'DELETE users/<id>' => 'user/delete'`: delete a user |
31
|
|
|
* - `'GET,HEAD users/<id>' => 'user/view'`: return the details/overview/options of a user |
32
|
|
|
* - `'POST users' => 'user/create'`: create a new user |
33
|
|
|
* - `'GET,HEAD users' => 'user/index'`: return a list/overview/options of users |
34
|
|
|
* - `'users/<id>' => 'user/options'`: process all unhandled verbs of a user |
35
|
|
|
* - `'users' => 'user/options'`: process all unhandled verbs of user collection |
36
|
|
|
* |
37
|
|
|
* You may configure [[only]] and/or [[except]] to disable some of the above rules. |
38
|
|
|
* You may configure [[patterns]] to completely redefine your own list of rules. |
39
|
|
|
* You may configure [[controller]] with multiple controller IDs to generate rules for all these controllers. |
40
|
|
|
* For example, the following code will disable the `delete` rule and generate rules for both `user` and `post` controllers: |
41
|
|
|
* |
42
|
|
|
* ```php |
43
|
|
|
* [ |
44
|
|
|
* 'class' => 'yii\rest\UrlRule', |
45
|
|
|
* 'controller' => ['user', 'post'], |
46
|
|
|
* 'except' => ['delete'], |
47
|
|
|
* ] |
48
|
|
|
* ``` |
49
|
|
|
* |
50
|
|
|
* The property [[controller]] is required and should represent one or multiple controller IDs. |
51
|
|
|
* Each controller ID should be prefixed with the module ID if the controller is within a module. |
52
|
|
|
* The controller ID used in the pattern will be automatically pluralized (e.g. `user` becomes `users` |
53
|
|
|
* as shown in the above examples). |
54
|
|
|
* |
55
|
|
|
* For more details and usage information on UrlRule, see the [guide article on rest routing](guide:rest-routing). |
56
|
|
|
* |
57
|
|
|
* @author Qiang Xue <[email protected]> |
58
|
|
|
* @since 2.0 |
59
|
|
|
*/ |
60
|
|
|
class UrlRule extends CompositeUrlRule |
61
|
|
|
{ |
62
|
|
|
/** |
63
|
|
|
* @var string the common prefix string shared by all patterns. |
64
|
|
|
*/ |
65
|
|
|
public $prefix; |
66
|
|
|
/** |
67
|
|
|
* @var string the suffix that will be assigned to [[\yii\web\UrlRule::suffix]] for every generated rule. |
68
|
|
|
*/ |
69
|
|
|
public $suffix; |
70
|
|
|
/** |
71
|
|
|
* @var string|array the controller ID (e.g. `user`, `post-comment`) that the rules in this composite rule |
72
|
|
|
* are dealing with. It should be prefixed with the module ID if the controller is within a module (e.g. `admin/user`). |
73
|
|
|
* |
74
|
|
|
* By default, the controller ID will be pluralized automatically when it is put in the patterns of the |
75
|
|
|
* generated rules. If you want to explicitly specify how the controller ID should appear in the patterns, |
76
|
|
|
* you may use an array with the array key being as the controller ID in the pattern, and the array value |
77
|
|
|
* the actual controller ID. For example, `['u' => 'user']`. |
78
|
|
|
* |
79
|
|
|
* You may also pass multiple controller IDs as an array. If this is the case, this composite rule will |
80
|
|
|
* generate applicable URL rules for EVERY specified controller. For example, `['user', 'post']`. |
81
|
|
|
*/ |
82
|
|
|
public $controller; |
83
|
|
|
/** |
84
|
|
|
* @var array list of acceptable actions. If not empty, only the actions within this array |
85
|
|
|
* will have the corresponding URL rules created. |
86
|
|
|
* @see patterns |
87
|
|
|
*/ |
88
|
|
|
public $only = []; |
89
|
|
|
/** |
90
|
|
|
* @var array list of actions that should be excluded. Any action found in this array |
91
|
|
|
* will NOT have its URL rules created. |
92
|
|
|
* @see patterns |
93
|
|
|
*/ |
94
|
|
|
public $except = []; |
95
|
|
|
/** |
96
|
|
|
* @var array patterns for supporting extra actions in addition to those listed in [[patterns]]. |
97
|
|
|
* The keys are the patterns and the values are the corresponding action IDs. |
98
|
|
|
* These extra patterns will take precedence over [[patterns]]. |
99
|
|
|
*/ |
100
|
|
|
public $extraPatterns = []; |
101
|
|
|
/** |
102
|
|
|
* @var array list of tokens that should be replaced for each pattern. The keys are the token names, |
103
|
|
|
* and the values are the corresponding replacements. |
104
|
|
|
* @see patterns |
105
|
|
|
*/ |
106
|
|
|
public $tokens = [ |
107
|
|
|
'{id}' => '<id:\\d[\\d,]*>', |
108
|
|
|
]; |
109
|
|
|
/** |
110
|
|
|
* @var array list of possible patterns and the corresponding actions for creating the URL rules. |
111
|
|
|
* The keys are the patterns and the values are the corresponding actions. |
112
|
|
|
* The format of patterns is `Verbs Pattern`, where `Verbs` stands for a list of HTTP verbs separated |
113
|
|
|
* by comma (without space). If `Verbs` is not specified, it means all verbs are allowed. |
114
|
|
|
* `Pattern` is optional. It will be prefixed with [[prefix]]/[[controller]]/, |
115
|
|
|
* and tokens in it will be replaced by [[tokens]]. |
116
|
|
|
*/ |
117
|
|
|
public $patterns = [ |
118
|
|
|
'PUT,PATCH {id}' => 'update', |
119
|
|
|
'DELETE {id}' => 'delete', |
120
|
|
|
'GET,HEAD {id}' => 'view', |
121
|
|
|
'POST' => 'create', |
122
|
|
|
'GET,HEAD' => 'index', |
123
|
|
|
'{id}' => 'options', |
124
|
|
|
'' => 'options', |
125
|
|
|
]; |
126
|
|
|
/** |
127
|
|
|
* @var array the default configuration for creating each URL rule contained by this rule. |
128
|
|
|
*/ |
129
|
|
|
public $ruleConfig = [ |
130
|
|
|
'class' => 'yii\web\UrlRule', |
131
|
|
|
]; |
132
|
|
|
/** |
133
|
|
|
* @var bool whether to automatically pluralize the URL names for controllers. |
134
|
|
|
* If true, a controller ID will appear in plural form in URLs. For example, `user` controller |
135
|
|
|
* will appear as `users` in URLs. |
136
|
|
|
* @see controller |
137
|
|
|
*/ |
138
|
|
|
public $pluralize = true; |
139
|
|
|
|
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @inheritdoc |
143
|
|
|
*/ |
144
|
9 |
|
public function init() |
145
|
|
|
{ |
146
|
9 |
|
if (empty($this->controller)) { |
147
|
|
|
throw new InvalidConfigException('"controller" must be set.'); |
148
|
|
|
} |
149
|
|
|
|
150
|
9 |
|
$controllers = []; |
151
|
9 |
|
foreach ((array) $this->controller as $urlName => $controller) { |
152
|
9 |
|
if (is_int($urlName)) { |
153
|
9 |
|
$urlName = $this->pluralize ? Inflector::pluralize($controller) : $controller; |
154
|
9 |
|
} |
155
|
9 |
|
$controllers[$urlName] = $controller; |
156
|
9 |
|
} |
157
|
9 |
|
$this->controller = $controllers; |
158
|
|
|
|
159
|
9 |
|
$this->prefix = trim($this->prefix, '/'); |
160
|
|
|
|
161
|
9 |
|
parent::init(); |
162
|
9 |
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @inheritdoc |
166
|
|
|
*/ |
167
|
9 |
|
protected function createRules() |
168
|
|
|
{ |
169
|
9 |
|
$only = array_flip($this->only); |
170
|
9 |
|
$except = array_flip($this->except); |
171
|
9 |
|
$patterns = $this->extraPatterns + $this->patterns; |
172
|
9 |
|
$rules = []; |
173
|
9 |
|
foreach ($this->controller as $urlName => $controller) { |
|
|
|
|
174
|
9 |
|
$prefix = trim($this->prefix . '/' . $urlName, '/'); |
175
|
9 |
|
foreach ($patterns as $pattern => $action) { |
176
|
9 |
|
if (!isset($except[$action]) && (empty($only) || isset($only[$action]))) { |
177
|
9 |
|
$rules[$urlName][] = $this->createRule($pattern, $prefix, $controller . '/' . $action); |
178
|
9 |
|
} |
179
|
9 |
|
} |
180
|
9 |
|
} |
181
|
|
|
|
182
|
9 |
|
return $rules; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Creates a URL rule using the given pattern and action. |
187
|
|
|
* @param string $pattern |
188
|
|
|
* @param string $prefix |
189
|
|
|
* @param string $action |
190
|
|
|
* @return \yii\web\UrlRuleInterface |
191
|
|
|
*/ |
192
|
9 |
|
protected function createRule($pattern, $prefix, $action) |
193
|
|
|
{ |
194
|
9 |
|
$verbs = 'GET|HEAD|POST|PUT|PATCH|DELETE|OPTIONS'; |
195
|
9 |
|
if (preg_match("/^((?:($verbs),)*($verbs))(?:\\s+(.*))?$/", $pattern, $matches)) { |
196
|
9 |
|
$verbs = explode(',', $matches[1]); |
197
|
9 |
|
$pattern = isset($matches[4]) ? $matches[4] : ''; |
198
|
9 |
|
} else { |
199
|
9 |
|
$verbs = []; |
200
|
|
|
} |
201
|
|
|
|
202
|
9 |
|
$config = $this->ruleConfig; |
203
|
9 |
|
$config['verb'] = $verbs; |
204
|
9 |
|
$config['pattern'] = rtrim($prefix . '/' . strtr($pattern, $this->tokens), '/'); |
205
|
9 |
|
$config['route'] = $action; |
206
|
9 |
|
if (!empty($verbs) && !in_array('GET', $verbs)) { |
207
|
9 |
|
$config['mode'] = \yii\web\UrlRule::PARSING_ONLY; |
208
|
9 |
|
} |
209
|
9 |
|
$config['suffix'] = $this->suffix; |
210
|
|
|
|
211
|
9 |
|
return Yii::createObject($config); |
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* @inheritdoc |
216
|
|
|
*/ |
217
|
1 |
|
public function parseRequest($manager, $request) |
218
|
|
|
{ |
219
|
1 |
|
$pathInfo = $request->getPathInfo(); |
220
|
1 |
|
foreach ($this->rules as $urlName => $rules) { |
221
|
1 |
|
if (strpos($pathInfo, $urlName) !== false) { |
222
|
1 |
|
foreach ($rules as $rule) { |
|
|
|
|
223
|
|
|
/* @var $rule \yii\web\UrlRule */ |
224
|
1 |
|
if (($result = $rule->parseRequest($manager, $request)) !== false) { |
225
|
1 |
|
Yii::trace("Request parsed with URL rule: {$rule->name}", __METHOD__); |
226
|
|
|
|
227
|
1 |
|
return $result; |
228
|
|
|
} |
229
|
1 |
|
} |
230
|
1 |
|
} |
231
|
1 |
|
} |
232
|
|
|
|
233
|
1 |
|
return false; |
234
|
|
|
} |
235
|
|
|
|
236
|
|
|
/** |
237
|
|
|
* @inheritdoc |
238
|
|
|
*/ |
239
|
7 |
|
public function createUrl($manager, $route, $params) |
240
|
|
|
{ |
241
|
7 |
|
foreach ($this->controller as $urlName => $controller) { |
|
|
|
|
242
|
7 |
|
if (strpos($route, $controller) !== false) { |
243
|
7 |
|
foreach ($this->rules[$urlName] as $rule) { |
|
|
|
|
244
|
|
|
/* @var $rule \yii\web\UrlRule */ |
245
|
7 |
|
if (($url = $rule->createUrl($manager, $route, $params)) !== false) { |
246
|
7 |
|
return $url; |
247
|
|
|
} |
248
|
7 |
|
} |
249
|
7 |
|
} |
250
|
7 |
|
} |
251
|
|
|
|
252
|
7 |
|
return false; |
253
|
|
|
} |
254
|
|
|
} |
255
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.