-
Notifications
You must be signed in to change notification settings - Fork 708
/
ViewResponseListener.php
211 lines (173 loc) · 6.92 KB
/
ViewResponseListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\EventListener;
use Doctrine\Common\Annotations\Reader;
use Doctrine\Persistence\Proxy;
use FOS\RestBundle\Controller\Annotations\View as ViewAnnotation;
use FOS\RestBundle\FOSRestBundle;
use FOS\RestBundle\View\View;
use FOS\RestBundle\View\ViewHandlerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ViewEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* The ViewResponseListener class handles the kernel.view event and creates a {@see Response} for a {@see View} provided by the controller result.
*
* @author Lukas Kahwe Smith <smith@pooteeweet.org>
*
* @internal
*/
class ViewResponseListener implements EventSubscriberInterface
{
/**
* @var ViewHandlerInterface
*/
private $viewHandler;
private $forceView;
/**
* @var Reader|null
*/
private $annotationReader;
public function __construct(ViewHandlerInterface $viewHandler, bool $forceView, ?Reader $annotationReader = null)
{
$this->viewHandler = $viewHandler;
$this->forceView = $forceView;
$this->annotationReader = $annotationReader;
}
/**
* Extracts configuration for a {@see ViewAnnotation} from the controller if present.
*/
public function onKernelController(ControllerEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
return;
}
$controller = $event->getController();
if (!\is_array($controller) && method_exists($controller, '__invoke')) {
$controller = [$controller, '__invoke'];
}
if (!\is_array($controller)) {
return;
}
$className = $this->getRealClass(\get_class($controller[0]));
$object = new \ReflectionClass($className);
$method = $object->getMethod($controller[1]);
/** @var ViewAnnotation|null $classConfiguration */
$classConfiguration = null;
/** @var ViewAnnotation|null $methodConfiguration */
$methodConfiguration = null;
if (null !== $this->annotationReader) {
$classConfiguration = $this->getViewConfiguration($this->annotationReader->getClassAnnotations($object));
$methodConfiguration = $this->getViewConfiguration($this->annotationReader->getMethodAnnotations($method));
}
if (80000 <= \PHP_VERSION_ID) {
if (null === $classConfiguration) {
$classAttributes = array_map(
function (\ReflectionAttribute $attribute) {
return $attribute->newInstance();
},
$object->getAttributes(ViewAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF)
);
$classConfiguration = $this->getViewConfiguration($classAttributes);
}
if (null === $methodConfiguration) {
$methodAttributes = array_map(
function (\ReflectionAttribute $attribute) {
return $attribute->newInstance();
},
$method->getAttributes(ViewAnnotation::class, \ReflectionAttribute::IS_INSTANCEOF)
);
$methodConfiguration = $this->getViewConfiguration($methodAttributes);
}
}
// An annotation/attribute on the method takes precedence over the class level
if (null !== $methodConfiguration) {
$request->attributes->set(FOSRestBundle::VIEW_ATTRIBUTE, $methodConfiguration);
} elseif (null !== $classConfiguration) {
$request->attributes->set(FOSRestBundle::VIEW_ATTRIBUTE, $classConfiguration);
}
}
public function onKernelView(ViewEvent $event): void
{
$request = $event->getRequest();
if (!$request->attributes->get(FOSRestBundle::ZONE_ATTRIBUTE, true)) {
return;
}
/** @var ViewAnnotation|null $configuration */
$configuration = $request->attributes->get(FOSRestBundle::VIEW_ATTRIBUTE);
$view = $event->getControllerResult();
if (!$view instanceof View) {
if (!$configuration instanceof ViewAnnotation && !$this->forceView) {
return;
}
$view = new View($view);
}
if ($configuration instanceof ViewAnnotation) {
if (null !== $configuration->getStatusCode() && (null === $view->getStatusCode() || Response::HTTP_OK === $view->getStatusCode())) {
$view->setStatusCode($configuration->getStatusCode());
}
$context = $view->getContext();
if ($configuration->getSerializerGroups()) {
if (null === $context->getGroups()) {
$context->setGroups($configuration->getSerializerGroups());
} else {
$context->setGroups(array_merge($context->getGroups(), $configuration->getSerializerGroups()));
}
}
if (true === $configuration->getSerializerEnableMaxDepthChecks()) {
$context->enableMaxDepth();
} elseif (false === $configuration->getSerializerEnableMaxDepthChecks()) {
$context->disableMaxDepth();
}
}
if (null === $view->getFormat()) {
$view->setFormat($request->getRequestFormat());
}
$event->setResponse($this->viewHandler->handle($view, $request));
}
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => 'onKernelController',
KernelEvents::VIEW => ['onKernelView', 30],
];
}
/**
* @param object[] $annotations
*/
private function getViewConfiguration(array $annotations): ?ViewAnnotation
{
$viewAnnotation = null;
foreach ($annotations as $annotation) {
if (!$annotation instanceof ViewAnnotation) {
continue;
}
if (null === $viewAnnotation) {
$viewAnnotation = $annotation;
} else {
throw new \LogicException('Multiple "view" annotations are not allowed.');
}
}
return $viewAnnotation;
}
private function getRealClass(string $class): string
{
if (class_exists(Proxy::class)) {
if (false === $pos = strrpos($class, '\\'.Proxy::MARKER.'\\')) {
return $class;
}
return substr($class, $pos + Proxy::MARKER_LENGTH + 2);
}
return $class;
}
}