Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\Routing;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * Holds information about the current request.
|
Chris@0
|
18 *
|
Chris@0
|
19 * This class implements a fluent interface.
|
Chris@0
|
20 *
|
Chris@0
|
21 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
22 * @author Tobias Schultze <http://tobion.de>
|
Chris@0
|
23 */
|
Chris@0
|
24 class RequestContext
|
Chris@0
|
25 {
|
Chris@0
|
26 private $baseUrl;
|
Chris@0
|
27 private $pathInfo;
|
Chris@0
|
28 private $method;
|
Chris@0
|
29 private $host;
|
Chris@0
|
30 private $scheme;
|
Chris@0
|
31 private $httpPort;
|
Chris@0
|
32 private $httpsPort;
|
Chris@0
|
33 private $queryString;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * @var array
|
Chris@0
|
37 */
|
Chris@0
|
38 private $parameters = array();
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * Constructor.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @param string $baseUrl The base URL
|
Chris@0
|
44 * @param string $method The HTTP method
|
Chris@0
|
45 * @param string $host The HTTP host name
|
Chris@0
|
46 * @param string $scheme The HTTP scheme
|
Chris@0
|
47 * @param int $httpPort The HTTP port
|
Chris@0
|
48 * @param int $httpsPort The HTTPS port
|
Chris@0
|
49 * @param string $path The path
|
Chris@0
|
50 * @param string $queryString The query string
|
Chris@0
|
51 */
|
Chris@0
|
52 public function __construct($baseUrl = '', $method = 'GET', $host = 'localhost', $scheme = 'http', $httpPort = 80, $httpsPort = 443, $path = '/', $queryString = '')
|
Chris@0
|
53 {
|
Chris@0
|
54 $this->setBaseUrl($baseUrl);
|
Chris@0
|
55 $this->setMethod($method);
|
Chris@0
|
56 $this->setHost($host);
|
Chris@0
|
57 $this->setScheme($scheme);
|
Chris@0
|
58 $this->setHttpPort($httpPort);
|
Chris@0
|
59 $this->setHttpsPort($httpsPort);
|
Chris@0
|
60 $this->setPathInfo($path);
|
Chris@0
|
61 $this->setQueryString($queryString);
|
Chris@0
|
62 }
|
Chris@0
|
63
|
Chris@0
|
64 /**
|
Chris@0
|
65 * Updates the RequestContext information based on a HttpFoundation Request.
|
Chris@0
|
66 *
|
Chris@0
|
67 * @param Request $request A Request instance
|
Chris@0
|
68 *
|
Chris@0
|
69 * @return $this
|
Chris@0
|
70 */
|
Chris@0
|
71 public function fromRequest(Request $request)
|
Chris@0
|
72 {
|
Chris@0
|
73 $this->setBaseUrl($request->getBaseUrl());
|
Chris@0
|
74 $this->setPathInfo($request->getPathInfo());
|
Chris@0
|
75 $this->setMethod($request->getMethod());
|
Chris@0
|
76 $this->setHost($request->getHost());
|
Chris@0
|
77 $this->setScheme($request->getScheme());
|
Chris@0
|
78 $this->setHttpPort($request->isSecure() ? $this->httpPort : $request->getPort());
|
Chris@0
|
79 $this->setHttpsPort($request->isSecure() ? $request->getPort() : $this->httpsPort);
|
Chris@0
|
80 $this->setQueryString($request->server->get('QUERY_STRING', ''));
|
Chris@0
|
81
|
Chris@0
|
82 return $this;
|
Chris@0
|
83 }
|
Chris@0
|
84
|
Chris@0
|
85 /**
|
Chris@0
|
86 * Gets the base URL.
|
Chris@0
|
87 *
|
Chris@0
|
88 * @return string The base URL
|
Chris@0
|
89 */
|
Chris@0
|
90 public function getBaseUrl()
|
Chris@0
|
91 {
|
Chris@0
|
92 return $this->baseUrl;
|
Chris@0
|
93 }
|
Chris@0
|
94
|
Chris@0
|
95 /**
|
Chris@0
|
96 * Sets the base URL.
|
Chris@0
|
97 *
|
Chris@0
|
98 * @param string $baseUrl The base URL
|
Chris@0
|
99 *
|
Chris@0
|
100 * @return $this
|
Chris@0
|
101 */
|
Chris@0
|
102 public function setBaseUrl($baseUrl)
|
Chris@0
|
103 {
|
Chris@0
|
104 $this->baseUrl = $baseUrl;
|
Chris@0
|
105
|
Chris@0
|
106 return $this;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 /**
|
Chris@0
|
110 * Gets the path info.
|
Chris@0
|
111 *
|
Chris@0
|
112 * @return string The path info
|
Chris@0
|
113 */
|
Chris@0
|
114 public function getPathInfo()
|
Chris@0
|
115 {
|
Chris@0
|
116 return $this->pathInfo;
|
Chris@0
|
117 }
|
Chris@0
|
118
|
Chris@0
|
119 /**
|
Chris@0
|
120 * Sets the path info.
|
Chris@0
|
121 *
|
Chris@0
|
122 * @param string $pathInfo The path info
|
Chris@0
|
123 *
|
Chris@0
|
124 * @return $this
|
Chris@0
|
125 */
|
Chris@0
|
126 public function setPathInfo($pathInfo)
|
Chris@0
|
127 {
|
Chris@0
|
128 $this->pathInfo = $pathInfo;
|
Chris@0
|
129
|
Chris@0
|
130 return $this;
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Gets the HTTP method.
|
Chris@0
|
135 *
|
Chris@0
|
136 * The method is always an uppercased string.
|
Chris@0
|
137 *
|
Chris@0
|
138 * @return string The HTTP method
|
Chris@0
|
139 */
|
Chris@0
|
140 public function getMethod()
|
Chris@0
|
141 {
|
Chris@0
|
142 return $this->method;
|
Chris@0
|
143 }
|
Chris@0
|
144
|
Chris@0
|
145 /**
|
Chris@0
|
146 * Sets the HTTP method.
|
Chris@0
|
147 *
|
Chris@0
|
148 * @param string $method The HTTP method
|
Chris@0
|
149 *
|
Chris@0
|
150 * @return $this
|
Chris@0
|
151 */
|
Chris@0
|
152 public function setMethod($method)
|
Chris@0
|
153 {
|
Chris@0
|
154 $this->method = strtoupper($method);
|
Chris@0
|
155
|
Chris@0
|
156 return $this;
|
Chris@0
|
157 }
|
Chris@0
|
158
|
Chris@0
|
159 /**
|
Chris@0
|
160 * Gets the HTTP host.
|
Chris@0
|
161 *
|
Chris@0
|
162 * The host is always lowercased because it must be treated case-insensitive.
|
Chris@0
|
163 *
|
Chris@0
|
164 * @return string The HTTP host
|
Chris@0
|
165 */
|
Chris@0
|
166 public function getHost()
|
Chris@0
|
167 {
|
Chris@0
|
168 return $this->host;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * Sets the HTTP host.
|
Chris@0
|
173 *
|
Chris@0
|
174 * @param string $host The HTTP host
|
Chris@0
|
175 *
|
Chris@0
|
176 * @return $this
|
Chris@0
|
177 */
|
Chris@0
|
178 public function setHost($host)
|
Chris@0
|
179 {
|
Chris@0
|
180 $this->host = strtolower($host);
|
Chris@0
|
181
|
Chris@0
|
182 return $this;
|
Chris@0
|
183 }
|
Chris@0
|
184
|
Chris@0
|
185 /**
|
Chris@0
|
186 * Gets the HTTP scheme.
|
Chris@0
|
187 *
|
Chris@0
|
188 * @return string The HTTP scheme
|
Chris@0
|
189 */
|
Chris@0
|
190 public function getScheme()
|
Chris@0
|
191 {
|
Chris@0
|
192 return $this->scheme;
|
Chris@0
|
193 }
|
Chris@0
|
194
|
Chris@0
|
195 /**
|
Chris@0
|
196 * Sets the HTTP scheme.
|
Chris@0
|
197 *
|
Chris@0
|
198 * @param string $scheme The HTTP scheme
|
Chris@0
|
199 *
|
Chris@0
|
200 * @return $this
|
Chris@0
|
201 */
|
Chris@0
|
202 public function setScheme($scheme)
|
Chris@0
|
203 {
|
Chris@0
|
204 $this->scheme = strtolower($scheme);
|
Chris@0
|
205
|
Chris@0
|
206 return $this;
|
Chris@0
|
207 }
|
Chris@0
|
208
|
Chris@0
|
209 /**
|
Chris@0
|
210 * Gets the HTTP port.
|
Chris@0
|
211 *
|
Chris@0
|
212 * @return int The HTTP port
|
Chris@0
|
213 */
|
Chris@0
|
214 public function getHttpPort()
|
Chris@0
|
215 {
|
Chris@0
|
216 return $this->httpPort;
|
Chris@0
|
217 }
|
Chris@0
|
218
|
Chris@0
|
219 /**
|
Chris@0
|
220 * Sets the HTTP port.
|
Chris@0
|
221 *
|
Chris@0
|
222 * @param int $httpPort The HTTP port
|
Chris@0
|
223 *
|
Chris@0
|
224 * @return $this
|
Chris@0
|
225 */
|
Chris@0
|
226 public function setHttpPort($httpPort)
|
Chris@0
|
227 {
|
Chris@0
|
228 $this->httpPort = (int) $httpPort;
|
Chris@0
|
229
|
Chris@0
|
230 return $this;
|
Chris@0
|
231 }
|
Chris@0
|
232
|
Chris@0
|
233 /**
|
Chris@0
|
234 * Gets the HTTPS port.
|
Chris@0
|
235 *
|
Chris@0
|
236 * @return int The HTTPS port
|
Chris@0
|
237 */
|
Chris@0
|
238 public function getHttpsPort()
|
Chris@0
|
239 {
|
Chris@0
|
240 return $this->httpsPort;
|
Chris@0
|
241 }
|
Chris@0
|
242
|
Chris@0
|
243 /**
|
Chris@0
|
244 * Sets the HTTPS port.
|
Chris@0
|
245 *
|
Chris@0
|
246 * @param int $httpsPort The HTTPS port
|
Chris@0
|
247 *
|
Chris@0
|
248 * @return $this
|
Chris@0
|
249 */
|
Chris@0
|
250 public function setHttpsPort($httpsPort)
|
Chris@0
|
251 {
|
Chris@0
|
252 $this->httpsPort = (int) $httpsPort;
|
Chris@0
|
253
|
Chris@0
|
254 return $this;
|
Chris@0
|
255 }
|
Chris@0
|
256
|
Chris@0
|
257 /**
|
Chris@0
|
258 * Gets the query string.
|
Chris@0
|
259 *
|
Chris@0
|
260 * @return string The query string without the "?"
|
Chris@0
|
261 */
|
Chris@0
|
262 public function getQueryString()
|
Chris@0
|
263 {
|
Chris@0
|
264 return $this->queryString;
|
Chris@0
|
265 }
|
Chris@0
|
266
|
Chris@0
|
267 /**
|
Chris@0
|
268 * Sets the query string.
|
Chris@0
|
269 *
|
Chris@0
|
270 * @param string $queryString The query string (after "?")
|
Chris@0
|
271 *
|
Chris@0
|
272 * @return $this
|
Chris@0
|
273 */
|
Chris@0
|
274 public function setQueryString($queryString)
|
Chris@0
|
275 {
|
Chris@0
|
276 // string cast to be fault-tolerant, accepting null
|
Chris@0
|
277 $this->queryString = (string) $queryString;
|
Chris@0
|
278
|
Chris@0
|
279 return $this;
|
Chris@0
|
280 }
|
Chris@0
|
281
|
Chris@0
|
282 /**
|
Chris@0
|
283 * Returns the parameters.
|
Chris@0
|
284 *
|
Chris@0
|
285 * @return array The parameters
|
Chris@0
|
286 */
|
Chris@0
|
287 public function getParameters()
|
Chris@0
|
288 {
|
Chris@0
|
289 return $this->parameters;
|
Chris@0
|
290 }
|
Chris@0
|
291
|
Chris@0
|
292 /**
|
Chris@0
|
293 * Sets the parameters.
|
Chris@0
|
294 *
|
Chris@0
|
295 * @param array $parameters The parameters
|
Chris@0
|
296 *
|
Chris@0
|
297 * @return $this
|
Chris@0
|
298 */
|
Chris@0
|
299 public function setParameters(array $parameters)
|
Chris@0
|
300 {
|
Chris@0
|
301 $this->parameters = $parameters;
|
Chris@0
|
302
|
Chris@0
|
303 return $this;
|
Chris@0
|
304 }
|
Chris@0
|
305
|
Chris@0
|
306 /**
|
Chris@0
|
307 * Gets a parameter value.
|
Chris@0
|
308 *
|
Chris@0
|
309 * @param string $name A parameter name
|
Chris@0
|
310 *
|
Chris@0
|
311 * @return mixed The parameter value or null if nonexistent
|
Chris@0
|
312 */
|
Chris@0
|
313 public function getParameter($name)
|
Chris@0
|
314 {
|
Chris@0
|
315 return isset($this->parameters[$name]) ? $this->parameters[$name] : null;
|
Chris@0
|
316 }
|
Chris@0
|
317
|
Chris@0
|
318 /**
|
Chris@0
|
319 * Checks if a parameter value is set for the given parameter.
|
Chris@0
|
320 *
|
Chris@0
|
321 * @param string $name A parameter name
|
Chris@0
|
322 *
|
Chris@0
|
323 * @return bool True if the parameter value is set, false otherwise
|
Chris@0
|
324 */
|
Chris@0
|
325 public function hasParameter($name)
|
Chris@0
|
326 {
|
Chris@0
|
327 return array_key_exists($name, $this->parameters);
|
Chris@0
|
328 }
|
Chris@0
|
329
|
Chris@0
|
330 /**
|
Chris@0
|
331 * Sets a parameter value.
|
Chris@0
|
332 *
|
Chris@0
|
333 * @param string $name A parameter name
|
Chris@0
|
334 * @param mixed $parameter The parameter value
|
Chris@0
|
335 *
|
Chris@0
|
336 * @return $this
|
Chris@0
|
337 */
|
Chris@0
|
338 public function setParameter($name, $parameter)
|
Chris@0
|
339 {
|
Chris@0
|
340 $this->parameters[$name] = $parameter;
|
Chris@0
|
341
|
Chris@0
|
342 return $this;
|
Chris@0
|
343 }
|
Chris@0
|
344 }
|