-
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
Open
JakovKnezovicc
wants to merge
8
commits into
master
Choose a base branch
from
NGSTACK-1017-schema-processor
base: master
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
8 commits
Select commit
Hold shift + click to select a range
9218bd7
NGSTACK-1017 add openapi factory decorator and openapi processor inte…
JakovKnezovicc 93cb7bc
NGSTACK-1017 add compiler pass for schema processor feature
JakovKnezovicc f8206fe
NGSTACK-1017 add compiler pass to bundle build and autoconfigure open…
JakovKnezovicc aca61bb
NGSTACK-1017 remove readonly from class and use fqcn in doc
JakovKnezovicc 02247a1
NGSTACK-1017 remove default priority method in compiler pass definiti…
JakovKnezovicc 74892a0
NGSTACK-1017 fix phpstan issues regarding generic iterable typehint
JakovKnezovicc 27de09f
NGSTACK-1017 cs-fixer
JakovKnezovicc 9c0e3c1
NGSTACK-1017 remove setting processor tagged services as lazy
JakovKnezovicc 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
38 changes: 38 additions & 0 deletions
38
src/DependencyInjection/CompilerPass/SchemaProcessorCompilerPass.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,38 @@ | ||
| <?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('netgen_api_platform_extras.open_api_processor'), | ||
| ]) | ||
| ->setDecoratedService('api_platform.openapi.factory', 'api_platform.openapi.factory.inner', -25); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Netgen\ApiPlatformExtras\OpenApi\Factory; | ||
|
|
||
| use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; | ||
| use ApiPlatform\OpenApi\OpenApi; | ||
|
|
||
| use function iterator_to_array; | ||
| use function usort; | ||
|
|
||
| final class OpenApiFactory implements OpenApiFactoryInterface | ||
| { | ||
| /** | ||
| * @param iterable<\Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface> $processors | ||
| */ | ||
| public function __construct( | ||
| private OpenApiFactoryInterface $decorated, | ||
| private iterable $processors, | ||
| ) { | ||
| $this->processors = $this->sortProcessors($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; | ||
| } | ||
|
|
||
| /** | ||
| * @param iterable<\Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface> $processors | ||
| * | ||
| * @return \Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface[] | ||
| */ | ||
| private function sortProcessors(iterable $processors): array | ||
| { | ||
| $processors = iterator_to_array($processors); | ||
|
|
||
| usort( | ||
| $processors, | ||
| static fn ($a, $b): int => $b::getPriority() <=> $a::getPriority(), | ||
| ); | ||
|
|
||
| return $processors; | ||
| } | ||
| } |
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,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; | ||
| } |
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.
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 need to be rebased as it will conflict once we merge #2 .