1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\ResponseCache\Serializer; |
4
|
|
|
|
5
|
|
|
use Illuminate\Http\JsonResponse; |
6
|
|
|
use Symfony\Component\HttpFoundation\Response; |
7
|
|
|
|
8
|
|
|
class ExampleSerializer extends DefaultSerializer |
9
|
|
|
{ |
10
|
|
|
protected function getResponseData(Response $response): array |
11
|
|
|
{ |
12
|
|
|
$statusCode = $response->getStatusCode(); |
13
|
|
|
$headers = $response->headers; |
14
|
|
|
|
15
|
|
|
if ($response instanceof BinaryFileResponse) { |
|
|
|
|
16
|
|
|
$content = $response->getFile()->getPathname(); |
17
|
|
|
$type = self::RESPONSE_TYPE_FILE; |
18
|
|
|
|
19
|
|
|
return compact('statusCode', 'headers', 'content', 'type'); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
// If you return it with JsonResponse, save the content as JSON |
23
|
|
|
$content = $response instanceof JsonResponse |
24
|
|
|
? $response->getData() |
25
|
|
|
: $response->getContent(); |
26
|
|
|
|
27
|
|
|
$type = self::RESPONSE_TYPE_NORMAL; |
28
|
|
|
|
29
|
|
|
// Save class name |
30
|
|
|
$class = get_class($response); |
31
|
|
|
|
32
|
|
|
return compact('statusCode', 'headers', 'content', 'type', 'class'); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
protected function buildResponse(array $responseProperties): Response |
36
|
|
|
{ |
37
|
|
|
$type = $responseProperties['type'] ?? self::RESPONSE_TYPE_NORMAL; |
38
|
|
|
|
39
|
|
|
if ($type === self::RESPONSE_TYPE_FILE) { |
40
|
|
|
return new BinaryFileResponse( |
41
|
|
|
$responseProperties['content'], |
42
|
|
|
$responseProperties['statusCode'] |
43
|
|
|
); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
$class = $responseProperties['class']; |
47
|
|
|
|
48
|
|
|
// Restore as saved class |
49
|
|
|
return new $class($responseProperties['content'], $responseProperties['statusCode']); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
This error could be the result of:
1. Missing dependencies
PHP Analyzer uses your
composer.json
file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects thecomposer.json
to be in the root folder of your repository.Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the
require
orrequire-dev
section?2. Missing use statement
PHP does not complain about undefined classes in
ìnstanceof
checks. For example, the following PHP code will work perfectly fine:If you have not tested against this specific condition, such errors might go unnoticed.