Skip to content

Commit 42d61d3

Browse files
committed
misc fixes and optimization
1 parent 49ea482 commit 42d61d3

File tree

3 files changed

+20
-21
lines changed

3 files changed

+20
-21
lines changed

src/State/Processor/RespondProcessor.php

+14-21
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
use ApiPlatform\Metadata\Operation\Factory\OperationMetadataFactoryInterface;
2424
use ApiPlatform\Metadata\Put;
2525
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
26-
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
2726
use ApiPlatform\Metadata\ResourceClassResolverInterface;
2827
use ApiPlatform\Metadata\UrlGeneratorInterface;
2928
use ApiPlatform\Metadata\Util\ClassInfoTrait;
@@ -94,11 +93,22 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
9493
}
9594

9695
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+
}
98109
$headers['Allow'] = implode(', ', $allowedMethods);
99110

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) {
102112
$headers['Accept-Post'] = implode(', ', array_merge(...array_values($outputFormats)));
103113
}
104114
}
@@ -165,21 +175,4 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
165175
$headers
166176
);
167177
}
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-
}
185178
}

src/Symfony/Bundle/Resources/config/symfony/events.xml

+1
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<argument type="service" id="api_platform.iri_converter" />
6262
<argument type="service" id="api_platform.resource_class_resolver" />
6363
<argument type="service" id="api_platform.metadata.operation.metadata_factory" />
64+
<argument type="service" id="api_platform.metadata.operation.metadata_collection_factory" />
6465
</service>
6566

6667
<service id="api_platform.state_processor.add_link_header" class="ApiPlatform\State\Processor\AddLinkHeaderProcessor" decorates="api_platform.state_processor.respond">

tests/Fixtures/TestBundle/Document/DummyGetPostDeleteOperation.php

+5
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,9 @@ public function setName(string $name): void
4949
{
5050
$this->name = $name;
5151
}
52+
53+
public function __toString(): string
54+
{
55+
return (string) $this->getId();
56+
}
5257
}

0 commit comments

Comments
 (0)