-
-
Notifications
You must be signed in to change notification settings - Fork 841
/
AddFormatListener.php
143 lines (123 loc) · 4.37 KB
/
AddFormatListener.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
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ApiPlatform\Core\EventListener;
use Negotiation\Negotiator;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Chooses the format to user according to the Accept header and supported formats.
*
* @author Kévin Dunglas <dunglas@gmail.com>
*/
final class AddFormatListener
{
private $negotiator;
private $formats;
private $mimeTypes;
public function __construct(Negotiator $negotiator, array $formats)
{
$this->negotiator = $negotiator;
$this->formats = $formats;
}
/**
* Sets the applicable format to the HttpFoundation Request.
*
* @param GetResponseEvent $event
*
* @throws NotFoundHttpException
* @throws NotAcceptableHttpException
*/
public function onKernelRequest(GetResponseEvent $event)
{
$request = $event->getRequest();
if (!$request->attributes->has('_api_resource_class') && !$request->attributes->has('_api_respond')) {
return;
}
$this->populateMimeTypes();
$this->addRequestFormats($request, $this->formats);
// Empty strings must be converted to null because the Symfony router doesn't support parameter typing before 3.2 (_format)
if (null === $routeFormat = $request->attributes->get('_format') ?: null) {
$mimeTypes = array_keys($this->mimeTypes);
} elseif (!isset($this->formats[$routeFormat])) {
throw new NotFoundHttpException('Not Found');
} else {
$mimeTypes = Request::getMimeTypes($routeFormat);
}
// First, try to guess the format from the Accept header
$accept = $request->headers->get('Accept');
if (null !== $accept) {
if (null === $acceptHeader = $this->negotiator->getBest($accept, $mimeTypes)) {
throw $this->getNotAcceptableHttpException($accept, $mimeTypes);
}
$request->setRequestFormat($request->getFormat($acceptHeader->getType()));
return;
}
// Then use the Symfony request format if available and applicable
$requestFormat = $request->getRequestFormat(null) ?: null;
if (null !== $requestFormat) {
$mimeType = $request->getMimeType($requestFormat);
if (isset($this->mimeTypes[$mimeType])) {
return;
}
throw $this->getNotAcceptableHttpException($mimeType);
}
// Finally, if no Accept header nor Symfony request format is set, return the default format
reset($this->formats);
$request->setRequestFormat(each($this->formats)['key']);
}
/**
* Adds API formats to the HttpFoundation Request.
*
* @param Request $request
* @param array $formats
*/
private function addRequestFormats(Request $request, array $formats)
{
foreach ($formats as $format => $mimeTypes) {
$request->setFormat($format, $mimeTypes);
}
}
/**
* Populates the $mimeTypes property.
*/
private function populateMimeTypes()
{
if (null !== $this->mimeTypes) {
return;
}
$this->mimeTypes = [];
foreach ($this->formats as $format => $mimeTypes) {
foreach ($mimeTypes as $mimeType) {
$this->mimeTypes[$mimeType] = $format;
}
}
}
/**
* Retrieves an instance of NotAcceptableHttpException.
*
* @param string $accept
* @param string[]|null $mimeTypes
*
* @return NotAcceptableHttpException
*/
private function getNotAcceptableHttpException(string $accept, array $mimeTypes = null) : NotAcceptableHttpException
{
if (null === $mimeTypes) {
$mimeTypes = array_keys($this->mimeTypes);
}
return new NotAcceptableHttpException(sprintf(
'Requested format "%s" is not supported. Supported MIME types are "%s".',
$accept,
implode('", "', $mimeTypes)
));
}
}