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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 <[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(

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 <[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(

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
}
28 changes: 28 additions & 0 deletions src/Metadata/Mutator/OperationMutatorCollectionInterface.php
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 src/Metadata/Mutator/OperationResourceMutatorCollection.php
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

Check warning on line 28 in src/Metadata/Mutator/OperationResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationResourceMutatorCollection.php#L28

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

Check warning on line 30 in src/Metadata/Mutator/OperationResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationResourceMutatorCollection.php#L30

Added line #L30 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 38 in src/Metadata/Mutator/OperationResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationResourceMutatorCollection.php#L38

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

Check warning on line 40 in src/Metadata/Mutator/OperationResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/OperationResourceMutatorCollection.php#L40

Added line #L40 was not covered by tests
}
}
28 changes: 28 additions & 0 deletions src/Metadata/Mutator/ResourceMutatorCollectionInterface.php
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 src/Metadata/Mutator/ResourceResourceMutatorCollection.php
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

Check warning on line 25 in src/Metadata/Mutator/ResourceResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceResourceMutatorCollection.php#L25

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

Check warning on line 27 in src/Metadata/Mutator/ResourceResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceResourceMutatorCollection.php#L27

Added line #L27 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 35 in src/Metadata/Mutator/ResourceResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceResourceMutatorCollection.php#L35

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

Check warning on line 37 in src/Metadata/Mutator/ResourceResourceMutatorCollection.php

View check run for this annotation

Codecov / codecov/patch

src/Metadata/Mutator/ResourceResourceMutatorCollection.php#L37

Added line #L37 was not covered by tests
}
}
19 changes: 19 additions & 0 deletions src/Metadata/OperationMutatorInterface.php
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;
}
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);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L55 was not covered by tests
}

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);

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

View check run for this annotation

Codecov / codecov/patch

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

Added line #L68 was not covered by tests
}

$newOperations->add($key, $operation);
}

return $newOperations;
}
}
19 changes: 19 additions & 0 deletions src/Metadata/ResourceMutatorInterface.php
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;
}
Loading
Loading