Skip to content

Commit 61fb8f6

Browse files
committed
Merge mutator pass & new mutator collection interfaces
1 parent 1371f1c commit 61fb8f6

File tree

11 files changed

+107
-71
lines changed

11 files changed

+107
-71
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Mutator;
15+
16+
use ApiPlatform\Metadata\OperationMutatorInterface;
17+
use Psr\Container\ContainerInterface;
18+
19+
/**
20+
* @template T of OperationMutatorInterface
21+
*
22+
* @extends ContainerInterface<T>
23+
*/
24+
25+
interface OperationMutatorCollectionInterface extends ContainerInterface
26+
{
27+
public function get(string $id): array;
28+
}

src/Metadata/Mutator/OperationMutatorCollection.php renamed to src/Metadata/Mutator/OperationResourceMutatorCollection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
namespace ApiPlatform\Metadata\Mutator;
1515

1616
use ApiPlatform\Metadata\OperationMutatorInterface;
17-
use Psr\Container\ContainerInterface;
1817

19-
final class OperationMutatorCollection implements ContainerInterface
18+
/**
19+
* @internal
20+
*/
21+
final class OperationResourceMutatorCollection implements OperationMutatorCollectionInterface
2022
{
2123
private array $mutators = [];
2224

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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\Mutator;
15+
16+
use ApiPlatform\Metadata\ResourceMutatorInterface;
17+
use Psr\Container\ContainerInterface;
18+
19+
/**
20+
* @template T of ResourceMutatorInterface
21+
*
22+
* @extends ContainerInterface<T>
23+
*/
24+
25+
interface ResourceMutatorCollectionInterface extends ContainerInterface
26+
{
27+
public function get(string $id): array;
28+
}

src/Metadata/Mutator/ResourceMutatorCollection.php renamed to src/Metadata/Mutator/ResourceResourceMutatorCollection.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
namespace ApiPlatform\Metadata\Mutator;
1515

1616
use ApiPlatform\Metadata\ResourceMutatorInterface;
17-
use Psr\Container\ContainerInterface;
1817

19-
final class ResourceMutatorCollection implements ContainerInterface
18+
/**
19+
* @internal
20+
*/
21+
final class ResourceResourceMutatorCollection implements ResourceMutatorCollectionInterface
2022
{
2123
private array $mutators;
2224

src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,17 @@
1414
namespace ApiPlatform\Metadata\Resource\Factory;
1515

1616
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\Mutator\OperationMutatorCollectionInterface;
18+
use ApiPlatform\Metadata\Mutator\ResourceMutatorCollectionInterface;
1719
use ApiPlatform\Metadata\Operation;
18-
use ApiPlatform\Metadata\OperationMutatorInterface;
1920
use ApiPlatform\Metadata\Operations;
2021
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection;
21-
use ApiPlatform\Metadata\ResourceMutatorInterface;
22-
use Psr\Container\ContainerInterface;
2322

2423
final class MutatorResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface
2524
{
26-
/**
27-
* @param ContainerInterface<ResourceMutatorInterface[]> $resourceMutators
28-
* @param ContainerInterface<OperationMutatorInterface[]> $operationMutators
29-
*/
3025
public function __construct(
31-
private readonly ContainerInterface $resourceMutators,
32-
private readonly ContainerInterface $operationMutators,
26+
private readonly ResourceMutatorCollectionInterface $resourceMutators,
27+
private readonly OperationMutatorCollectionInterface $operationMutators,
3328
private readonly ?ResourceMetadataCollectionFactoryInterface $decorated = null,
3429
) {
3530
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
use ApiPlatform\Metadata\ApiResource;
1717
use ApiPlatform\Metadata\HttpOperation;
18-
use ApiPlatform\Metadata\Mutator\OperationMutatorCollection;
19-
use ApiPlatform\Metadata\Mutator\ResourceMutatorCollection;
18+
use ApiPlatform\Metadata\Mutator\OperationResourceMutatorCollection;
19+
use ApiPlatform\Metadata\Mutator\ResourceResourceMutatorCollection;
2020
use ApiPlatform\Metadata\Operation;
2121
use ApiPlatform\Metadata\OperationMutatorInterface;
2222
use ApiPlatform\Metadata\Operations;
@@ -35,10 +35,10 @@ public function testMutateResource(): void
3535
$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
3636
$resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass);
3737

38-
$resourceMutatorCollection = new ResourceMutatorCollection();
38+
$resourceMutatorCollection = new ResourceResourceMutatorCollection();
3939
$resourceMutatorCollection->addMutator($resourceClass, new DummyResourceMutator());
4040

41-
$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory($resourceMutatorCollection, new OperationMutatorCollection(), $decorated);
41+
$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory($resourceMutatorCollection, new OperationResourceMutatorCollection(), $decorated);
4242

4343
$decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn(
4444
$resourceMetadataCollection,
@@ -62,10 +62,10 @@ public function testMutateOperation(): void
6262
$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass);
6363
$resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass)->withOperations($operations);
6464

65-
$operationMutatorCollection = new OperationMutatorCollection();
65+
$operationMutatorCollection = new OperationResourceMutatorCollection();
6666
$operationMutatorCollection->addMutator('_api_Dummy_get', new DummyOperationMutator());
6767

68-
$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory(new ResourceMutatorCollection(), $operationMutatorCollection, $decorated);
68+
$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory(new ResourceResourceMutatorCollection(), $operationMutatorCollection, $decorated);
6969

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

src/Symfony/Bundle/ApiPlatformBundle.php

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\GraphQlResolverPass;
2525
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\GraphQlTypePass;
2626
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
27-
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\OperationMutatorPass;
28-
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\ResourceMutatorPass;
27+
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MutatorPass;
2928
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\SerializerMappingLoaderPass;
3029
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
3130
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestMercureHubPass;
@@ -64,7 +63,6 @@ public function build(ContainerBuilder $container): void
6463
$container->addCompilerPass(new TestMercureHubPass());
6564
$container->addCompilerPass(new AuthenticatorManagerPass());
6665
$container->addCompilerPass(new SerializerMappingLoaderPass());
67-
$container->addCompilerPass(new ResourceMutatorPass());
68-
$container->addCompilerPass(new OperationMutatorPass());
66+
$container->addCompilerPass(new MutatorPass());
6967
}
7068
}

src/Symfony/Bundle/DependencyInjection/Compiler/OperationMutatorPass.php renamed to src/Symfony/Bundle/DependencyInjection/Compiler/MutatorPass.php

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,35 @@
1717
use Symfony\Component\DependencyInjection\ContainerBuilder;
1818
use Symfony\Component\DependencyInjection\Reference;
1919

20-
class OperationMutatorPass implements CompilerPassInterface
20+
final class MutatorPass implements CompilerPassInterface
2121
{
2222
public function process(ContainerBuilder $container): void
23+
{
24+
$this->processResourceMutators($container);
25+
$this->processOperationMutators($container);
26+
}
27+
28+
public function processResourceMutators(ContainerBuilder $container): void
29+
{
30+
if (!$container->hasDefinition('api_platform.metadata.mutator_collection.resource')) {
31+
return;
32+
}
33+
34+
$definition = $container->getDefinition('api_platform.metadata.mutator_collection.resource');
35+
36+
$mutators = $container->findTaggedServiceIds('api_platform.resource_mutator');
37+
38+
foreach ($mutators as $id => $tags) {
39+
foreach ($tags as $tag) {
40+
$definition->addMethodCall('add', [
41+
$tag['resourceClass'],
42+
new Reference($id),
43+
]);
44+
}
45+
}
46+
}
47+
48+
private function processOperationMutators(ContainerBuilder $container): void
2349
{
2450
if (!$container->hasDefinition('api_platform.metadata.mutator_collection.operation')) {
2551
return;

src/Symfony/Bundle/DependencyInjection/Compiler/ResourceMutatorPass.php

Lines changed: 0 additions & 41 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
66

77
<services>
8-
<service id="api_platform.metadata.mutator_collection.resource" class="ApiPlatform\Metadata\Mutator\ResourceMutatorCollection" public="false" />
8+
<service id="api_platform.metadata.mutator_collection.resource" class="ResourceResourceMutatorCollection" public="false" />
99

10-
<service id="api_platform.metadata.mutator_collection.operation" class="ApiPlatform\Metadata\Mutator\OperationMutatorCollection" public="false" />
10+
<service id="api_platform.metadata.mutator_collection.operation" class="OperationResourceMutatorCollection" public="false" />
1111
</services>
1212
</container>

0 commit comments

Comments
 (0)