1 | <?php |
||
8 | class CacheEntry |
||
9 | { |
||
10 | /** |
||
11 | * @var RequestInterface |
||
12 | */ |
||
13 | protected $request; |
||
14 | |||
15 | /** |
||
16 | * @var ResponseInterface |
||
17 | */ |
||
18 | protected $response; |
||
19 | |||
20 | /** |
||
21 | * This field is only used for serialize. |
||
22 | * Response::body is a stream and can't be serialized. |
||
23 | * |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $responseBody; |
||
27 | |||
28 | /** |
||
29 | * @var \DateTime |
||
30 | */ |
||
31 | protected $staleAt; |
||
32 | |||
33 | /** |
||
34 | * @var \DateTime |
||
35 | */ |
||
36 | protected $staleIfErrorTo; |
||
37 | |||
38 | /** |
||
39 | * @var \DateTime |
||
40 | */ |
||
41 | protected $staleWhileRevalidateTo; |
||
42 | |||
43 | /** |
||
44 | * @var \DateTime |
||
45 | */ |
||
46 | protected $dateCreated; |
||
47 | |||
48 | /** |
||
49 | * Cached timestamp of staleAt variable. |
||
50 | * |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $timestampStale; |
||
54 | |||
55 | /** |
||
56 | * @param RequestInterface $request |
||
57 | * @param ResponseInterface $response |
||
58 | * @param \DateTime $staleAt |
||
59 | * @param \DateTime|null $staleIfErrorTo if null, detected with the headers (RFC 5861) |
||
60 | * @param \DateTime|null $staleWhileRevalidateTo |
||
61 | */ |
||
62 | public function __construct( |
||
63 | RequestInterface $request, |
||
64 | ResponseInterface $response, |
||
65 | \DateTime $staleAt, |
||
66 | \DateTime $staleIfErrorTo = null, |
||
67 | \DateTime $staleWhileRevalidateTo = null |
||
68 | ) { |
||
69 | $this->dateCreated = new \DateTime(); |
||
70 | |||
71 | $this->request = $request; |
||
72 | $this->response = $response; |
||
73 | $this->staleAt = $staleAt; |
||
74 | |||
75 | $values = new KeyValueHttpHeader($response->getHeader('Cache-Control')); |
||
76 | |||
77 | if ($staleIfErrorTo === null && $values->has('stale-if-error')) { |
||
78 | $this->staleIfErrorTo = (new \DateTime( |
||
79 | '@'.($this->staleAt->getTimestamp() + (int) $values->get('stale-if-error')) |
||
80 | )); |
||
81 | } else { |
||
82 | $this->staleIfErrorTo = $staleIfErrorTo; |
||
83 | } |
||
84 | |||
85 | if ($staleWhileRevalidateTo === null && $values->has('stale-while-revalidate')) { |
||
86 | $this->staleWhileRevalidateTo = new \DateTime( |
||
87 | '@'.($this->staleAt->getTimestamp() + (int) $values->get('stale-while-revalidate')) |
||
88 | ); |
||
89 | } else { |
||
90 | $this->staleWhileRevalidateTo = $staleWhileRevalidateTo; |
||
91 | } |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return ResponseInterface |
||
96 | */ |
||
97 | public function getResponse() |
||
102 | |||
103 | /** |
||
104 | * @return ResponseInterface |
||
105 | */ |
||
106 | public function getOriginalResponse() |
||
110 | |||
111 | /** |
||
112 | * @return RequestInterface |
||
113 | */ |
||
114 | public function getOriginalRequest() |
||
118 | |||
119 | /** |
||
120 | * @param RequestInterface $request |
||
121 | * @return bool |
||
122 | */ |
||
123 | public function isVaryEquals(RequestInterface $request) |
||
124 | { |
||
125 | if ($this->response->hasHeader('Vary')) { |
||
126 | if ($this->request === null) { |
||
127 | return false; |
||
128 | } |
||
129 | |||
130 | foreach ($this->getVaryHeaders() as $key => $value) { |
||
131 | if (!$this->request->hasHeader($key) |
||
132 | && !$request->hasHeader($key) |
||
133 | ) { |
||
134 | // Absent from both |
||
135 | continue; |
||
136 | } elseif ($this->request->getHeaderLine($key) |
||
137 | == $request->getHeaderLine($key) |
||
138 | ) { |
||
139 | // Same content |
||
140 | continue; |
||
141 | } |
||
142 | |||
143 | return false; |
||
144 | } |
||
145 | } |
||
146 | |||
147 | return true; |
||
148 | } |
||
149 | |||
150 | /** |
||
151 | * Get the vary headers that should be honoured by the cache. |
||
152 | * |
||
153 | * @return KeyValueHttpHeader |
||
154 | */ |
||
155 | public function getVaryHeaders() |
||
159 | |||
160 | /** |
||
161 | * @return \DateTime |
||
162 | */ |
||
163 | public function getStaleAt() |
||
167 | |||
168 | /** |
||
169 | * @return bool |
||
170 | */ |
||
171 | public function isFresh() |
||
175 | |||
176 | /** |
||
177 | * @return bool |
||
178 | */ |
||
179 | public function isStale() |
||
183 | |||
184 | /** |
||
185 | * @return int positive value equal staled |
||
186 | */ |
||
187 | public function getStaleAge() |
||
196 | |||
197 | /** |
||
198 | * @return bool |
||
199 | */ |
||
200 | public function serveStaleIfError() |
||
205 | |||
206 | /** |
||
207 | * @return bool |
||
208 | */ |
||
209 | public function staleWhileValidate() |
||
214 | |||
215 | /** |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function hasValidationInformation() |
||
222 | |||
223 | /** |
||
224 | * @return int TTL in seconds (0 = infinite) |
||
225 | */ |
||
226 | public function getTTL() |
||
244 | |||
245 | /** |
||
246 | * @return int Age in seconds |
||
247 | */ |
||
248 | public function getAge() |
||
252 | |||
253 | public function __sleep() |
||
263 | |||
264 | public function __wakeup() |
||
274 | } |
||
275 |