Repository navigation
feat(symfony): ApiOperation attribute on controllers #8628
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
soyuka
wants to merge
2
commits into
api-platform:main
Choose a base branch
from
soyuka:feat/controller-api-operation
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.
Open
Changes from all commits
Commits
Show all changes
2 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,25 @@ | ||
| <?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; | ||
|
|
||
| /** | ||
| * Runs an API Platform operation on a controller method bound to the application's own route. | ||
| */ | ||
| #[\Attribute(\Attribute::TARGET_METHOD)] | ||
| final class ApiOperation | ||
| { | ||
| public function __construct(public readonly HttpOperation $operation) | ||
| { | ||
| } | ||
| } |
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,70 @@ | ||
| <?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\State\Util; | ||
|
|
||
| use ApiPlatform\Metadata\HttpOperation; | ||
| use ApiPlatform\State\SerializerContextBuilderInterface; | ||
| use Symfony\Component\HttpFoundation\Request; | ||
|
|
||
| /** | ||
| * Resolves the stage flags left unset on an operation to their request-dependent defaults. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class OperationStageDefaults | ||
| { | ||
| private function __construct() | ||
| { | ||
| } | ||
|
|
||
| public static function forProvider(HttpOperation $operation, Request $request): HttpOperation | ||
| { | ||
| if (null === $operation->canValidate()) { | ||
| $operation = $operation->withValidate(!$request->isMethodSafe() && !$request->isMethod('DELETE')); | ||
| } | ||
|
|
||
| if (null === $operation->canRead()) { | ||
| $operation = $operation->withRead($operation->getUriVariables() || $request->isMethodSafe()); | ||
| } | ||
|
|
||
| if (null === $operation->canDeserialize()) { | ||
| $operation = $operation->withDeserialize(\in_array($operation->getMethod(), ['POST', 'PUT', 'PATCH'], true)); | ||
| } | ||
|
|
||
| $denormalizationContext = $operation->getDenormalizationContext() ?? []; | ||
| if ($operation->canDeserialize() && !isset($denormalizationContext[SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE])) { | ||
| $method = $operation->getMethod(); | ||
| $assignObjectToPopulate = 'POST' === $method | ||
| || 'PATCH' === $method | ||
| || ('PUT' === $method && !($operation->getExtraProperties()['standard_put'] ?? true)); | ||
|
|
||
| $operation = $operation->withDenormalizationContext($denormalizationContext + [SerializerContextBuilderInterface::ASSIGN_OBJECT_TO_POPULATE => $assignObjectToPopulate]); | ||
| } | ||
|
|
||
| return $operation; | ||
| } | ||
|
|
||
| public static function forProcessor(HttpOperation $operation, Request $request): HttpOperation | ||
| { | ||
| if (null === $operation->canWrite()) { | ||
| $operation = $operation->withWrite(!$request->isMethodSafe()); | ||
| } | ||
|
|
||
| if (null === $operation->canSerialize()) { | ||
| $operation = $operation->withSerialize(true); | ||
| } | ||
|
|
||
| return $operation; | ||
| } | ||
| } |
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
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
78 changes: 78 additions & 0 deletions
78
src/Symfony/Bundle/DependencyInjection/Compiler/ControllerApiOperationPass.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,78 @@ | ||
| <?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\Symfony\Bundle\DependencyInjection\Compiler; | ||
|
|
||
| use ApiPlatform\Metadata\ApiOperation; | ||
| use ApiPlatform\Metadata\HttpOperation; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; | ||
|
|
||
| /** | ||
| * Discovers the controller methods carrying {@see ApiOperation} and registers their resource classes. | ||
| * | ||
| * @internal | ||
| */ | ||
| final class ControllerApiOperationPass implements CompilerPassInterface | ||
| { | ||
| public function process(ContainerBuilder $container): void | ||
| { | ||
| $controllerOperations = []; | ||
| $resources = []; | ||
|
|
||
| foreach (array_keys($container->findTaggedServiceIds('controller.service_arguments')) as $id) { | ||
| $definition = $container->getDefinition($id); | ||
| if ($definition->isAbstract()) { | ||
| continue; | ||
| } | ||
|
|
||
| $class = $container->getParameterBag()->resolveValue($definition->getClass() ?? $id); | ||
| if (!\is_string($class) || !($reflectionClass = $container->getReflectionClass($class, false))) { | ||
| continue; | ||
| } | ||
|
|
||
| foreach ($reflectionClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) { | ||
| foreach ($method->getAttributes(ApiOperation::class) as $attribute) { | ||
| $controller = $reflectionClass->getName().'::'.$method->getName(); | ||
| $resourceClass = $this->getResourceClass($attribute->newInstance()->operation); | ||
|
|
||
| if (null === $resourceClass) { | ||
| throw new InvalidArgumentException(\sprintf('The "#[%s]" on "%s" must define a "class" or an "output": an "input" alone does not define a resource.', ApiOperation::class, $controller)); | ||
| } | ||
|
|
||
| $controllerOperations[$resourceClass][] = $controller; | ||
| $resources[$resourceClass] = true; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| $container->setParameter('api_platform.controller_operations', $controllerOperations); | ||
| $container->setParameter('api_platform.controller_operation_resources', $resources); | ||
| } | ||
|
|
||
| private function getResourceClass(HttpOperation $operation): ?string | ||
| { | ||
| foreach ([$operation->getClass(), $operation->getOutput()] as $candidate) { | ||
| if (\is_array($candidate)) { | ||
| $candidate = $candidate['class'] ?? null; | ||
| } | ||
|
|
||
| if (\is_string($candidate) && '' !== $candidate) { | ||
| return $candidate; | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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
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
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
27 changes: 27 additions & 0 deletions
27
src/Symfony/Bundle/Resources/config/symfony/controller_operation.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,27 @@ | ||
| <?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 Symfony\Component\DependencyInjection\Loader\Configurator; | ||
|
|
||
| use ApiPlatform\Symfony\EventListener\ControllerApiOperationListener; | ||
|
|
||
| return static function (ContainerConfigurator $container) { | ||
| $services = $container->services(); | ||
|
|
||
| $services->set('api_platform.listener.request.controller_api_operation', ControllerApiOperationListener::class) | ||
| ->args([ | ||
| service('api_platform.metadata.resource.metadata_collection_factory'), | ||
| '%api_platform.controller_operations%', | ||
| ]) | ||
| ->tag('kernel.event_listener', ['event' => 'kernel.request', 'method' => 'onKernelRequest', 'priority' => 30]); | ||
| }; |
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
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.