-
Notifications
You must be signed in to change notification settings - Fork 0
Ngstack 1017 schema processor #3
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: master
Are you sure you want to change the base?
Changes from all commits
3d4af0c
d04262b
40d98e8
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,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Netgen\ApiPlatformExtras\DependencyInjection\CompilerPass; | ||
|
|
||
| use Netgen\ApiPlatformExtras\OpenApi\Factory\OpenApiFactory; | ||
| use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument; | ||
| use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\DependencyInjection\Definition; | ||
| use Symfony\Component\DependencyInjection\Reference; | ||
|
|
||
| final class SchemaProcessorCompilerPass implements CompilerPassInterface | ||
| { | ||
| private const FEATURE_ENABLED_PARAMETER = 'netgen_api_platform_extras.features.schema_processor.enabled'; | ||
|
|
||
| public function process(ContainerBuilder $container): void | ||
| { | ||
| if ( | ||
| !$container->hasParameter(self::FEATURE_ENABLED_PARAMETER) | ||
| || $container->getParameter(self::FEATURE_ENABLED_PARAMETER) === false | ||
| ) { | ||
| return; | ||
| } | ||
|
|
||
| $container | ||
| ->setDefinition( | ||
| OpenApiFactory::class, | ||
| new Definition(OpenApiFactory::class), | ||
| ) | ||
| ->setArguments([ | ||
| new Reference('api_platform.openapi.factory.inner'), | ||
| new TaggedIteratorArgument( | ||
| tag: 'netgen_api_platform_extras.open_api_processor', | ||
| defaultPriorityMethod: 'getPriority', | ||
| ), | ||
| ]) | ||
| ->setDecoratedService('api_platform.openapi.factory', 'api_platform.openapi.factory.inner', -25); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,20 @@ | |
|
|
||
| namespace Netgen\ApiPlatformExtras; | ||
|
|
||
| use Netgen\ApiPlatformExtras\DependencyInjection\CompilerPass\SchemaProcessorCompilerPass; | ||
| use Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface; | ||
| use Symfony\Component\DependencyInjection\ContainerBuilder; | ||
| use Symfony\Component\HttpKernel\Bundle\Bundle; | ||
|
|
||
| final class NetgenApiPlatformExtrasBundle extends Bundle {} | ||
| final class NetgenApiPlatformExtrasBundle extends Bundle | ||
|
Member
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 will need to be rebased as it will conflict once we merge #2 . |
||
| { | ||
| public function build(ContainerBuilder $container): void | ||
| { | ||
| $container->addCompilerPass( | ||
| new SchemaProcessorCompilerPass(), | ||
| ); | ||
|
|
||
| $container->registerForAutoconfiguration(OpenApiProcessorInterface::class) | ||
| ->addTag('netgen_api_platform_extras.open_api_processor'); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Netgen\ApiPlatformExtras\OpenApi\Factory; | ||
|
|
||
| use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | ||
| use ApiPlatform\OpenApi\OpenApi; | ||
| use Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface; | ||
|
|
||
| final readonly class OpenApiFactory implements OpenApiFactoryInterface | ||
|
Member
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. Unnecessary readonly. |
||
| { | ||
| /** | ||
| * @param iterable<OpenApiProcessorInterface> $processors | ||
|
Member
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. FQCN missing. |
||
| */ | ||
| public function __construct( | ||
| private OpenApiFactoryInterface $decorated, | ||
| private iterable $processors, | ||
| ) {} | ||
|
|
||
| public function __invoke(array $context = []): OpenApi | ||
| { | ||
| $openApi = ($this->decorated)($context); | ||
|
|
||
| return $this->applyProcessors($openApi); | ||
| } | ||
|
|
||
| private function applyProcessors(OpenApi $openApi): OpenApi | ||
| { | ||
| foreach ($this->processors as $processor) { | ||
| $openApi = $processor->process($openApi); | ||
| } | ||
|
|
||
| return $openApi; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Netgen\ApiPlatformExtras\OpenApi\Processor; | ||
|
|
||
| use ApiPlatform\OpenApi\OpenApi; | ||
|
|
||
| interface OpenApiProcessorInterface | ||
| { | ||
| /** | ||
| * Used in compiler pass to set the tagged items service priority. | ||
| */ | ||
| public static function getPriority(): int; | ||
|
|
||
| /** | ||
| * Process the OpenAPI specification. | ||
| * Can modify schemas, paths, operations, or any other part of the spec. | ||
| */ | ||
| public function process(OpenApi $openApi): OpenApi; | ||
| } |
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.
This will be deprecated in Symfony 8.1, so better not to use it here. Instead, we should sort the processors list in the factory class.
Check here for more info symfony/symfony#62339
and here on how I solved in Layouts netgen-layouts/layouts-core@e17483b (it's for default_index_method, but the principle applies)