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\HttpFoundation;
13
 
14
/**
15
 * ResponseHeaderBag is a container for Response HTTP headers.
16
 *
17
 * @author Fabien Potencier <fabien@symfony.com>
18
 */
19
class ResponseHeaderBag extends HeaderBag
20
{
21
    public const COOKIES_FLAT = 'flat';
22
    public const COOKIES_ARRAY = 'array';
23
 
24
    public const DISPOSITION_ATTACHMENT = 'attachment';
25
    public const DISPOSITION_INLINE = 'inline';
26
 
27
    protected $computedCacheControl = [];
28
    protected $cookies = [];
29
    protected $headerNames = [];
30
 
31
    public function __construct(array $headers = [])
32
    {
33
        parent::__construct($headers);
34
 
35
        if (!isset($this->headers['cache-control'])) {
36
            $this->set('Cache-Control', '');
37
        }
38
 
39
        /* RFC2616 - 14.18 says all Responses need to have a Date */
40
        if (!isset($this->headers['date'])) {
41
            $this->initDate();
42
        }
43
    }
44
 
45
    /**
46
     * Returns the headers, with original capitalizations.
47
     */
48
    public function allPreserveCase(): array
49
    {
50
        $headers = [];
51
        foreach ($this->all() as $name => $value) {
52
            $headers[$this->headerNames[$name] ?? $name] = $value;
53
        }
54
 
55
        return $headers;
56
    }
57
 
58
    public function allPreserveCaseWithoutCookies()
59
    {
60
        $headers = $this->allPreserveCase();
61
        if (isset($this->headerNames['set-cookie'])) {
62
            unset($headers[$this->headerNames['set-cookie']]);
63
        }
64
 
65
        return $headers;
66
    }
67
 
68
    public function replace(array $headers = [])
69
    {
70
        $this->headerNames = [];
71
 
72
        parent::replace($headers);
73
 
74
        if (!isset($this->headers['cache-control'])) {
75
            $this->set('Cache-Control', '');
76
        }
77
 
78
        if (!isset($this->headers['date'])) {
79
            $this->initDate();
80
        }
81
    }
82
 
83
    public function all(string $key = null): array
84
    {
85
        $headers = parent::all();
86
 
87
        if (null !== $key) {
88
            $key = strtr($key, self::UPPER, self::LOWER);
89
 
90
            return 'set-cookie' !== $key ? $headers[$key] ?? [] : array_map('strval', $this->getCookies());
91
        }
92
 
93
        foreach ($this->getCookies() as $cookie) {
94
            $headers['set-cookie'][] = (string) $cookie;
95
        }
96
 
97
        return $headers;
98
    }
99
 
100
    public function set(string $key, string|array|null $values, bool $replace = true)
101
    {
102
        $uniqueKey = strtr($key, self::UPPER, self::LOWER);
103
 
104
        if ('set-cookie' === $uniqueKey) {
105
            if ($replace) {
106
                $this->cookies = [];
107
            }
108
            foreach ((array) $values as $cookie) {
109
                $this->setCookie(Cookie::fromString($cookie));
110
            }
111
            $this->headerNames[$uniqueKey] = $key;
112
 
113
            return;
114
        }
115
 
116
        $this->headerNames[$uniqueKey] = $key;
117
 
118
        parent::set($key, $values, $replace);
119
 
120
        // ensure the cache-control header has sensible defaults
121
        if (\in_array($uniqueKey, ['cache-control', 'etag', 'last-modified', 'expires'], true) && '' !== $computed = $this->computeCacheControlValue()) {
122
            $this->headers['cache-control'] = [$computed];
123
            $this->headerNames['cache-control'] = 'Cache-Control';
124
            $this->computedCacheControl = $this->parseCacheControl($computed);
125
        }
126
    }
127
 
128
    public function remove(string $key)
129
    {
130
        $uniqueKey = strtr($key, self::UPPER, self::LOWER);
131
        unset($this->headerNames[$uniqueKey]);
132
 
133
        if ('set-cookie' === $uniqueKey) {
134
            $this->cookies = [];
135
 
136
            return;
137
        }
138
 
139
        parent::remove($key);
140
 
141
        if ('cache-control' === $uniqueKey) {
142
            $this->computedCacheControl = [];
143
        }
144
 
145
        if ('date' === $uniqueKey) {
146
            $this->initDate();
147
        }
148
    }
149
 
150
    public function hasCacheControlDirective(string $key): bool
151
    {
152
        return \array_key_exists($key, $this->computedCacheControl);
153
    }
154
 
155
    public function getCacheControlDirective(string $key): bool|string|null
156
    {
157
        return $this->computedCacheControl[$key] ?? null;
158
    }
159
 
160
    public function setCookie(Cookie $cookie)
161
    {
162
        $this->cookies[$cookie->getDomain()][$cookie->getPath()][$cookie->getName()] = $cookie;
163
        $this->headerNames['set-cookie'] = 'Set-Cookie';
164
    }
165
 
166
    /**
167
     * Removes a cookie from the array, but does not unset it in the browser.
168
     */
169
    public function removeCookie(string $name, ?string $path = '/', string $domain = null)
170
    {
171
        $path ??= '/';
172
 
173
        unset($this->cookies[$domain][$path][$name]);
174
 
175
        if (empty($this->cookies[$domain][$path])) {
176
            unset($this->cookies[$domain][$path]);
177
 
178
            if (empty($this->cookies[$domain])) {
179
                unset($this->cookies[$domain]);
180
            }
181
        }
182
 
183
        if (empty($this->cookies)) {
184
            unset($this->headerNames['set-cookie']);
185
        }
186
    }
187
 
188
    /**
189
     * Returns an array with all cookies.
190
     *
191
     * @return Cookie[]
192
     *
193
     * @throws \InvalidArgumentException When the $format is invalid
194
     */
195
    public function getCookies(string $format = self::COOKIES_FLAT): array
196
    {
197
        if (!\in_array($format, [self::COOKIES_FLAT, self::COOKIES_ARRAY])) {
198
            throw new \InvalidArgumentException(sprintf('Format "%s" invalid (%s).', $format, implode(', ', [self::COOKIES_FLAT, self::COOKIES_ARRAY])));
199
        }
200
 
201
        if (self::COOKIES_ARRAY === $format) {
202
            return $this->cookies;
203
        }
204
 
205
        $flattenedCookies = [];
206
        foreach ($this->cookies as $path) {
207
            foreach ($path as $cookies) {
208
                foreach ($cookies as $cookie) {
209
                    $flattenedCookies[] = $cookie;
210
                }
211
            }
212
        }
213
 
214
        return $flattenedCookies;
215
    }
216
 
217
    /**
218
     * Clears a cookie in the browser.
219
     */
220
    public function clearCookie(string $name, ?string $path = '/', string $domain = null, bool $secure = false, bool $httpOnly = true, string $sameSite = null)
221
    {
222
        $this->setCookie(new Cookie($name, null, 1, $path, $domain, $secure, $httpOnly, false, $sameSite));
223
    }
224
 
225
    /**
226
     * @see HeaderUtils::makeDisposition()
227
     */
228
    public function makeDisposition(string $disposition, string $filename, string $filenameFallback = '')
229
    {
230
        return HeaderUtils::makeDisposition($disposition, $filename, $filenameFallback);
231
    }
232
 
233
    /**
234
     * Returns the calculated value of the cache-control header.
235
     *
236
     * This considers several other headers and calculates or modifies the
237
     * cache-control header to a sensible, conservative value.
238
     */
239
    protected function computeCacheControlValue(): string
240
    {
241
        if (!$this->cacheControl) {
242
            if ($this->has('Last-Modified') || $this->has('Expires')) {
243
                return 'private, must-revalidate'; // allows for heuristic expiration (RFC 7234 Section 4.2.2) in the case of "Last-Modified"
244
            }
245
 
246
            // conservative by default
247
            return 'no-cache, private';
248
        }
249
 
250
        $header = $this->getCacheControlHeader();
251
        if (isset($this->cacheControl['public']) || isset($this->cacheControl['private'])) {
252
            return $header;
253
        }
254
 
255
        // public if s-maxage is defined, private otherwise
256
        if (!isset($this->cacheControl['s-maxage'])) {
257
            return $header.', private';
258
        }
259
 
260
        return $header;
261
    }
262
 
263
    private function initDate(): void
264
    {
265
        $this->set('Date', gmdate('D, d M Y H:i:s').' GMT');
266
    }
267
}