Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 string $baseUrl;
27
    private string $pathInfo;
28
    private string $method;
29
    private string $host;
30
    private string $scheme;
31
    private int $httpPort;
32
    private int $httpsPort;
33
    private string $queryString;
34
    private array $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): static
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
    public function getBaseUrl(): string
88
    {
89
        return $this->baseUrl;
90
    }
91
 
92
    /**
93
     * Sets the base URL.
94
     *
95
     * @return $this
96
     */
97
    public function setBaseUrl(string $baseUrl): static
98
    {
99
        $this->baseUrl = rtrim($baseUrl, '/');
100
 
101
        return $this;
102
    }
103
 
104
    /**
105
     * Gets the path info.
106
     */
107
    public function getPathInfo(): string
108
    {
109
        return $this->pathInfo;
110
    }
111
 
112
    /**
113
     * Sets the path info.
114
     *
115
     * @return $this
116
     */
117
    public function setPathInfo(string $pathInfo): static
118
    {
119
        $this->pathInfo = $pathInfo;
120
 
121
        return $this;
122
    }
123
 
124
    /**
125
     * Gets the HTTP method.
126
     *
127
     * The method is always an uppercased string.
128
     */
129
    public function getMethod(): string
130
    {
131
        return $this->method;
132
    }
133
 
134
    /**
135
     * Sets the HTTP method.
136
     *
137
     * @return $this
138
     */
139
    public function setMethod(string $method): static
140
    {
141
        $this->method = strtoupper($method);
142
 
143
        return $this;
144
    }
145
 
146
    /**
147
     * Gets the HTTP host.
148
     *
149
     * The host is always lowercased because it must be treated case-insensitive.
150
     */
151
    public function getHost(): string
152
    {
153
        return $this->host;
154
    }
155
 
156
    /**
157
     * Sets the HTTP host.
158
     *
159
     * @return $this
160
     */
161
    public function setHost(string $host): static
162
    {
163
        $this->host = strtolower($host);
164
 
165
        return $this;
166
    }
167
 
168
    /**
169
     * Gets the HTTP scheme.
170
     */
171
    public function getScheme(): string
172
    {
173
        return $this->scheme;
174
    }
175
 
176
    /**
177
     * Sets the HTTP scheme.
178
     *
179
     * @return $this
180
     */
181
    public function setScheme(string $scheme): static
182
    {
183
        $this->scheme = strtolower($scheme);
184
 
185
        return $this;
186
    }
187
 
188
    /**
189
     * Gets the HTTP port.
190
     */
191
    public function getHttpPort(): int
192
    {
193
        return $this->httpPort;
194
    }
195
 
196
    /**
197
     * Sets the HTTP port.
198
     *
199
     * @return $this
200
     */
201
    public function setHttpPort(int $httpPort): static
202
    {
203
        $this->httpPort = $httpPort;
204
 
205
        return $this;
206
    }
207
 
208
    /**
209
     * Gets the HTTPS port.
210
     */
211
    public function getHttpsPort(): int
212
    {
213
        return $this->httpsPort;
214
    }
215
 
216
    /**
217
     * Sets the HTTPS port.
218
     *
219
     * @return $this
220
     */
221
    public function setHttpsPort(int $httpsPort): static
222
    {
223
        $this->httpsPort = $httpsPort;
224
 
225
        return $this;
226
    }
227
 
228
    /**
229
     * Gets the query string without the "?".
230
     */
231
    public function getQueryString(): string
232
    {
233
        return $this->queryString;
234
    }
235
 
236
    /**
237
     * Sets the query string.
238
     *
239
     * @return $this
240
     */
241
    public function setQueryString(?string $queryString): static
242
    {
243
        // string cast to be fault-tolerant, accepting null
244
        $this->queryString = (string) $queryString;
245
 
246
        return $this;
247
    }
248
 
249
    /**
250
     * Returns the parameters.
251
     */
252
    public function getParameters(): array
253
    {
254
        return $this->parameters;
255
    }
256
 
257
    /**
258
     * Sets the parameters.
259
     *
260
     * @param array $parameters The parameters
261
     *
262
     * @return $this
263
     */
264
    public function setParameters(array $parameters): static
265
    {
266
        $this->parameters = $parameters;
267
 
268
        return $this;
269
    }
270
 
271
    /**
272
     * Gets a parameter value.
273
     */
274
    public function getParameter(string $name): mixed
275
    {
276
        return $this->parameters[$name] ?? null;
277
    }
278
 
279
    /**
280
     * Checks if a parameter value is set for the given parameter.
281
     */
282
    public function hasParameter(string $name): bool
283
    {
284
        return \array_key_exists($name, $this->parameters);
285
    }
286
 
287
    /**
288
     * Sets a parameter value.
289
     *
290
     * @return $this
291
     */
292
    public function setParameter(string $name, mixed $parameter): static
293
    {
294
        $this->parameters[$name] = $parameter;
295
 
296
        return $this;
297
    }
298
 
299
    public function isSecure(): bool
300
    {
301
        return 'https' === $this->scheme;
302
    }
303
}