Subversion Repositories php-qbpwcf

Rev

Details | Last modification | View Log | RSS feed

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