|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache\Serializers; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Response as IlluminateResponse; |
|
6
|
|
|
use Spatie\ResponseCache\Exceptions\CouldNotUnserialize; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\Response; |
|
9
|
|
|
|
|
10
|
|
|
class DefaultSerializer implements Serializer |
|
11
|
|
|
{ |
|
12
|
|
|
public const RESPONSE_TYPE_NORMAL = 'normal'; |
|
13
|
|
|
public const RESPONSE_TYPE_FILE = 'file'; |
|
14
|
|
|
|
|
15
|
|
|
public function serialize(Response $response): string |
|
16
|
|
|
{ |
|
17
|
|
|
return serialize($this->getResponseData($response)); |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public function unserialize(string $serializedResponse): Response |
|
21
|
|
|
{ |
|
22
|
|
|
$responseProperties = unserialize($serializedResponse); |
|
23
|
|
|
|
|
24
|
|
|
if (! $this->containsValidResponseProperties($responseProperties)) { |
|
25
|
|
|
throw CouldNotUnserialize::serializedResponse($serializedResponse); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$response = $this->buildResponse($responseProperties); |
|
29
|
|
|
|
|
30
|
|
|
$response->headers = $responseProperties['headers']; |
|
31
|
|
|
|
|
32
|
|
|
return $response; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
protected function getResponseData(Response $response): array |
|
36
|
|
|
{ |
|
37
|
|
|
$statusCode = $response->getStatusCode(); |
|
38
|
|
|
$headers = $response->headers; |
|
39
|
|
|
|
|
40
|
|
|
if ($response instanceof BinaryFileResponse) { |
|
41
|
|
|
$content = $response->getFile()->getPathname(); |
|
42
|
|
|
$type = static::RESPONSE_TYPE_FILE; |
|
43
|
|
|
|
|
44
|
|
|
return compact('statusCode', 'headers', 'content', 'type'); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$content = $response->getContent(); |
|
48
|
|
|
$type = static::RESPONSE_TYPE_NORMAL; |
|
49
|
|
|
|
|
50
|
|
|
return compact('statusCode', 'headers', 'content', 'type'); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected function containsValidResponseProperties($properties): bool |
|
54
|
|
|
{ |
|
55
|
|
|
if (! is_array($properties)) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
if (! isset($properties['content'], $properties['statusCode'])) { |
|
60
|
|
|
return false; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return true; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
protected function buildResponse(array $responseProperties): Response |
|
67
|
|
|
{ |
|
68
|
|
|
$type = $responseProperties['type'] ?? static::RESPONSE_TYPE_NORMAL; |
|
69
|
|
|
|
|
70
|
|
|
if ($type === static::RESPONSE_TYPE_FILE) { |
|
71
|
|
|
return new BinaryFileResponse( |
|
72
|
|
|
$responseProperties['content'], |
|
73
|
|
|
$responseProperties['statusCode'] |
|
74
|
|
|
); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
return new IlluminateResponse($responseProperties['content'], $responseProperties['statusCode']); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|