|
23 | 23 | use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
|
24 | 24 | use ApiPlatform\Metadata\Put;
|
25 | 25 | use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
|
26 |
| -use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; |
27 | 26 | use ApiPlatform\Metadata\ResourceClassResolverInterface;
|
28 | 27 | use ApiPlatform\Metadata\UrlGeneratorInterface;
|
29 | 28 | use ApiPlatform\Metadata\Util\ClassInfoTrait;
|
@@ -94,11 +93,22 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
|
94 | 93 | }
|
95 | 94 |
|
96 | 95 | if (!$exception) {
|
97 |
| - $allowedMethods = $this->getAllowedMethods($context['resource_class'] ?? null, $operation->getUriTemplate()); |
| 96 | + $isPostAllowed = false; |
| 97 | + $allowedMethods = self::DEFAULT_ALLOWED_METHOD; |
| 98 | + if (null !== ($context['resource_class'] ?? null) && null !== $this->resourceCollectionMetadataFactory && null !== ($currentUriTemplate = $operation->getUriTemplate()) && $this->resourceClassResolver?->isResourceClass($context['resource_class'])) { |
| 99 | + $resourceMetadataCollection = $this->resourceCollectionMetadataFactory->create($context['resource_class']); |
| 100 | + foreach ($resourceMetadataCollection as $resource) { |
| 101 | + foreach ($resource->getOperations() as $resourceOperation) { |
| 102 | + if ($resourceOperation->getUriTemplate() === $currentUriTemplate) { |
| 103 | + $allowedMethods[] = $operationMethod = $resourceOperation->getMethod(); |
| 104 | + $isPostAllowed = $isPostAllowed || ('POST' === $operationMethod); |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + } |
98 | 109 | $headers['Allow'] = implode(', ', $allowedMethods);
|
99 | 110 |
|
100 |
| - $outputFormats = $operation->getOutputFormats(); |
101 |
| - if (\is_array($outputFormats) && [] !== $outputFormats && \in_array('POST', $allowedMethods, true)) { |
| 111 | + if ($isPostAllowed && \is_array($outputFormats = ($outputFormats = $operation->getOutputFormats())) && [] !== $outputFormats) { |
102 | 112 | $headers['Accept-Post'] = implode(', ', array_merge(...array_values($outputFormats)));
|
103 | 113 | }
|
104 | 114 | }
|
@@ -165,21 +175,4 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
|
165 | 175 | $headers
|
166 | 176 | );
|
167 | 177 | }
|
168 |
| - |
169 |
| - private function getAllowedMethods(?string $resourceClass, ?string $currentUriTemplate): array |
170 |
| - { |
171 |
| - $allowedMethods = self::DEFAULT_ALLOWED_METHOD; |
172 |
| - if (null !== $currentUriTemplate && null !== $resourceClass && $this->resourceClassResolver->isResourceClass($resourceClass)) { |
173 |
| - $resourceMetadataCollection = $this->resourceCollectionMetadataFactory ? $this->resourceCollectionMetadataFactory->create($resourceClass) : new ResourceMetadataCollection($resourceClass); |
174 |
| - foreach ($resourceMetadataCollection as $resource) { |
175 |
| - foreach ($resource->getOperations() as $operation) { |
176 |
| - if ($operation->getUriTemplate() === $currentUriTemplate) { |
177 |
| - $allowedMethods[] = $operation->getMethod(); |
178 |
| - } |
179 |
| - } |
180 |
| - } |
181 |
| - } |
182 |
| - |
183 |
| - return array_unique($allowedMethods); |
184 |
| - } |
185 | 178 | }
|
0 commit comments