14
14
namespace ApiPlatform \Core \EventListener ;
15
15
16
16
use ApiPlatform \Core \Metadata \Resource \Factory \ResourceMetadataFactoryInterface ;
17
+ use ApiPlatform \Core \Metadata \Resource \ResourceMetadata ;
17
18
use ApiPlatform \Core \Util \RequestAttributesExtractor ;
18
19
use Symfony \Component \HttpFoundation \Response ;
19
20
use Symfony \Component \HttpKernel \Event \GetResponseForControllerResultEvent ;
@@ -51,7 +52,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
51
52
52
53
return ;
53
54
}
54
- if ($ controllerResult instanceof Response || !($ attributes ['respond ' ] ?? $ request ->attributes ->getBoolean ('_api_respond ' , false ))) {
55
+ if ($ controllerResult instanceof Response || !($ attributes ['respond ' ] ?? $ request ->attributes ->getBoolean ('_api_respond ' ))) {
55
56
return ;
56
57
}
57
58
@@ -78,6 +79,7 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
78
79
$ headers ['Sunset ' ] = (new \DateTimeImmutable ($ sunset ))->format (\DateTime::RFC1123 );
79
80
}
80
81
82
+ $ headers = $ this ->addAcceptPatchHeader ($ headers , $ attributes , $ resourceMetadata );
81
83
$ status = $ resourceMetadata ->getOperationAttribute ($ attributes , 'status ' );
82
84
}
83
85
@@ -87,4 +89,29 @@ public function onKernelView(GetResponseForControllerResultEvent $event): void
87
89
$ headers
88
90
));
89
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
+ }
90
117
}
0 commit comments