Skip to content

Commit 6619485

Browse files
committed
feat(state): fixes
1 parent dea6352 commit 6619485

File tree

5 files changed

+30
-21
lines changed

5 files changed

+30
-21
lines changed

src/Laravel/ApiPlatformProvider.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -558,15 +558,23 @@ public function register(): void
558558
});
559559

560560
$this->app->singleton(RespondProcessor::class, function () {
561-
return new AddLinkHeaderProcessor(new RespondProcessor(), new HttpHeaderSerializer());
561+
return new RespondProcessor();
562562
});
563563

564-
$this->app->singleton(RespondProcessor::class, function (Application $app) {
565-
return new LinkedDataPlatformProcessor(new RespondProcessor(), $app->make(ResourceClassResolverInterface::class), $app->make(ResourceMetadataCollectionFactoryInterface::class));
564+
$this->app->singleton(AddLinkHeaderProcessor::class, function (Application $app) {
565+
return new AddLinkHeaderProcessor($app->make(RespondProcessor::class), new HttpHeaderSerializer());
566+
});
567+
568+
$this->app->singleton(LinkedDataPlatformProcessor::class, function (Application $app) {
569+
return new LinkedDataPlatformProcessor(
570+
$app->make(AddLinkHeaderProcessor::class), // Original service
571+
$app->make(ResourceClassResolverInterface::class),
572+
$app->make(ResourceMetadataCollectionFactoryInterface::class)
573+
);
566574
});
567575

568576
$this->app->singleton(SerializeProcessor::class, function (Application $app) {
569-
return new SerializeProcessor($app->make(RespondProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class));
577+
return new SerializeProcessor($app->make(LinkedDataPlatformProcessor::class), $app->make(Serializer::class), $app->make(SerializerContextBuilderInterface::class));
570578
});
571579

572580
$this->app->singleton(WriteProcessor::class, function (Application $app) {

src/State/Processor/LinkedDataPlatformProcessor.php

+12-13
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
*/
3030
final class LinkedDataPlatformProcessor implements ProcessorInterface
3131
{
32-
private const DEFAULT_ALLOWED_METHOD = ['OPTIONS', 'HEAD'];
32+
private const DEFAULT_ALLOWED_METHODS = ['OPTIONS', 'HEAD'];
3333

3434
/**
3535
* @param ProcessorInterface<T1, T2> $decorated
3636
*/
3737
public function __construct(
38-
private readonly ProcessorInterface $decorated, // todo is processor interface nullable
38+
private readonly ProcessorInterface $decorated,
3939
private readonly ?ResourceClassResolverInterface $resourceClassResolver = null,
40-
private readonly ?ResourceMetadataCollectionFactoryInterface $resourceCollectionMetadataFactory = null,
40+
private readonly ?ResourceMetadataCollectionFactoryInterface $resourceMetadataCollectionFactory = null,
4141
) {
4242
}
4343

@@ -48,22 +48,21 @@ public function process(mixed $data, Operation $operation, array $uriVariables =
4848
!$response instanceof Response
4949
|| !$operation instanceof HttpOperation
5050
|| $operation instanceof Error
51-
|| null === $this->resourceCollectionMetadataFactory
51+
|| !$this->resourceMetadataCollectionFactory
5252
|| !($context['resource_class'] ?? null)
53-
|| null === $operation->getUriTemplate()
53+
|| !$operation->getUriTemplate()
5454
|| !$this->resourceClassResolver?->isResourceClass($context['resource_class'])
5555
) {
5656
return $response;
5757
}
5858

59-
$allowedMethods = self::DEFAULT_ALLOWED_METHOD;
60-
$resourceMetadataCollection = $this->resourceCollectionMetadataFactory->create($context['resource_class']);
61-
foreach ($resourceMetadataCollection as $resource) {
62-
foreach ($resource->getOperations() as $resourceOperation) {
63-
if ($resourceOperation->getUriTemplate() === $operation->getUriTemplate()) {
64-
$operationMethod = $resourceOperation->getMethod();
65-
$allowedMethods[] = $operationMethod;
66-
if ('POST' === $operationMethod && \is_array($outputFormats = $operation->getOutputFormats())) {
59+
$allowedMethods = self::DEFAULT_ALLOWED_METHODS;
60+
$resourceCollection = $this->resourceMetadataCollectionFactory->create($context['resource_class']);
61+
foreach ($resourceCollection as $resource) {
62+
foreach ($resource->getOperations() as $op) {
63+
if ($op->getUriTemplate() === $operation->getUriTemplate()) {
64+
$allowedMethods[] = $method = $op->getMethod();
65+
if ('POST' === $method && \is_array($outputFormats = $op->getOutputFormats())) {
6766
$response->headers->set('Accept-Post', implode(', ', array_merge(...array_values($outputFormats))));
6867
}
6968
}

src/State/Tests/Processor/LinkedDataPlatformProcessorTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function setUp(): void
5353
new ApiResource(operations: [
5454
new Get(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', name: 'get'),
5555
new GetCollection(uriTemplate: '/dummy_resources{._format}', name: 'get_collections'),
56-
new Post(uriTemplate: '/dummy_resources{._format}', name: 'post'),
56+
new Post(uriTemplate: '/dummy_resources{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']], name: 'post'),
5757
new Delete(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', name: 'delete'),
5858
new Put(uriTemplate: '/dummy_resources/{dummyResourceId}{._format}', name: 'put'),
5959
]),
@@ -66,7 +66,7 @@ protected function setUp(): void
6666

6767
public function testHeadersAcceptPostIsReturnWhenPostAllowed(): void
6868
{
69-
$operation = (new HttpOperation('GET', '/dummy_resources{._format}', outputFormats: ['jsonld' => ['application/ld+json'], 'text/turtle' => ['text/turtle']]));
69+
$operation = (new HttpOperation('GET', '/dummy_resources{._format}'));
7070

7171
$context = $this->getContext();
7272

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

+2
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969

7070
<service id="api_platform.state_processor.linked_data_platform" class="ApiPlatform\State\Processor\LinkedDataPlatformProcessor" decorates="api_platform.state_processor.respond">
7171
<argument type="service" id="api_platform.state_processor.linked_data_platform.inner" />
72+
<argument type="service" id="api_platform.resource_class_resolver" />
73+
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory" />
7274
</service>
7375

7476
<service id="api_platform.listener.view.write" class="ApiPlatform\Symfony\EventListener\WriteListener">

tests/Functional/State/RespondProcessorTest.php tests/Functional/LinkDataPlatformTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
declare(strict_types=1);
1313

14-
namespace ApiPlatform\Tests\Functional\State;
14+
namespace ApiPlatform\Tests\Functional;
1515

1616
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
1717
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\DummyGetPostDeleteOperation;
1818
use ApiPlatform\Tests\SetupClassResourcesTrait;
1919

20-
class RespondProcessorTest extends ApiTestCase
20+
class LinkDataPlatformTest extends ApiTestCase
2121
{
2222
use SetupClassResourcesTrait;
2323

0 commit comments

Comments
 (0)