1515 * A Route describes a route and its parameters.
1616 *
1717 * @author Fabien Potencier <[email protected] > 18+ * @author Tobias Schultze <http://tobion.de>
1819 *
1920 * @api
2021 */
@@ -30,7 +31,17 @@ class Route implements \Serializable
3031 */
3132 private $ hostnamePattern = '' ;
3233
33- /**
34+ /**
35+ * @var array
36+ */
37+ private $ schemes = array ();
38+
39+ /**
40+ * @var array
41+ */
42+ private $ methods = array ();
43+
44+ /**
3445 * @var array
3546 */
3647 private $ defaults = array ();
@@ -59,21 +70,32 @@ class Route implements \Serializable
5970 *
6071 * * compiler_class: A class name able to compile this route instance (RouteCompiler by default)
6172 *
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
6780 *
6881 * @api
6982 */
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 ())
7185 {
7286 $ this ->setPattern ($ pattern );
7387 $ this ->setDefaults ($ defaults );
7488 $ this ->setRequirements ($ requirements );
7589 $ this ->setOptions ($ options );
7690 $ 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+ }
7799 }
78100
79101 public function serialize ()
@@ -84,6 +106,8 @@ public function serialize()
84106 'defaults ' => $ this ->defaults ,
85107 'requirements ' => $ this ->requirements ,
86108 'options ' => $ this ->options ,
109+ 'schemes ' => $ this ->schemes ,
110+ 'methods ' => $ this ->methods ,
87111 ));
88112 }
89113
@@ -95,6 +119,8 @@ public function unserialize($data)
95119 $ this ->defaults = $ data ['defaults ' ];
96120 $ this ->requirements = $ data ['requirements ' ];
97121 $ this ->options = $ data ['options ' ];
122+ $ this ->schemes = $ data ['schemes ' ];
123+ $ this ->methods = $ data ['methods ' ];
98124 }
99125
100126 /**
@@ -149,6 +175,80 @@ public function setHostnamePattern($pattern)
149175 return $ this ;
150176 }
151177
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+
152252 /**
153253 * Returns the options.
154254 *
@@ -454,6 +554,13 @@ private function sanitizeRequirement($key, $regex)
454554 throw new \InvalidArgumentException (sprintf ('Routing requirement for "%s" cannot be empty. ' , $ key ));
455555 }
456556
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+
457564 return $ regex ;
458565 }
459566}
0 commit comments