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\Metadata\Resource\Factory\ResourceMetadataFactoryInterface; |
17
|
|
|
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata; |
18
|
|
|
use ApiPlatform\Core\Util\RequestAttributesExtractor; |
19
|
|
|
use Symfony\Component\HttpFoundation\Response; |
20
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Builds the response object. |
24
|
|
|
* |
25
|
|
|
* @author Kévin Dunglas <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class RespondListener |
28
|
|
|
{ |
29
|
|
|
public const METHOD_TO_CODE = [ |
30
|
|
|
'POST' => Response::HTTP_CREATED, |
31
|
|
|
'DELETE' => Response::HTTP_NO_CONTENT, |
32
|
|
|
]; |
33
|
|
|
|
34
|
|
|
private $resourceMetadataFactory; |
35
|
|
|
|
36
|
|
|
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory = null) |
37
|
|
|
{ |
38
|
|
|
$this->resourceMetadataFactory = $resourceMetadataFactory; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Creates a Response to send to the client according to the requested format. |
43
|
|
|
*/ |
44
|
|
|
public function onKernelView(GetResponseForControllerResultEvent $event): void |
45
|
|
|
{ |
46
|
|
|
$controllerResult = $event->getControllerResult(); |
47
|
|
|
$request = $event->getRequest(); |
48
|
|
|
|
49
|
|
|
$attributes = RequestAttributesExtractor::extractAttributes($request); |
50
|
|
|
if ($controllerResult instanceof Response && ($attributes['respond'] ?? false)) { |
51
|
|
|
$event->setResponse($controllerResult); |
52
|
|
|
|
53
|
|
|
return; |
54
|
|
|
} |
55
|
|
|
if ($controllerResult instanceof Response || !($attributes['respond'] ?? $request->attributes->getBoolean('_api_respond'))) { |
56
|
|
|
return; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$headers = [ |
60
|
|
|
'Content-Type' => sprintf('%s; charset=utf-8', $request->getMimeType($request->getRequestFormat())), |
61
|
|
|
'Vary' => 'Accept', |
62
|
|
|
'X-Content-Type-Options' => 'nosniff', |
63
|
|
|
'X-Frame-Options' => 'deny', |
64
|
|
|
]; |
65
|
|
|
|
66
|
|
|
if ($request->attributes->has('_api_write_item_iri')) { |
67
|
|
|
$headers['Content-Location'] = $request->attributes->get('_api_write_item_iri'); |
68
|
|
|
|
69
|
|
|
if ($request->isMethod('POST')) { |
70
|
|
|
$headers['Location'] = $request->attributes->get('_api_write_item_iri'); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$status = null; |
75
|
|
|
if ($this->resourceMetadataFactory && $attributes) { |
|
|
|
|
76
|
|
|
$resourceMetadata = $this->resourceMetadataFactory->create($attributes['resource_class']); |
77
|
|
|
|
78
|
|
|
if ($sunset = $resourceMetadata->getOperationAttribute($attributes, 'sunset', null, true)) { |
79
|
|
|
$headers['Sunset'] = (new \DateTimeImmutable($sunset))->format(\DateTime::RFC1123); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$headers = $this->addAcceptPatchHeader($headers, $attributes, $resourceMetadata); |
83
|
|
|
$status = $resourceMetadata->getOperationAttribute($attributes, 'status'); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$event->setResponse(new Response( |
87
|
|
|
$controllerResult, |
88
|
|
|
$status ?? self::METHOD_TO_CODE[$request->getMethod()] ?? Response::HTTP_OK, |
89
|
|
|
$headers |
90
|
|
|
)); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
private function addAcceptPatchHeader(array $headers, array $attributes, ResourceMetadata $resourceMetadata): array |
94
|
|
|
{ |
95
|
|
|
if (!isset($attributes['item_operation_name'])) { |
96
|
|
|
return $headers; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
$patchMimeTypes = []; |
100
|
|
|
foreach ($resourceMetadata->getItemOperations() as $operation) { |
101
|
|
|
if ('PATCH' !== ($operation['method'] ?? '') || !isset($operation['input_formats'])) { |
102
|
|
|
continue; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
foreach ($operation['input_formats'] as $mimeTypes) { |
106
|
|
|
foreach ($mimeTypes as $mimeType) { |
107
|
|
|
$patchMimeTypes[] = $mimeType; |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
$headers['Accept-Patch'] = implode(', ', $patchMimeTypes); |
111
|
|
|
|
112
|
|
|
return $headers; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $headers; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|