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