1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Saikootau\ApiBundle\Event\Listener; |
6
|
|
|
|
7
|
|
|
use Saikootau\ApiBundle\MediaType\Exception\NonNegotiableMediaTypeException; |
8
|
|
|
use Saikootau\ApiBundle\MediaType\MediaTypeNegotiator; |
9
|
|
|
use Saikootau\ApiBundle\MediaType\MediaTypes; |
10
|
|
|
use Saikootau\ApiBundle\Serializer\Serializer; |
11
|
|
|
use Symfony\Component\HttpFoundation\Request; |
12
|
|
|
use Symfony\Component\HttpFoundation\Response; |
13
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForControllerResultEvent; |
14
|
|
|
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException; |
15
|
|
|
|
16
|
|
|
class ResponseListener |
17
|
|
|
{ |
18
|
|
|
private $defaultContentType; |
19
|
|
|
private $mediaTypeNegotiator; |
20
|
|
|
|
21
|
|
|
private static $noContentMethods = [ |
22
|
|
|
'HEAD', |
23
|
|
|
]; |
24
|
|
|
|
25
|
3 |
|
public function __construct(MediaTypeNegotiator $mediaTypeNegotiator, string $defaultContentType) |
26
|
|
|
{ |
27
|
3 |
|
$this->mediaTypeNegotiator = $mediaTypeNegotiator; |
28
|
3 |
|
$this->defaultContentType = $defaultContentType; |
29
|
3 |
|
} |
30
|
|
|
|
31
|
3 |
|
public function onKernelView(GetResponseForControllerResultEvent $event): void |
32
|
|
|
{ |
33
|
3 |
|
$controllerResult = $event->getControllerResult(); |
34
|
|
|
|
35
|
3 |
|
if ($controllerResult instanceof Response) { |
36
|
1 |
|
return; |
37
|
|
|
} |
38
|
|
|
|
39
|
2 |
|
$serializer = $this->getSerializer($event->getRequest()); |
40
|
1 |
|
$body = ''; |
41
|
1 |
|
if ($this->isContentAllowed($event->getRequest()->getMethod())) { |
42
|
1 |
|
$body = $serializer->serialize($controllerResult); |
43
|
|
|
} |
44
|
|
|
|
45
|
1 |
|
$event->setResponse( |
46
|
1 |
|
$this->createResponse( |
47
|
1 |
|
$body, |
48
|
1 |
|
Response::HTTP_OK, |
49
|
1 |
|
$serializer->getContentType() |
50
|
|
|
) |
51
|
|
|
); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Checks if the response should contain any content. |
56
|
|
|
* |
57
|
|
|
* @param string $method |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
1 |
|
private function isContentAllowed(string $method): bool |
62
|
|
|
{ |
63
|
1 |
|
return !in_array($method, self::$noContentMethods); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Create a response for the given data. |
68
|
|
|
* |
69
|
|
|
* @param string $body |
70
|
|
|
* @param int $status |
71
|
|
|
* @param string $contentType |
72
|
|
|
* |
73
|
|
|
* @return Response |
74
|
|
|
*/ |
75
|
1 |
|
private function createResponse(string $body, int $status, string $contentType): Response |
76
|
|
|
{ |
77
|
1 |
|
return new Response( |
78
|
1 |
|
$body, |
79
|
1 |
|
$status, |
80
|
1 |
|
['Content-Type' => $contentType] |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get a matching serializer for the given request's acceptable content types. |
86
|
|
|
* |
87
|
|
|
* @param Request $request |
88
|
|
|
* |
89
|
|
|
* @return Serializer |
90
|
|
|
* |
91
|
|
|
* @throws NotAcceptableHttpException |
92
|
|
|
*/ |
93
|
2 |
|
protected function getSerializer(Request $request): Serializer |
94
|
|
|
{ |
95
|
|
|
try { |
96
|
|
|
/** @var Serializer $serializer */ |
97
|
2 |
|
$serializer = $this->mediaTypeNegotiator->negotiate(...$this->getRequestAcceptableContentTypes($request)); |
98
|
1 |
|
} catch (NonNegotiableMediaTypeException $exception) { |
99
|
1 |
|
throw new NotAcceptableHttpException( |
100
|
1 |
|
sprintf( |
101
|
1 |
|
'Deliverable content types are: %s', |
102
|
1 |
|
implode(', ', $this->mediaTypeNegotiator->getSupportedMediaTypes()) |
103
|
|
|
) |
104
|
|
|
); |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return $serializer; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Returns an array of acceptable content types for the given request. |
112
|
|
|
* |
113
|
|
|
* @param Request $request |
114
|
|
|
* |
115
|
|
|
* @return string[] |
116
|
|
|
*/ |
117
|
2 |
|
private function getRequestAcceptableContentTypes(Request $request): array |
118
|
|
|
{ |
119
|
2 |
|
$acceptableContentTypes = $request->getAcceptableContentTypes(); |
120
|
|
|
|
121
|
2 |
|
if (false !== ($anyIndex = array_search(MediaTypes::TYPE_APPLICATION_ANY, $acceptableContentTypes))) { |
122
|
1 |
|
$acceptableContentTypes[$anyIndex] = $this->defaultContentType; |
123
|
|
|
} |
124
|
|
|
|
125
|
2 |
|
return $acceptableContentTypes; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|