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 <dunglas@gmail.com>
*
* 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 <dunglas@gmail.com>
*
* 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
}
36 changes: 36 additions & 0 deletions src/Metadata/Mutator/ResourceMutatorCollection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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 Psr\Container\ContainerInterface;

final class ResourceMutatorCollection implements ContainerInterface
{
private array $mutators;

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

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

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L22

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

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

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L24

Added line #L24 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 32 in src/Metadata/Mutator/ResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L32

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

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

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceMutatorCollection.php#L34

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

namespace ApiPlatform\Metadata;

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

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Resource\ResourceMetadataCollection;
use ApiPlatform\Metadata\ResourceMutatorInterface;
use Psr\Container\ContainerInterface;

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

Check failure on line 26 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.
private readonly ContainerInterface $resourceMutators,

Check failure on line 27 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 ?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) {
foreach ($this->resourceMutators->get($resourceClass) as $mutator) {
$resource = $mutator($resource);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L43 was not covered by tests
}

$newMetadataCollection[] = $resource;
}

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

namespace ApiPlatform\Metadata;

interface ResourceMutatorInterface
{
public function __invoke(ApiResource $resource): ApiResource;
}
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/ApiPlatformBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
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\MutatorPass;
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 +63,6 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new TestMercureHubPass());
$container->addCompilerPass(new AuthenticatorManagerPass());
$container->addCompilerPass(new SerializerMappingLoaderPass());
$container->addCompilerPass(new MutatorPass());
}
}
28 changes: 28 additions & 0 deletions src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
use ApiPlatform\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface;
use ApiPlatform\Metadata\ApiResource;
use ApiPlatform\Metadata\AsOperationMutator;
use ApiPlatform\Metadata\AsResourceMutator;
use ApiPlatform\Metadata\FilterInterface;
use ApiPlatform\Metadata\OperationMutatorInterface;
use ApiPlatform\Metadata\ResourceMutatorInterface;
use ApiPlatform\Metadata\UriVariableTransformerInterface;
use ApiPlatform\Metadata\UrlGeneratorInterface;
use ApiPlatform\OpenApi\Model\Tag;
Expand Down Expand Up @@ -184,6 +188,29 @@
->addTag('api_platform.resource')
->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']);
});
$container->registerAttributeForAutoconfiguration(AsResourceMutator::class,
static function (ChildDefinition $definition, AsResourceMutator $attribute, \ReflectionClass $reflector): void {

Check failure on line 192 in src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Parameter #2 $configurator of method Symfony\Component\DependencyInjection\ContainerBuilder::registerAttributeForAutoconfiguration() expects callable(Symfony\Component\DependencyInjection\ChildDefinition, ApiPlatform\Metadata\AsResourceMutator, Reflector): void, Closure(Symfony\Component\DependencyInjection\ChildDefinition, ApiPlatform\Metadata\AsResourceMutator, ReflectionClass): void given.
if (!is_a($reflector->name, ResourceMutatorInterface::class, true)) {
throw new RuntimeException(sprintf('Resource mutator "%s" should implement %s', $reflector->name, ResourceMutatorInterface::class));

Check warning on line 194 in src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php#L193-L194

Added lines #L193 - L194 were not covered by tests
}

$definition->addTag('api_platform.resource_mutator', [
'resourceClass' => $attribute->resourceClass,
]);

Check warning on line 199 in src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php#L197-L199

Added lines #L197 - L199 were not covered by tests
},
);

$container->registerAttributeForAutoconfiguration(AsOperationMutator::class,
static function (ChildDefinition $definition, AsOperationMutator $attribute, \ReflectionClass $reflector): void {

Check failure on line 204 in src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

View workflow job for this annotation

GitHub Actions / PHPStan (PHP 8.4)

Parameter #2 $configurator of method Symfony\Component\DependencyInjection\ContainerBuilder::registerAttributeForAutoconfiguration() expects callable(Symfony\Component\DependencyInjection\ChildDefinition, ApiPlatform\Metadata\AsOperationMutator, Reflector): void, Closure(Symfony\Component\DependencyInjection\ChildDefinition, ApiPlatform\Metadata\AsOperationMutator, ReflectionClass): void given.
if (!is_a($reflector->name, OperationMutatorInterface::class, true)) {
throw new RuntimeException(sprintf('Operation mutator "%s" should implement %s', $reflector->name, OperationMutatorInterface::class));

Check warning on line 206 in src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php#L205-L206

Added lines #L205 - L206 were not covered by tests
}

$definition->addTag('api_platform.operation_mutator', [
'operationName' => $attribute->operationName,
]);

Check warning on line 211 in src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php#L209-L211

Added lines #L209 - L211 were not covered by tests
},
);

if (!$container->has('api_platform.state.item_provider')) {
$container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
Expand Down Expand Up @@ -341,6 +368,7 @@
$loader->load('metadata/property.xml');
$loader->load('metadata/resource.xml');
$loader->load('metadata/operation.xml');
$loader->load('metadata/mutator.xml');

$container->getDefinition('api_platform.metadata.resource_extractor.xml')->replaceArgument(0, $xmlResources);
$container->getDefinition('api_platform.metadata.property_extractor.xml')->replaceArgument(0, $xmlResources);
Expand Down
41 changes: 41 additions & 0 deletions src/Symfony/Bundle/DependencyInjection/Compiler/MutatorPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* 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\Symfony\Bundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class MutatorPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container): void
{
if (!$container->hasDefinition('api_platform.metadata.mutator_collection.resource')) {
return;

Check warning on line 25 in src/Symfony/Bundle/DependencyInjection/Compiler/MutatorPass.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Bundle/DependencyInjection/Compiler/MutatorPass.php#L25

Added line #L25 was not covered by tests
}

$definition = $container->getDefinition('api_platform.metadata.mutator_collection.resource');

$mutators = $container->findTaggedServiceIds('api_platform.resource_mutator');

foreach ($mutators as $id => $tags) {
foreach ($tags as $tag) {
$definition->addMethodCall('addMutator', [
$tag['resourceClass'],
new Reference($id),
]);

Check warning on line 37 in src/Symfony/Bundle/DependencyInjection/Compiler/MutatorPass.php

View check run for this annotation

Codecov / codecov/patch

src/Symfony/Bundle/DependencyInjection/Compiler/MutatorPass.php#L33-L37

Added lines #L33 - L37 were not covered by tests
}
}
}
}
10 changes: 10 additions & 0 deletions src/Symfony/Bundle/Resources/config/metadata/mutator.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="api_platform.metadata.mutator_collection.resource" class="ApiPlatform\Metadata\Mutator\ResourceMutatorCollection" public="false" />
</services>
</container>
5 changes: 5 additions & 0 deletions src/Symfony/Bundle/Resources/config/metadata/resource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
<argument type="service" id="service_container" on-invalid="null" />
</service>

<service id="api_platform.metadata.resource.metadata_collection_factory.mutator" class="ApiPlatform\Metadata\Resource\Factory\MutatorResourceMetadataCollectionFactory" decorates="api_platform.metadata.resource.metadata_collection_factory" decoration-priority="800" public="false" >
<argument type="service" id="api_platform.metadata.mutator_collection.resource" />
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory.mutator.inner" />
</service>

<service id="api_platform.metadata.resource.metadata_collection_factory.concerns" class="ApiPlatform\Metadata\Resource\Factory\ConcernsResourceMetadataCollectionFactory" decorates="api_platform.metadata.resource.metadata_collection_factory" decoration-priority="800" public="false">
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory.concerns.inner" />
</service>
Expand Down
2 changes: 2 additions & 0 deletions tests/Symfony/Bundle/ApiPlatformBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
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\MutatorPass;
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 @@ -58,6 +59,7 @@ public function testBuild(): void
$containerProphecy->addCompilerPass(Argument::type(TestMercureHubPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
$containerProphecy->addCompilerPass(Argument::type(AuthenticatorManagerPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
$containerProphecy->addCompilerPass(Argument::type(SerializerMappingLoaderPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();
$containerProphecy->addCompilerPass(Argument::type(MutatorPass::class))->willReturn($containerProphecy->reveal())->shouldBeCalled();

$bundle = new ApiPlatformBundle();
$bundle->build($containerProphecy->reveal());
Expand Down
Loading