Skip to content

feat(metadata) Customize Resource & operations #7213

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions src/Metadata/AsOperationMutator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsOperationMutator
{
public function __construct(

Check warning on line 19 in src/Metadata/AsOperationMutator.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/AsOperationMutator.php#L19

Added line #L19 was not covered by tests
public readonly string $operationName,
) {
}

Check warning on line 22 in src/Metadata/AsOperationMutator.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/AsOperationMutator.php#L22

Added line #L22 was not covered by tests
}
26 changes: 26 additions & 0 deletions src/Metadata/AsResourceMutator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata;

#[\Attribute(\Attribute::TARGET_CLASS)]
final class AsResourceMutator
{
/**
* @param class-string $resourceClass
*/
public function __construct(

Check warning on line 22 in src/Metadata/AsResourceMutator.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/AsResourceMutator.php#L22

Added line #L22 was not covered by tests
public readonly string $resourceClass,
) {
}

Check warning on line 25 in src/Metadata/AsResourceMutator.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/AsResourceMutator.php#L25

Added line #L25 was not covered by tests
}
37 changes: 37 additions & 0 deletions src/Metadata/Mutator/OperationMutatorCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata\Mutator;

use ApiPlatform\Metadata\OperationMutatorInterface;
use Psr\Container\ContainerInterface;

final class OperationMutatorCollection implements ContainerInterface
{
private array $mutators;

public function addMutator(string $operationName, OperationMutatorInterface $mutator): void

Check warning on line 23 in src/Metadata/Mutator/OperationMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationMutatorCollection.php#L23

Added line #L23 was not covered by tests
{
$this->mutators[$operationName][] = $mutator;

Check warning on line 25 in src/Metadata/Mutator/OperationMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationMutatorCollection.php#L25

Added line #L25 was not covered by tests
}

public function get(string $id): array
{
return $this->mutators[$id] ?? [];
}

public function has(string $id): bool

Check warning on line 33 in src/Metadata/Mutator/OperationMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationMutatorCollection.php#L33

Added line #L33 was not covered by tests
{
return isset($this->mutators[$id]);

Check warning on line 35 in src/Metadata/Mutator/OperationMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationMutatorCollection.php#L35

Added line #L35 was not covered by tests
}
}
37 changes: 37 additions & 0 deletions src/Metadata/Mutator/ResourceMutatorCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata\Mutator;

use ApiPlatform\Metadata\ResourceMutatorInterface;
use Psr\Container\ContainerInterface;

final class ResourceMutatorCollection implements ContainerInterface
{
private array $mutators;

public function addMutator(string $resourceClass, ResourceMutatorInterface $mutator): void

Check warning on line 23 in src/Metadata/Mutator/ResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L23

Added line #L23 was not covered by tests
{
$this->mutators[$resourceClass][] = $mutator;

Check warning on line 25 in src/Metadata/Mutator/ResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L25

Added line #L25 was not covered by tests
}

public function get(string $id): array
{
return $this->mutators[$id] ?? [];
}

public function has(string $id): bool

Check warning on line 33 in src/Metadata/Mutator/ResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L33

Added line #L33 was not covered by tests
{
return isset($this->mutators[$id]);

Check warning on line 35 in src/Metadata/Mutator/ResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L35

Added line #L35 was not covered by tests
}
}
19 changes: 19 additions & 0 deletions src/Metadata/OperationMutatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata;

interface OperationMutatorInterface
{
public function __invoke(Operation $operation): Operation;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata\Resource\Factory;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\OperationMutatorInterface;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\ResourceMutatorInterface;
use Psr\Container\ContainerInterface;

final class MutatorResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
{
/**
* @param ContainerInterface<ResourceMutatorInterface[]> $resourceMutators
* @param ContainerInterface<OperationMutatorInterface[]> $operationMutators
*/
public function __construct(

Check failure on line 30 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

PHPDoc tag @param for parameter $resourceMutators contains generic type Psr\Container\ContainerInterface<array<ApiPlatform\Metadata\ResourceMutatorInterface>> but interface Psr\Container\ContainerInterface is not generic.

Check failure on line 30 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

PHPDoc tag @param for parameter $operationMutators contains generic type Psr\Container\ContainerInterface<array<ApiPlatform\Metadata\OperationMutatorInterface>> but interface Psr\Container\ContainerInterface is not generic.
private readonly ContainerInterface $resourceMutators,

Check failure on line 31 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

PHPDoc type for property ApiPlatform\Metadata\Resource\Factory\MutatorResourceMetadataCollectionFactory::$resourceMutators contains generic type Psr\Container\ContainerInterface<array<ApiPlatform\Metadata\ResourceMutatorInterface>> but interface Psr\Container\ContainerInterface is not generic.
private readonly ContainerInterface $operationMutators,

Check failure on line 32 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

PHPDoc type for property ApiPlatform\Metadata\Resource\Factory\MutatorResourceMetadataCollectionFactory::$operationMutators contains generic type Psr\Container\ContainerInterface<array<ApiPlatform\Metadata\OperationMutatorInterface>> but interface Psr\Container\ContainerInterface is not generic.
private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
) {
}

public function create(string $resourceClass): ResourceMetadataCollection
{
$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
if ($this->decorated) {
$resourceMetadataCollection = $this->decorated->create($resourceClass);
}

$newMetadataCollection = new ResourceMetadataCollection($resourceClass);

foreach ($resourceMetadataCollection as $resource) {
$resource = $this->mutateResource($resource, $resourceClass);
$operations = $this->mutateOperations($resource->getOperations() ?? new Operations());
$resource = $resource->withOperations($operations);

$newMetadataCollection[] = $resource;
}

return $newMetadataCollection;
}

private function mutateResource(ApiResource $resource, string $resourceClass): ApiResource
{
foreach ($this->resourceMutators->get($resourceClass) as $mutator) {
$resource = $mutator($resource);

Check warning on line 60 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php#L60

Added line #L60 was not covered by tests
}

return $resource;
}

private function mutateOperations(Operations $operations): Operations
{
$newOperations = new Operations();

/** @var Operation $operation */
foreach ($operations as $key => $operation) {
foreach ($this->operationMutators->get($key) as $mutator) {
$operation = $mutator($operation);

Check warning on line 73 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php#L73

Added line #L73 was not covered by tests
}

$newOperations->add($key, $operation);
}

return $newOperations;
}
}
19 changes: 19 additions & 0 deletions src/Metadata/ResourceMutatorInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata;

interface ResourceMutatorInterface
{
public function __invoke(ApiResource $resource): ApiResource;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Metadata\Tests\Resource\Factory;

use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\HttpOperation;
use ApiPlatform\Metadata\Mutator\OperationMutatorCollection;
use ApiPlatform\Metadata\Mutator\ResourceMutatorCollection;
use ApiPlatform\Metadata\Operation;
use ApiPlatform\Metadata\OperationMutatorInterface;
use ApiPlatform\Metadata\Operations;
use ApiPlatform\Metadata\Resource\Factory\MutatorResourceMetadataCollectionFactory;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\ResourceMutatorInterface;
use PHPUnit\Framework\TestCase;

final class MutatorResourceMetadataCollectionFactoryTest extends TestCase
{
public function testMutateResource(): void

Check warning on line 31 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L31

Added line #L31 was not covered by tests
{
$decorated = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$resourceClass = \stdClass::class;
$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
$resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass);

Check warning on line 36 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L33-L36

Added lines #L33 - L36 were not covered by tests

$resourceMutatorCollection = new ResourceMutatorCollection();
$resourceMutatorCollection->addMutator($resourceClass, new DummyResourceMutator());

Check warning on line 39 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L38-L39

Added lines #L38 - L39 were not covered by tests

$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory($resourceMutatorCollection, new OperationMutatorCollection(), $decorated);

Check warning on line 41 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L41

Added line #L41 was not covered by tests

$decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn(
$resourceMetadataCollection,
);

Check warning on line 45 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L43-L45

Added lines #L43 - L45 were not covered by tests

$resourceMetadataCollection = $customResourceMetadataCollectionFactory->create($resourceClass);

Check warning on line 47 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L47

Added line #L47 was not covered by tests

$resource = $resourceMetadataCollection->getIterator()->current();
$this->assertInstanceOf(ApiResource::class, $resource);
$this->assertSame('dummy', $resource->getShortName());

Check warning on line 51 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L49-L51

Added lines #L49 - L51 were not covered by tests
}

public function testMutateOperation(): void

Check warning on line 54 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L54

Added line #L54 was not covered by tests
{
$decorated = $this->createMock(ResourceMetadataCollectionFactoryInterface::class);
$resourceClass = \stdClass::class;

Check warning on line 57 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L56-L57

Added lines #L56 - L57 were not covered by tests

$operations = new Operations();
$operations->add('_api_Dummy_get', new HttpOperation());

Check warning on line 60 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L59-L60

Added lines #L59 - L60 were not covered by tests

$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
$resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass)->withOperations($operations);

Check warning on line 63 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L62-L63

Added lines #L62 - L63 were not covered by tests

$operationMutatorCollection = new OperationMutatorCollection();
$operationMutatorCollection->addMutator('_api_Dummy_get', new DummyOperationMutator());

Check warning on line 66 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L65-L66

Added lines #L65 - L66 were not covered by tests

$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory(new ResourceMutatorCollection(), $operationMutatorCollection, $decorated);

Check warning on line 68 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L68

Added line #L68 was not covered by tests

$decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn(
$resourceMetadataCollection,
);

Check warning on line 72 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L70-L72

Added lines #L70 - L72 were not covered by tests

$resourceMetadataCollection = $customResourceMetadataCollectionFactory->create($resourceClass);

Check warning on line 74 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L74

Added line #L74 was not covered by tests

$resource = $resourceMetadataCollection->getIterator()->current();
$this->assertInstanceOf(ApiResource::class, $resource);
$this->assertEquals('custom_dummy', $resourceMetadataCollection->getOperation('_api_Dummy_get')->getShortName());

Check warning on line 78 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L76-L78

Added lines #L76 - L78 were not covered by tests
}
}

final class DummyResourceMutator implements ResourceMutatorInterface
{
public function __invoke(ApiResource $resource): ApiResource

Check warning on line 84 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L84

Added line #L84 was not covered by tests
{
return $resource->withShortName('dummy');

Check warning on line 86 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L86

Added line #L86 was not covered by tests
}
}

final class DummyOperationMutator implements OperationMutatorInterface
{
public function __invoke(Operation $operation): Operation

Check warning on line 92 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L92

Added line #L92 was not covered by tests
{
return $operation->withShortName('custom_dummy');

Check warning on line 94 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php#L94

Added line #L94 was not covered by tests
}
}
4 changes: 4 additions & 0 deletions src/Symfony/Bundle/ApiPlatformBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\GraphQlResolverPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\OperationMutatorPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\ResourceMutatorPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\SerializerMappingLoaderPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestMercureHubPass;
Expand Down Expand Up @@ -62,5 +64,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new TestMercureHubPass());
$container->addCompilerPass(new AuthenticatorManagerPass());
$container->addCompilerPass(new SerializerMappingLoaderPass());
$container->addCompilerPass(new ResourceMutatorPass());
$container->addCompilerPass(new OperationMutatorPass());
}
}
Loading
Loading