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