15
15
* A Route describes a route and its parameters.
16
16
*
17
17
* @author Fabien Potencier <[email protected] >
18
+ * @author Tobias Schultze <http://tobion.de>
18
19
*
19
20
* @api
20
21
*/
@@ -30,7 +31,17 @@ class Route implements \Serializable
30
31
*/
31
32
private $ hostnamePattern = '' ;
32
33
33
- /**
34
+ /**
35
+ * @var array
36
+ */
37
+ private $ schemes = array ();
38
+
39
+ /**
40
+ * @var array
41
+ */
42
+ private $ methods = array ();
43
+
44
+ /**
34
45
* @var array
35
46
*/
36
47
private $ defaults = array ();
@@ -59,21 +70,32 @@ class Route implements \Serializable
59
70
*
60
71
* * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
61
72
*
62
- * @param string $pattern The path pattern to match
63
- * @param array $defaults An array of default parameter values
64
- * @param array $requirements An array of requirements for parameters (regexes)
65
- * @param array $options An array of options
66
- * @param string $hostnamePattern The hostname pattern to match
73
+ * @param string $pattern The path pattern to match
74
+ * @param array $defaults An array of default parameter values
75
+ * @param array $requirements An array of requirements for parameters (regexes)
76
+ * @param array $options An array of options
77
+ * @param string $hostnamePattern The hostname pattern to match
78
+ * @param string|array $schemes A required URI scheme or an array of restricted schemes
79
+ * @param string|array $methods A required HTTP method or an array of restricted methods
67
80
*
68
81
* @api
69
82
*/
70
- public function __construct ($ pattern , array $ defaults = array (), array $ requirements = array (), array $ options = array (), $ hostnamePattern = '' )
83
+ public function __construct ($ pattern , array $ defaults = array (), array $ requirements = array (), array $ options = array (),
84
+ $ hostnamePattern = '' , $ schemes = array (), $ methods = array ())
71
85
{
72
86
$ this ->setPattern ($ pattern );
73
87
$ this ->setDefaults ($ defaults );
74
88
$ this ->setRequirements ($ requirements );
75
89
$ this ->setOptions ($ options );
76
90
$ this ->setHostnamePattern ($ hostnamePattern );
91
+ // The conditions make sure that an initial empty $schemes/$methods does not override the corresponding requirement.
92
+ // They can be removed when the BC layer is removed.
93
+ if ($ schemes ) {
94
+ $ this ->setSchemes ($ schemes );
95
+ }
96
+ if ($ methods ) {
97
+ $ this ->setMethods ($ methods );
98
+ }
77
99
}
78
100
79
101
public function serialize ()
@@ -84,6 +106,8 @@ public function serialize()
84
106
'defaults ' => $ this ->defaults ,
85
107
'requirements ' => $ this ->requirements ,
86
108
'options ' => $ this ->options ,
109
+ 'schemes ' => $ this ->schemes ,
110
+ 'methods ' => $ this ->methods ,
87
111
));
88
112
}
89
113
@@ -95,6 +119,8 @@ public function unserialize($data)
95
119
$ this ->defaults = $ data ['defaults ' ];
96
120
$ this ->requirements = $ data ['requirements ' ];
97
121
$ this ->options = $ data ['options ' ];
122
+ $ this ->schemes = $ data ['schemes ' ];
123
+ $ this ->methods = $ data ['methods ' ];
98
124
}
99
125
100
126
/**
@@ -149,6 +175,80 @@ public function setHostnamePattern($pattern)
149
175
return $ this ;
150
176
}
151
177
178
+ /**
179
+ * Returns the lowercased schemes this route is restricted to.
180
+ * So an empty array means that any scheme is allowed.
181
+ *
182
+ * @return array The schemes
183
+ */
184
+ public function getSchemes ()
185
+ {
186
+ return $ this ->schemes ;
187
+ }
188
+
189
+ /**
190
+ * Sets the schemes (e.g. 'https') this route is restricted to.
191
+ * So an empty array means that any scheme is allowed.
192
+ *
193
+ * This method implements a fluent interface.
194
+ *
195
+ * @param string|array $schemes The scheme or an array of schemes
196
+ *
197
+ * @return Route The current Route instance
198
+ */
199
+ public function setSchemes ($ schemes )
200
+ {
201
+ $ this ->schemes = array_map ('strtolower ' , (array ) $ schemes );
202
+
203
+ // this is to keep BC and will be removed in a future version
204
+ if ($ this ->schemes ) {
205
+ $ this ->requirements ['_scheme ' ] = implode ('| ' , $ this ->schemes );
206
+ } else {
207
+ unset($ this ->requirements ['_scheme ' ]);
208
+ }
209
+
210
+ $ this ->compiled = null ;
211
+
212
+ return $ this ;
213
+ }
214
+
215
+ /**
216
+ * Returns the uppercased HTTP methods this route is restricted to.
217
+ * So an empty array means that any method is allowed.
218
+ *
219
+ * @return array The schemes
220
+ */
221
+ public function getMethods ()
222
+ {
223
+ return $ this ->methods ;
224
+ }
225
+
226
+ /**
227
+ * Sets the HTTP methods (e.g. 'POST') this route is restricted to.
228
+ * So an empty array means that any method is allowed.
229
+ *
230
+ * This method implements a fluent interface.
231
+ *
232
+ * @param string|array $methods The method or an array of methods
233
+ *
234
+ * @return Route The current Route instance
235
+ */
236
+ public function setMethods ($ methods )
237
+ {
238
+ $ this ->methods = array_map ('strtoupper ' , (array ) $ methods );
239
+
240
+ // this is to keep BC and will be removed in a future version
241
+ if ($ this ->methods ) {
242
+ $ this ->requirements ['_method ' ] = implode ('| ' , $ this ->methods );
243
+ } else {
244
+ unset($ this ->requirements ['_method ' ]);
245
+ }
246
+
247
+ $ this ->compiled = null ;
248
+
249
+ return $ this ;
250
+ }
251
+
152
252
/**
153
253
* Returns the options.
154
254
*
@@ -454,6 +554,13 @@ private function sanitizeRequirement($key, $regex)
454
554
throw new \InvalidArgumentException (sprintf ('Routing requirement for "%s" cannot be empty. ' , $ key ));
455
555
}
456
556
557
+ // this is to keep BC and will be removed in a future version
558
+ if ('_scheme ' === $ key ) {
559
+ $ this ->setSchemes (explode ('| ' , $ regex ));
560
+ } elseif ('_method ' === $ key ) {
561
+ $ this ->setMethods (explode ('| ' , $ regex ));
562
+ }
563
+
457
564
return $ regex ;
458
565
}
459
566
}
0 commit comments