17
17
* Holds information about the current request.
18
18
*
19
19
* @author Fabien Potencier <fabien@symfony.com>
20
+ * @author Tobias Schultze <http://tobion.de>
20
21
*
21
22
* @api
22
23
*/
@@ -52,16 +53,21 @@ class RequestContext
52
53
*/
53
54
public function __construct ($ baseUrl = '' , $ method = 'GET ' , $ host = 'localhost ' , $ scheme = 'http ' , $ httpPort = 80 , $ httpsPort = 443 , $ path = '/ ' , $ queryString = '' )
54
55
{
55
- $ this ->baseUrl = $ baseUrl ;
56
- $ this ->method = strtoupper ($ method );
57
- $ this ->host = $ host ;
58
- $ this ->scheme = strtolower ($ scheme );
59
- $ this ->httpPort = $ httpPort ;
60
- $ this ->httpsPort = $ httpsPort ;
61
- $ this ->pathInfo = $ path ;
62
- $ this ->queryString = $ queryString ;
56
+ $ this ->setBaseUrl ( $ baseUrl) ;
57
+ $ this ->setMethod ($ method );
58
+ $ this ->setHost ( $ host) ;
59
+ $ this ->setScheme ($ scheme );
60
+ $ this ->setHttpPort ( $ httpPort) ;
61
+ $ this ->setHttpsPort ( $ httpsPort) ;
62
+ $ this ->setPathInfo ( $ path) ;
63
+ $ this ->setQueryString ( $ queryString) ;
63
64
}
64
65
66
+ /**
67
+ * Updates the RequestContext information based on a HttpFoundation Request.
68
+ *
69
+ * @param Request $request A Request instance
70
+ */
65
71
public function fromRequest (Request $ request )
66
72
{
67
73
$ this ->setBaseUrl ($ request ->getBaseUrl ());
@@ -71,7 +77,7 @@ public function fromRequest(Request $request)
71
77
$ this ->setScheme ($ request ->getScheme ());
72
78
$ this ->setHttpPort ($ request ->isSecure () ? $ this ->httpPort : $ request ->getPort ());
73
79
$ this ->setHttpsPort ($ request ->isSecure () ? $ request ->getPort () : $ this ->httpsPort );
74
- $ this ->setQueryString ($ request ->server ->get ('QUERY_STRING ' ));
80
+ $ this ->setQueryString ($ request ->server ->get ('QUERY_STRING ' , '' ));
75
81
}
76
82
77
83
/**
@@ -143,6 +149,8 @@ public function setMethod($method)
143
149
/**
144
150
* Gets the HTTP host.
145
151
*
152
+ * The host is always lowercased because it must be treated case-insensitive.
153
+ *
146
154
* @return string The HTTP host
147
155
*/
148
156
public function getHost ()
@@ -159,7 +167,7 @@ public function getHost()
159
167
*/
160
168
public function setHost ($ host )
161
169
{
162
- $ this ->host = $ host ;
170
+ $ this ->host = strtolower ( $ host) ;
163
171
}
164
172
165
173
/**
@@ -187,7 +195,7 @@ public function setScheme($scheme)
187
195
/**
188
196
* Gets the HTTP port.
189
197
*
190
- * @return string The HTTP port
198
+ * @return int The HTTP port
191
199
*/
192
200
public function getHttpPort ()
193
201
{
@@ -197,19 +205,19 @@ public function getHttpPort()
197
205
/**
198
206
* Sets the HTTP port.
199
207
*
200
- * @param string $httpPort The HTTP port
208
+ * @param int $httpPort The HTTP port
201
209
*
202
210
* @api
203
211
*/
204
212
public function setHttpPort ($ httpPort )
205
213
{
206
- $ this ->httpPort = $ httpPort ;
214
+ $ this ->httpPort = ( int ) $ httpPort ;
207
215
}
208
216
209
217
/**
210
218
* Gets the HTTPS port.
211
219
*
212
- * @return string The HTTPS port
220
+ * @return int The HTTPS port
213
221
*/
214
222
public function getHttpsPort ()
215
223
{
@@ -219,19 +227,19 @@ public function getHttpsPort()
219
227
/**
220
228
* Sets the HTTPS port.
221
229
*
222
- * @param string $httpsPort The HTTPS port
230
+ * @param int $httpsPort The HTTPS port
223
231
*
224
232
* @api
225
233
*/
226
234
public function setHttpsPort ($ httpsPort )
227
235
{
228
- $ this ->httpsPort = $ httpsPort ;
236
+ $ this ->httpsPort = ( int ) $ httpsPort ;
229
237
}
230
238
231
239
/**
232
240
* Gets the query string.
233
241
*
234
- * @return string The query string
242
+ * @return string The query string without the "?"
235
243
*/
236
244
public function getQueryString ()
237
245
{
@@ -241,13 +249,14 @@ public function getQueryString()
241
249
/**
242
250
* Sets the query string.
243
251
*
244
- * @param string $queryString The query string
252
+ * @param string $queryString The query string (after "?")
245
253
*
246
254
* @api
247
255
*/
248
256
public function setQueryString ($ queryString )
249
257
{
250
- $ this ->queryString = $ queryString ;
258
+ // string cast to be fault-tolerant, accepting null
259
+ $ this ->queryString = (string ) $ queryString ;
251
260
}
252
261
253
262
/**
@@ -263,11 +272,9 @@ public function getParameters()
263
272
/**
264
273
* Sets the parameters.
265
274
*
266
- * This method implements a fluent interface.
267
- *
268
275
* @param array $parameters The parameters
269
276
*
270
- * @return Route The current Route instance
277
+ * @return RequestContext The current instance, implementing a fluent interface
271
278
*/
272
279
public function setParameters (array $ parameters )
273
280
{
@@ -281,7 +288,7 @@ public function setParameters(array $parameters)
281
288
*
282
289
* @param string $name A parameter name
283
290
*
284
- * @return mixed The parameter value
291
+ * @return mixed The parameter value or null if nonexistent
285
292
*/
286
293
public function getParameter ($ name )
287
294
{
@@ -293,7 +300,7 @@ public function getParameter($name)
293
300
*
294
301
* @param string $name A parameter name
295
302
*
296
- * @return bool true if the parameter value is set, false otherwise
303
+ * @return bool True if the parameter value is set, false otherwise
297
304
*/
298
305
public function hasParameter ($ name )
299
306
{
0 commit comments