|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* PSR-7 Cache Helpers |
|
4
|
|
|
* |
|
5
|
|
|
* @copyright Copyright (c) 2016, Michel Hunziker <[email protected]> |
|
6
|
|
|
* @license http://www.opensource.org/licenses/BSD-3-Clause The BSD-3-Clause License |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Micheh\Cache\Header; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Cache-Control header for a response. |
|
13
|
|
|
*/ |
|
14
|
|
|
class ResponseCacheControl extends CacheControl |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* {@inheritdoc} |
|
18
|
|
|
*/ |
|
19
|
|
|
protected static $directiveMethods = [ |
|
20
|
|
|
'public' => 'withPublic', |
|
21
|
|
|
'private' => 'withPrivate', |
|
22
|
|
|
's-maxage' => 'withSharedMaxAge', |
|
23
|
|
|
'stale-while-revalidate' => 'withStaleWhileRevalidate', |
|
24
|
|
|
'stale-if-error' => 'withStaleIfError', |
|
25
|
|
|
'must-revalidate' => 'withMustRevalidate', |
|
26
|
|
|
'proxy-revalidate' => 'withProxyRevalidate', |
|
27
|
|
|
]; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Create a new Response Cache-Control object from a header string. |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $string |
|
33
|
|
|
* @return static |
|
34
|
|
|
*/ |
|
35
|
1 |
|
public static function fromString($string) |
|
36
|
|
|
{ |
|
37
|
1 |
|
return static::createFromString($string); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Set whether a response should be cached by shared caches. The method will automatically |
|
42
|
|
|
* remove the private flag if it is set. |
|
43
|
|
|
* |
|
44
|
|
|
* @param bool $flag |
|
45
|
|
|
* @return static |
|
46
|
|
|
*/ |
|
47
|
4 |
|
public function withPublic($flag = true) |
|
48
|
|
|
{ |
|
49
|
4 |
|
return $this->withPublicPrivate(true, $flag); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return bool |
|
54
|
|
|
*/ |
|
55
|
1 |
|
public function isPublic() |
|
56
|
|
|
{ |
|
57
|
1 |
|
return $this->hasDirective('public'); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Set whether a response should be private (only cacheable by the client who made the request. |
|
62
|
|
|
* The method will automatically remove the public flag if it is set. |
|
63
|
|
|
* |
|
64
|
|
|
* @param bool $flag |
|
65
|
|
|
* @return static |
|
66
|
|
|
*/ |
|
67
|
4 |
|
public function withPrivate($flag = true) |
|
68
|
|
|
{ |
|
69
|
4 |
|
return $this->withPublicPrivate(false, $flag); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return bool |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function isPrivate() |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->hasDirective('private'); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Set how many seconds shared caches should cache the response. Use this directive only if it |
|
82
|
|
|
* is different than the max age value. |
|
83
|
|
|
* |
|
84
|
|
|
* @param int $seconds |
|
85
|
|
|
* @return static |
|
86
|
|
|
*/ |
|
87
|
1 |
|
public function withSharedMaxAge($seconds) |
|
88
|
|
|
{ |
|
89
|
1 |
|
return $this->withDirective('s-maxage', (int) $seconds); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return int|null |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function getSharedMaxAge() |
|
96
|
|
|
{ |
|
97
|
1 |
|
return $this->getDirective('s-maxage'); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Returns the number of seconds the response should be cached. The method returns the shared |
|
102
|
|
|
* max age if available and the normal max age otherwise. If both directives are not available, |
|
103
|
|
|
* the method returns `null`. |
|
104
|
|
|
* |
|
105
|
|
|
* @return int|null Lifetime in seconds if available, null otherwise |
|
106
|
|
|
*/ |
|
107
|
4 |
|
public function getLifetime() |
|
108
|
|
|
{ |
|
109
|
4 |
|
$lifetime = $this->getSharedMaxAge(); |
|
110
|
4 |
|
if ($lifetime === null) { |
|
111
|
2 |
|
$lifetime = $this->getMaxAge(); |
|
112
|
2 |
|
} |
|
113
|
|
|
|
|
114
|
4 |
|
return $lifetime; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Set how many seconds a stale representation can be used while revalidating in the background. |
|
119
|
|
|
* |
|
120
|
|
|
* @param int $seconds |
|
121
|
|
|
* @return static |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function withStaleWhileRevalidate($seconds) |
|
124
|
|
|
{ |
|
125
|
1 |
|
return $this->withDirective('stale-while-revalidate', (int) $seconds); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @return int|null |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getStaleWhileRevalidate() |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->getDirective('stale-while-revalidate'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Set how many seconds a stale representation can be used in the case of a server error. |
|
138
|
|
|
* |
|
139
|
|
|
* @param int $seconds |
|
140
|
|
|
* @return static |
|
141
|
|
|
*/ |
|
142
|
1 |
|
public function withStaleIfError($seconds) |
|
143
|
|
|
{ |
|
144
|
1 |
|
return $this->withDirective('stale-if-error', (int) $seconds); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* @return int|null |
|
149
|
|
|
*/ |
|
150
|
1 |
|
public function getStaleIfError() |
|
151
|
|
|
{ |
|
152
|
1 |
|
return $this->getDirective('stale-if-error'); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Set whether a stale representation should be validated. |
|
157
|
|
|
* |
|
158
|
|
|
* @param bool $flag |
|
159
|
|
|
* @return static |
|
160
|
|
|
*/ |
|
161
|
1 |
|
public function withMustRevalidate($flag = true) |
|
162
|
|
|
{ |
|
163
|
1 |
|
return $this->withDirective('must-revalidate', (bool) $flag); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* @return bool |
|
168
|
|
|
*/ |
|
169
|
1 |
|
public function hasMustRevalidate() |
|
170
|
|
|
{ |
|
171
|
1 |
|
return $this->hasDirective('must-revalidate'); |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
/** |
|
175
|
|
|
* Set whether a public cache should validate a stale representation. |
|
176
|
|
|
* |
|
177
|
|
|
* @param bool $flag |
|
178
|
|
|
* @return static |
|
179
|
|
|
*/ |
|
180
|
1 |
|
public function withProxyRevalidate($flag = true) |
|
181
|
|
|
{ |
|
182
|
1 |
|
return $this->withDirective('proxy-revalidate', (bool) $flag); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* @return bool |
|
187
|
|
|
*/ |
|
188
|
1 |
|
public function hasProxyRevalidate() |
|
189
|
|
|
{ |
|
190
|
1 |
|
return $this->hasDirective('proxy-revalidate'); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Convenience method to set flags which should prevent the client from caching. |
|
195
|
|
|
* Adds `no-cache, no-store, must-revalidate`. |
|
196
|
|
|
* |
|
197
|
|
|
* @return static |
|
198
|
|
|
*/ |
|
199
|
1 |
|
public function withCachePrevention() |
|
200
|
|
|
{ |
|
201
|
1 |
|
return $this->withNoCache()->withNoStore()->withMustRevalidate(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Sets the flag for the public and private directives. |
|
206
|
|
|
* |
|
207
|
|
|
* @param bool $isPublic |
|
208
|
|
|
* @param bool $flag |
|
209
|
|
|
* @return static |
|
210
|
|
|
*/ |
|
211
|
5 |
|
private function withPublicPrivate($isPublic, $flag) |
|
212
|
|
|
{ |
|
213
|
5 |
|
$type = $isPublic ? 'public' : 'private'; |
|
214
|
5 |
|
$otherType = $isPublic ? 'private' : 'public'; |
|
215
|
|
|
|
|
216
|
5 |
|
$clone = $this->withDirective($type, (bool) $flag); |
|
217
|
5 |
|
if ($flag && $clone->hasDirective($otherType)) { |
|
218
|
2 |
|
$clone = $clone->withDirective($otherType, false); |
|
219
|
2 |
|
} |
|
220
|
|
|
|
|
221
|
5 |
|
return $clone; |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|