1 | <?php |
||
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() |
|
163 | |||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | 9 | protected function createRules() |
|
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) |
|
235 | |||
236 | /** |
||
237 | * @inheritdoc |
||
238 | */ |
||
239 | 7 | public function createUrl($manager, $route, $params) |
|
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.