-
-
Notifications
You must be signed in to change notification settings - Fork 922
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
Open
loic425
wants to merge
4
commits into
api-platform:main
Choose a base branch
from
loic425:customize-resource-and-operations
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+521
−0
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
class AsOperationMutator | ||
{ | ||
public function __construct( | ||
public readonly string $operationName, | ||
) { | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
class AsResourceMutator | ||
{ | ||
/** | ||
* @param class-string $resourceClass | ||
*/ | ||
public function __construct( | ||
public readonly string $resourceClass, | ||
) { | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Metadata/Mutator/OperationMutatorCollectionInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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; | ||
|
||
/** | ||
* Collection of Operation mutators to mutate Operation metadata. | ||
*/ | ||
interface OperationMutatorCollectionInterface extends ContainerInterface | ||
{ | ||
/** | ||
* @return list<OperationMutatorInterface> | ||
*/ | ||
public function get(string $id): mixed; | ||
} |
42 changes: 42 additions & 0 deletions
42
src/Metadata/Mutator/OperationResourceMutatorCollection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class OperationResourceMutatorCollection implements OperationMutatorCollectionInterface | ||
{ | ||
private array $mutators = []; | ||
|
||
/** | ||
* Adds a mutator to the container for a given operation name. | ||
*/ | ||
public function add(string $operationName, OperationMutatorInterface $mutator): void | ||
{ | ||
$this->mutators[$operationName][] = $mutator; | ||
} | ||
|
||
public function get(string $id): array | ||
{ | ||
return $this->mutators[$id] ?? []; | ||
} | ||
|
||
public function has(string $id): bool | ||
{ | ||
return isset($this->mutators[$id]); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
src/Metadata/Mutator/ResourceMutatorCollectionInterface.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?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; | ||
|
||
/** | ||
* Collection of Resource mutators to mutate ApiResource metadata. | ||
*/ | ||
interface ResourceMutatorCollectionInterface extends ContainerInterface | ||
{ | ||
/** | ||
* @return list<ResourceMutatorInterface> | ||
*/ | ||
public function get(string $id): array; | ||
} |
39 changes: 39 additions & 0 deletions
39
src/Metadata/Mutator/ResourceResourceMutatorCollection.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?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; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
final class ResourceResourceMutatorCollection implements ResourceMutatorCollectionInterface | ||
{ | ||
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]); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
76 changes: 76 additions & 0 deletions
76
src/Metadata/Resource/Factory/MutatorResourceMetadataCollectionFactory.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
<?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\Mutator\OperationMutatorCollectionInterface; | ||
use ApiPlatform\Metadata\Mutator\ResourceMutatorCollectionInterface; | ||
use ApiPlatform\Metadata\Operation; | ||
use ApiPlatform\Metadata\Operations; | ||
use ApiPlatform\Metadata\Resource\ResourceMetadataCollection; | ||
|
||
final class MutatorResourceMetadataCollectionFactory implements ResourceMetadataCollectionFactoryInterface | ||
{ | ||
public function __construct( | ||
private readonly ResourceMutatorCollectionInterface $resourceMutators, | ||
private readonly OperationMutatorCollectionInterface $operationMutators, | ||
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; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.