|
3 | 3 | namespace Kevinrob\GuzzleCache; |
4 | 4 |
|
5 | 5 | use GuzzleHttp\Psr7\PumpStream; |
| 6 | +use Psr\Http\Message\MessageInterface; |
6 | 7 | use Psr\Http\Message\RequestInterface; |
7 | 8 | use Psr\Http\Message\ResponseInterface; |
8 | 9 |
|
9 | | -class CacheEntry |
| 10 | +class CacheEntry implements \Serializable |
10 | 11 | { |
11 | 12 | /** |
12 | 13 | * @var RequestInterface |
@@ -256,47 +257,72 @@ public function getAge() |
256 | 257 | return time() - $this->dateCreated->getTimestamp(); |
257 | 258 | } |
258 | 259 |
|
259 | | - public function __sleep() |
| 260 | + public function __serialize(): array |
260 | 261 | { |
261 | | - // Stream/Resource can't be serialized... So we copy the content into an implementation of `Psr\Http\Message\StreamInterface` |
262 | | - if ($this->response !== null) { |
263 | | - $responseBody = (string)$this->response->getBody(); |
264 | | - $this->response = $this->response->withBody( |
265 | | - new PumpStream( |
266 | | - new BodyStore($responseBody), |
267 | | - [ |
268 | | - 'size' => mb_strlen($responseBody), |
269 | | - ] |
270 | | - ) |
271 | | - ); |
272 | | - } |
| 262 | + return [ |
| 263 | + 'request' => self::toSerializeableMessage($this->request), |
| 264 | + 'response' => $this->response !== null ? self::toSerializeableMessage($this->response) : null, |
| 265 | + 'staleAt' => $this->staleAt, |
| 266 | + 'staleIfErrorTo' => $this->staleIfErrorTo, |
| 267 | + 'staleWhileRevalidateTo' => $this->staleWhileRevalidateTo, |
| 268 | + 'dateCreated' => $this->dateCreated, |
| 269 | + 'timestampStale' => $this->timestampStale, |
| 270 | + ]; |
| 271 | + } |
| 272 | + |
| 273 | + public function __unserialize(array $data): void |
| 274 | + { |
| 275 | + $this->request = self::restoreStreamBody($data['request']); |
| 276 | + $this->response = $data['response'] !== null ? self::restoreStreamBody($data['response']) : null; |
| 277 | + $this->staleAt = $data['staleAt']; |
| 278 | + $this->staleIfErrorTo = $data['staleIfErrorTo']; |
| 279 | + $this->staleWhileRevalidateTo = $data['staleWhileRevalidateTo']; |
| 280 | + $this->dateCreated = $data['dateCreated']; |
| 281 | + $this->timestampStale = $data['timestampStale']; |
| 282 | + } |
| 283 | + |
| 284 | + /** |
| 285 | + * Stream/Resource can't be serialized... So we copy the content into an implementation of `Psr\Http\Message\StreamInterface` |
| 286 | + * |
| 287 | + * @template T of MessageInterface |
| 288 | + * |
| 289 | + * @param T $message |
| 290 | + * @return T |
| 291 | + */ |
| 292 | + private static function toSerializeableMessage(MessageInterface $message): MessageInterface |
| 293 | + { |
| 294 | + $bodyString = (string)$message->getBody(); |
273 | 295 |
|
274 | | - $requestBody = (string)$this->request->getBody(); |
275 | | - $this->request = $this->request->withBody( |
| 296 | + return $message->withBody( |
276 | 297 | new PumpStream( |
277 | | - new BodyStore($requestBody), |
| 298 | + new BodyStore($bodyString), |
278 | 299 | [ |
279 | | - 'size' => mb_strlen($requestBody) |
| 300 | + 'size' => mb_strlen($bodyString), |
280 | 301 | ] |
281 | 302 | ) |
282 | 303 | ); |
| 304 | + } |
283 | 305 |
|
284 | | - return array_keys(get_object_vars($this)); |
| 306 | + /** |
| 307 | + * @template T of MessageInterface |
| 308 | + * |
| 309 | + * @param T $message |
| 310 | + * @return T |
| 311 | + */ |
| 312 | + private static function restoreStreamBody(MessageInterface $message): MessageInterface |
| 313 | + { |
| 314 | + return $message->withBody( |
| 315 | + \GuzzleHttp\Psr7\Utils::streamFor((string) $message->getBody()) |
| 316 | + ); |
285 | 317 | } |
286 | 318 |
|
287 | | - public function __wakeup() |
| 319 | + public function serialize() |
288 | 320 | { |
289 | | - // We re-create the stream of the response |
290 | | - if ($this->response !== null) { |
291 | | - $this->response = $this->response |
292 | | - ->withBody( |
293 | | - \GuzzleHttp\Psr7\Utils::streamFor((string) $this->response->getBody()) |
294 | | - ); |
295 | | - } |
296 | | - $this->request = $this->request |
297 | | - ->withBody( |
298 | | - \GuzzleHttp\Psr7\Utils::streamFor((string) $this->request->getBody()) |
299 | | - ); |
| 321 | + return serialize($this->__serialize()); |
300 | 322 | } |
301 | 323 |
|
| 324 | + public function unserialize($data) |
| 325 | + { |
| 326 | + $this->__unserialize(unserialize($data)); |
| 327 | + } |
302 | 328 | } |
0 commit comments