-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
CacheControlDirectives.php
195 lines (183 loc) · 5.99 KB
/
CacheControlDirectives.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?php
declare(strict_types=1);
namespace Neos\Flow\Http;
/*
* This file is part of the Neos.Flow package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
/**
* Cache-Control HTTP header generation/parsing
*/
class CacheControlDirectives
{
/**
* @var array
*/
protected $cacheDirectives = [
'visibility' => '',
'max-age' => '',
's-maxage' => '',
'must-revalidate' => '',
'proxy-revalidate' => '',
'no-store' => '',
'no-transform' => ''
];
/**
* @param string $rawHeaderValue The value of a specification compliant Cache-Control header
*/
private function __construct(string $rawHeaderValue)
{
foreach (array_keys($this->cacheDirectives) as $key) {
$this->cacheDirectives[$key] = '';
}
preg_match_all('/([a-zA-Z][a-zA-Z_-]*)\s*(?:=\s*(?:"([^"]*)"|([^,;\s"]*)))?/', $rawHeaderValue, $matches, PREG_SET_ORDER);
foreach ($matches as $match) {
if (isset($match[2]) && $match[2] !== '') {
$value = $match[2];
} elseif (isset($match[3]) && $match[3] !== '') {
$value = $match[3];
} else {
$value = null;
}
$this->setDirective(strtolower($match[1]), $value);
}
}
/**
* Internally sets the cache directives correctly by parsing the given
* Cache-Control header value.
*
* @param string $rawHeaderValue The value of a specification compliant Cache-Control header
* @return self
*/
public static function fromRawHeader(string $rawHeaderValue): self
{
return new self($rawHeaderValue);
}
/**
* Sets a special directive for use in the Cache-Control header, according to
* RFC 2616 / 14.9
*
* @param string $name Name of the directive, for example "max-age"
* @param string|null $value An optional value
* @return void
* @api
*/
public function setDirective(string $name, ?string $value = null): void
{
switch ($name) {
case 'public':
$this->cacheDirectives['visibility'] = 'public';
break;
case 'private':
case 'no-cache':
$this->cacheDirectives['visibility'] = $name . (!empty($value) ? '="' . $value . '"' : '');
break;
case 'no-store':
case 'no-transform':
case 'must-revalidate':
case 'proxy-revalidate':
$this->cacheDirectives[$name] = $name;
break;
case 'max-age':
case 's-maxage':
$this->cacheDirectives[$name] = $name . '=' . $value;
break;
}
}
/**
* Removes a special directive previously set for the Cache-Control header.
*
* @param string $name Name of the directive, for example "public"
* @return void
*/
public function removeDirective(string $name): void
{
switch ($name) {
case 'public':
case 'private':
case 'no-cache':
$this->cacheDirectives['visibility'] = '';
break;
case 'no-store':
case 'max-age':
case 's-maxage':
case 'no-transform':
case 'must-revalidate':
case 'proxy-revalidate':
$this->cacheDirectives[$name] = '';
break;
}
}
/**
* Returns the value of the specified Cache-Control directive.
*
* If the cache directive is not present, NULL is returned. If the specified
* directive is present but contains no value, this method returns true. Finally,
* if the directive is present and does contain a value, the value is returned.
*
* @param string $name Name of the cache directive, for example "max-age"
* @return mixed
* @api
*/
public function getDirective(string $name)
{
$value = null;
switch ($name) {
case 'public':
$value = ($this->cacheDirectives['visibility'] === 'public' ? true : null);
break;
case 'private':
case 'no-cache':
preg_match('/^(' . $name . ')(?:="([^"]+)")?$/', $this->cacheDirectives['visibility'], $matches);
if (!isset($matches[1])) {
$value = null;
} else {
$value = ($matches[2] ?? true);
}
break;
case 'no-store':
case 'no-transform':
case 'must-revalidate':
case 'proxy-revalidate':
$value = ($this->cacheDirectives[$name] !== '' ? true : null);
break;
case 'max-age':
case 's-maxage':
preg_match('/^(' . $name . ')=(.+)$/', $this->cacheDirectives[$name], $matches);
if (!isset($matches[1])) {
$value = null;
} else {
$value = (isset($matches[2]) ? (int)$matches[2] : true);
}
break;
}
return $value;
}
/**
* @return array
*/
public function getDirectives(): array
{
return array_values(array_filter($this->cacheDirectives));
}
/**
* Renders and returns a Cache-Control header value, based on the previously set
* cache control directives.
*
* @return string|null Either the value of the header or NULL if it shall be omitted
*/
public function getCacheControlHeaderValue(): ?string
{
$cacheControl = '';
foreach ($this->cacheDirectives as $cacheDirective) {
$cacheControl .= ($cacheDirective !== '' ? $cacheDirective . ', ' : '');
}
$cacheControl = trim($cacheControl, ' ,');
return ($cacheControl === '' ? null : $cacheControl);
}
}