-
-
Notifications
You must be signed in to change notification settings - Fork 306
Expand file tree
/
Copy pathTargetedCacheControl.php
More file actions
152 lines (126 loc) · 4.13 KB
/
Copy pathTargetedCacheControl.php
File metadata and controls
152 lines (126 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\HttpFoundation;
/**
* Cache directives addressed to one cache, as defined by RFC 9213.
*
* A cache that knows itself as "CDN" reads "CDN-Cache-Control" and ignores
* "Cache-Control"; every other cache does the opposite. That makes it possible
* to keep a response for hours in a CDN while telling browsers not to store it.
*
* There is no "s-maxage" counterpart to setMaxAge(): the target already names
* the cache the directives are for, and "max-age" is the only one every cache
* implementing RFC 9213 is required to honour.
*
* @see https://www.rfc-editor.org/rfc/rfc9213.html
*
* @author David Buchmann <mail@davidbu.ch>
*/
final class TargetedCacheControl
{
/**
* RFC 9213 section 2: the target is a token, matched case-insensitively.
*/
private const TARGET_REGEX = '/^[!#$%&\'*+.^_`|~0-9A-Za-z-]+$/D';
private readonly string $header;
/**
* @param string $target The cache the directives are addressed to, e.g. "CDN"
*
* @throws \InvalidArgumentException When the target is not a valid token
*/
public function __construct(
private readonly ResponseHeaderBag $headers,
string $target,
) {
if (!preg_match(self::TARGET_REGEX, $target)) {
throw new \InvalidArgumentException(\sprintf('The cache-control target "%s" is not a valid token.', $target));
}
$this->header = $target.'-Cache-Control';
}
public function setMaxAge(int $value): static
{
return $this->set('max-age', (string) $value);
}
public function setStaleWhileRevalidate(int $value): static
{
return $this->set('stale-while-revalidate', (string) $value);
}
public function setStaleIfError(int $value): static
{
return $this->set('stale-if-error', (string) $value);
}
public function setPublic(): static
{
return $this->set('public')->remove('private');
}
public function setPrivate(): static
{
return $this->set('private')->remove('public');
}
public function setImmutable(bool $immutable = true): static
{
return $immutable ? $this->set('immutable') : $this->remove('immutable');
}
public function setNoStore(bool $noStore = true): static
{
return $noStore ? $this->set('no-store') : $this->remove('no-store');
}
/**
* Adds or replaces a directive, for the ones this class does not model.
*
* Passing false removes the directive.
*/
public function set(string $directive, bool|string $value = true): static
{
if (false === $value) {
return $this->remove($directive);
}
$directives = $this->all();
$directives[strtolower($directive)] = $value;
return $this->write($directives);
}
public function remove(string $directive): static
{
$directives = $this->all();
unset($directives[strtolower($directive)]);
return $this->write($directives);
}
public function has(string $directive): bool
{
return \array_key_exists(strtolower($directive), $this->all());
}
public function get(string $directive): bool|string|null
{
return $this->all()[strtolower($directive)] ?? null;
}
/**
* @return array<string, bool|string>
*/
public function all(): array
{
if (!$header = $this->headers->all($this->header)) {
return [];
}
return HeaderUtils::combine(HeaderUtils::split(implode(', ', $header), ',='));
}
/**
* @param array<string, bool|string> $directives
*/
private function write(array $directives): static
{
if (!$directives) {
$this->headers->remove($this->header);
return $this;
}
ksort($directives);
$this->headers->set($this->header, HeaderUtils::toString($directives, ','));
return $this;
}
}