-
-
Notifications
You must be signed in to change notification settings - Fork 918
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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( | ||
public readonly string $operationName, | ||
) { | ||
} | ||
} |
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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
{ | ||||||
/** | ||||||
* @param class-string $resourceClass | ||||||
*/ | ||||||
public function __construct( | ||||||
public readonly string $resourceClass, | ||||||
) { | ||||||
} | ||||||
} |
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 <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 ApiPlatform\Metadata\OperationMutatorInterface; | ||||||||||||
use Psr\Container\ContainerInterface; | ||||||||||||
|
||||||||||||
final class OperationMutatorCollection implements ContainerInterface | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you comment on this class to explain what its used for? Also mark it |
||||||||||||
{ | ||||||||||||
private array $mutators; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
|
||||||||||||
public function addMutator(string $operationName, OperationMutatorInterface $mutator): void | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
{ | ||||||||||||
$this->mutators[$operationName][] = $mutator; | ||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this creates an array if it doesn't exist for key |
||||||||||||
} | ||||||||||||
|
||||||||||||
public function get(string $id): array | ||||||||||||
{ | ||||||||||||
return $this->mutators[$id] ?? []; | ||||||||||||
} | ||||||||||||
|
||||||||||||
public function has(string $id): bool | ||||||||||||
{ | ||||||||||||
return isset($this->mutators[$id]); | ||||||||||||
} | ||||||||||||
} |
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 <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 ApiPlatform\Metadata\ResourceMutatorInterface; | ||
use Psr\Container\ContainerInterface; | ||
|
||
final class ResourceMutatorCollection implements ContainerInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should use the same class as for OperationMutatorCollection maybe just rename that |
||
{ | ||
private array $mutators; | ||
|
||
public function addMutator(string $resourceClass, ResourceMutatorInterface $mutator): void | ||
{ | ||
$this->mutators[$resourceClass][] = $mutator; | ||
} | ||
|
||
public function get(string $id): array | ||
{ | ||
return $this->mutators[$id] ?? []; | ||
} | ||
|
||
public function has(string $id): bool | ||
{ | ||
return isset($this->mutators[$id]); | ||
} | ||
} |
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 <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; | ||
|
||
interface OperationMutatorInterface | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. comment on the interface |
||
{ | ||
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 <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\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
|
||
private readonly ContainerInterface $resourceMutators, | ||
Check failure on line 31 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php
|
||
private readonly ContainerInterface $operationMutators, | ||
Check failure on line 32 in src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure container interface has any generic template, you can't phpdoc like you did or we need a new interface that inherits the container interface (which is alright) |
||
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); | ||
} | ||
|
||
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); | ||
} | ||
|
||
$newOperations->add($key, $operation); | ||
} | ||
|
||
return $newOperations; | ||
} | ||
} |
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 <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; | ||
|
||
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 <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\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
|
||
{ | ||
$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
|
||
|
||
$resourceMutatorCollection = new ResourceMutatorCollection(); | ||
$resourceMutatorCollection->addMutator($resourceClass, new DummyResourceMutator()); | ||
Check warning on line 39 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory($resourceMutatorCollection, new OperationMutatorCollection(), $decorated); | ||
Check warning on line 41 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn( | ||
$resourceMetadataCollection, | ||
); | ||
Check warning on line 45 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$resourceMetadataCollection = $customResourceMetadataCollectionFactory->create($resourceClass); | ||
Check warning on line 47 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$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
|
||
} | ||
|
||
public function testMutateOperation(): void | ||
Check warning on line 54 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
{ | ||
$decorated = $this->createMock(ResourceMetadataCollectionFactoryInterface::class); | ||
$resourceClass = \stdClass::class; | ||
Check warning on line 57 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$operations = new Operations(); | ||
$operations->add('_api_Dummy_get', new HttpOperation()); | ||
Check warning on line 60 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$resourceMetadataCollection = new ResourceMetadataCollection($resourceClass); | ||
$resourceMetadataCollection[] = (new ApiResource())->withClass($resourceClass)->withOperations($operations); | ||
Check warning on line 63 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$operationMutatorCollection = new OperationMutatorCollection(); | ||
$operationMutatorCollection->addMutator('_api_Dummy_get', new DummyOperationMutator()); | ||
Check warning on line 66 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$customResourceMetadataCollectionFactory = new MutatorResourceMetadataCollectionFactory(new ResourceMutatorCollection(), $operationMutatorCollection, $decorated); | ||
Check warning on line 68 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$decorated->expects($this->once())->method('create')->with($resourceClass)->willReturn( | ||
$resourceMetadataCollection, | ||
); | ||
Check warning on line 72 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$resourceMetadataCollection = $customResourceMetadataCollectionFactory->create($resourceClass); | ||
Check warning on line 74 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
|
||
$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
|
||
} | ||
} | ||
|
||
final class DummyResourceMutator implements ResourceMutatorInterface | ||
{ | ||
public function __invoke(ApiResource $resource): ApiResource | ||
Check warning on line 84 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
{ | ||
return $resource->withShortName('dummy'); | ||
Check warning on line 86 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
} | ||
} | ||
|
||
final class DummyOperationMutator implements OperationMutatorInterface | ||
{ | ||
public function __invoke(Operation $operation): Operation | ||
Check warning on line 92 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
{ | ||
return $operation->withShortName('custom_dummy'); | ||
Check warning on line 94 in src/Metadata/Tests/Resource/Factory/MutatorResourceMetadataCollectionFactoryTest.php
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should create only one pass |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.