1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the API Platform project. |
5
|
|
|
* |
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\EventListener; |
15
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Exception\RuntimeException; |
17
|
|
|
use ApiPlatform\Core\Serializer\SerializerContextBuilderInterface; |
18
|
|
|
use ApiPlatform\Core\Util\RequestAttributesExtractor; |
19
|
|
|
use Symfony\Component\HttpFoundation\Request; |
20
|
|
|
use Symfony\Component\HttpFoundation\Response; |
21
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
22
|
|
|
use Symfony\Component\Serializer\Encoder\EncoderInterface; |
23
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Serializes data. |
27
|
|
|
* |
28
|
|
|
* @author Kévin Dunglas <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
final class SerializeListener |
31
|
|
|
{ |
32
|
|
|
private $serializer; |
33
|
|
|
private $serializerContextBuilder; |
34
|
|
|
|
35
|
|
|
public function __construct(SerializerInterface $serializer, SerializerContextBuilderInterface $serializerContextBuilder) |
36
|
|
|
{ |
37
|
|
|
$this->serializer = $serializer; |
38
|
|
|
$this->serializerContextBuilder = $serializerContextBuilder; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Serializes the data to the requested format. |
43
|
|
|
* |
44
|
|
|
* @param GetResponseForControllerResultEvent $event |
45
|
|
|
*/ |
46
|
|
|
public function onKernelView(GetResponseForControllerResultEvent $event) |
47
|
|
|
{ |
48
|
|
|
$controllerResult = $event->getControllerResult(); |
49
|
|
|
$request = $event->getRequest(); |
50
|
|
|
|
51
|
|
|
if ($controllerResult instanceof Response) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (!$attributes = RequestAttributesExtractor::extractAttributes($request)) { |
56
|
|
|
$this->serializeRawData($event, $request, $controllerResult); |
57
|
|
|
|
58
|
|
|
return; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
$context = $this->serializerContextBuilder->createFromRequest($request, true, $attributes); |
62
|
|
|
$resources = new class() extends \ArrayObject { |
63
|
|
|
public function serialize() |
64
|
|
|
{ |
65
|
|
|
} |
66
|
|
|
}; |
67
|
|
|
$context['resources'] = &$resources; |
68
|
|
|
|
69
|
|
|
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat(), $context)); |
70
|
|
|
|
71
|
|
|
$request->attributes->set('_api_respond', true); |
72
|
|
|
$request->attributes->set('_resources', $request->attributes->get('_resources', []) + (array) $resources); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tries to serialize data that are not API resources (e.g. the entrypoint or data returned by a custom controller). |
77
|
|
|
* |
78
|
|
|
* @param GetResponseForControllerResultEvent $event |
79
|
|
|
* @param Request $request |
80
|
|
|
* @param object $controllerResult |
81
|
|
|
* |
82
|
|
|
* @throws RuntimeException |
83
|
|
|
*/ |
84
|
|
|
private function serializeRawData(GetResponseForControllerResultEvent $event, Request $request, $controllerResult) |
85
|
|
|
{ |
86
|
|
|
if (!$request->attributes->get('_api_respond')) { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (\is_object($controllerResult)) { |
91
|
|
|
$event->setControllerResult($this->serializer->serialize($controllerResult, $request->getRequestFormat())); |
92
|
|
|
|
93
|
|
|
return; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
if (!$this->serializer instanceof EncoderInterface) { |
97
|
|
|
throw new RuntimeException(sprintf('The serializer instance must implements the "%s" interface.', EncoderInterface::class)); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
$event->setControllerResult($this->serializer->encode($controllerResult, $request->getRequestFormat())); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|