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