Skip to content

Commit cb16e36

Browse files
committed
Add Operation mutator
1 parent 7f165b9 commit cb16e36

File tree

6 files changed

+69
-6
lines changed

6 files changed

+69
-6
lines changed

src/Metadata/AsOperationMutator.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Metadata;
15+
16+
#[\Attribute(\Attribute::TARGET_CLASS)]
17+
final class AsOperationMutator
18+
{
19+
public function __construct(
20+
public readonly string $operationName,
21+
) {
22+
}
23+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ApiPlatform\Metadata;
4+
5+
interface OperationMutatorInterface
6+
{
7+
public function __invoke(Operation $operation): Operation;
8+
}

src/Metadata/Resource/Factory/CustomResourceMetadataCollectionFactory.php renamed to src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,15 @@
1414
namespace ApiPlatform\Metadata\Resource\Factory;
1515

1616
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
17+
use ApiPlatform\Metadata\ResourceMutatorInterface;
1718
use Psr\Container\ContainerInterface;
1819

19-
final class CustomResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
20+
final class MutatorResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
2021
{
22+
/**
23+
* @param ContainerInterface<ResourceMutatorInterface[]> $resourceMutators
24+
* @param ResourceMetadataCollectionFactoryInterface|null $decorated
25+
*/
2126
public function __construct(
2227
private readonly ContainerInterface $resourceMutators,
2328
private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
@@ -34,8 +39,8 @@ public function create(string $resourceClass): ResourceMetadataCollection
3439
$newMetadataCollection = new ResourceMetadataCollection($resourceClass);
3540

3641
foreach ($resourceMetadataCollection as $resource) {
37-
foreach ($this->resourceMutators->get($resourceClass) as $mutators) {
38-
$resource = $mutators($resource);
42+
foreach ($this->resourceMutators->get($resourceClass) as $mutator) {
43+
$resource = $mutator($resource);
3944
}
4045

4146
$newMetadataCollection[] = $resource;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace ApiPlatform\Metadata;
4+
5+
interface ResourceMutatorInterface
6+
{
7+
public function __invoke(ApiResource $resource): ApiResource;
8+
}

src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,11 @@
3131
use ApiPlatform\GraphQl\Resolver\QueryItemResolverInterface;
3232
use ApiPlatform\GraphQl\Type\Definition\TypeInterface as GraphQlTypeInterface;
3333
use ApiPlatform\Metadata\ApiResource;
34+
use ApiPlatform\Metadata\AsOperationMutator;
3435
use ApiPlatform\Metadata\AsResourceMutator;
3536
use ApiPlatform\Metadata\FilterInterface;
37+
use ApiPlatform\Metadata\OperationMutatorInterface;
38+
use ApiPlatform\Metadata\ResourceMutatorInterface;
3639
use ApiPlatform\Metadata\UriVariableTransformerInterface;
3740
use ApiPlatform\Metadata\UrlGeneratorInterface;
3841
use ApiPlatform\OpenApi\Model\Tag;
@@ -186,13 +189,29 @@ public function load(array $configs, ContainerBuilder $container): void
186189
->addTag('container.excluded', ['source' => 'by #[ApiResource] attribute']);
187190
});
188191
$container->registerAttributeForAutoconfiguration(AsResourceMutator::class,
189-
static function (ChildDefinition $definition, AsResourceMutator $attribute): void {
192+
static function (ChildDefinition $definition, AsResourceMutator $attribute, \ReflectionClass $reflector): void {
193+
if (!is_a($reflector->name, ResourceMutatorInterface::class, true)) {
194+
throw new RuntimeException(sprintf('Resource mutator "%s" should implement %s', $reflector->name, ResourceMutatorInterface::class));
195+
}
196+
190197
$definition->addTag('api_platform.resource_mutator', [
191198
'resourceClass' => $attribute->resourceClass,
192199
]);
193200
},
194201
);
195202

203+
$container->registerAttributeForAutoconfiguration(AsOperationMutator::class,
204+
static function (ChildDefinition $definition, AsOperationMutator $attribute, \ReflectionClass $reflector): void {
205+
if (!is_a($reflector->name, OperationMutatorInterface::class, true)) {
206+
throw new RuntimeException(sprintf('Operation mutator "%s" should implement %s', $reflector->name, OperationMutatorInterface::class));
207+
}
208+
209+
$definition->addTag('api_platform.operation_mutator', [
210+
'operationName' => $attribute->operationName,
211+
]);
212+
},
213+
);
214+
196215
if (!$container->has('api_platform.state.item_provider')) {
197216
$container->setAlias('api_platform.state.item_provider', 'api_platform.state_provider.object');
198217
}

src/Symfony/Bundle/Resources/config/metadata/resource.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
<argument type="service" id="service_container" on-invalid="null" />
3131
</service>
3232

33-
<service id="api_platform.metadata.resource.metadata_collection_factory.custom" class="ApiPlatform\Metadata\Resource\Factory\CustomResourceMetadataCollectionFactory" decorates="api_platform.metadata.resource.metadata_collection_factory" decoration-priority="800" public="false" >
33+
<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" >
3434
<argument type="service" id="api_platform.metadata.mutator_collection.resource" />
35-
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory.custom.inner" />
35+
<argument type="service" id="api_platform.metadata.resource.metadata_collection_factory.mutator.inner" />
3636
</service>
3737

3838
<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">

0 commit comments

Comments
 (0)