Skip to content

Commit ffdf716

Browse files
authored
Merge pull request #3 from netgen/NGSTACK-1017-schema-processor
Ngstack 1017 schema processor
2 parents a5a236c + 9c0e3c1 commit ffdf716

4 files changed

Lines changed: 126 additions & 1 deletion

File tree

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\ApiPlatformExtras\DependencyInjection\CompilerPass;
6+
7+
use Netgen\ApiPlatformExtras\OpenApi\Factory\OpenApiFactory;
8+
use Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument;
9+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
10+
use Symfony\Component\DependencyInjection\ContainerBuilder;
11+
use Symfony\Component\DependencyInjection\Definition;
12+
use Symfony\Component\DependencyInjection\Reference;
13+
14+
final class SchemaProcessorCompilerPass implements CompilerPassInterface
15+
{
16+
private const FEATURE_ENABLED_PARAMETER = 'netgen_api_platform_extras.features.schema_processor.enabled';
17+
18+
public function process(ContainerBuilder $container): void
19+
{
20+
if (
21+
!$container->hasParameter(self::FEATURE_ENABLED_PARAMETER)
22+
|| $container->getParameter(self::FEATURE_ENABLED_PARAMETER) === false
23+
) {
24+
return;
25+
}
26+
27+
$container
28+
->setDefinition(
29+
OpenApiFactory::class,
30+
new Definition(OpenApiFactory::class),
31+
)
32+
->setArguments([
33+
new Reference('api_platform.openapi.factory.inner'),
34+
new TaggedIteratorArgument('netgen_api_platform_extras.open_api_processor'),
35+
])
36+
->setDecoratedService('api_platform.openapi.factory', 'api_platform.openapi.factory.inner', -25);
37+
}
38+
}

src/NetgenApiPlatformExtrasBundle.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,24 @@
55
namespace Netgen\ApiPlatformExtras;
66

77
use Netgen\ApiPlatformExtras\DependencyInjection\CompilerPass\IriTemplateGeneratorCompilerPass;
8+
use Netgen\ApiPlatformExtras\DependencyInjection\CompilerPass\SchemaProcessorCompilerPass;
9+
use Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface;
810
use Symfony\Component\DependencyInjection\ContainerBuilder;
911
use Symfony\Component\HttpKernel\Bundle\Bundle;
1012

1113
final class NetgenApiPlatformExtrasBundle extends Bundle
1214
{
1315
public function build(ContainerBuilder $container): void
1416
{
15-
$container->addCompilerPass(
17+
$container
18+
->addCompilerPass(
1619
new IriTemplateGeneratorCompilerPass(),
20+
)
21+
->addCompilerPass(
22+
new SchemaProcessorCompilerPass(),
1723
);
24+
25+
$container->registerForAutoconfiguration(OpenApiProcessorInterface::class)
26+
->addTag('netgen_api_platform_extras.open_api_processor');
1827
}
1928
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\ApiPlatformExtras\OpenApi\Factory;
6+
7+
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
8+
use ApiPlatform\OpenApi\OpenApi;
9+
10+
use function iterator_to_array;
11+
use function usort;
12+
13+
final class OpenApiFactory implements OpenApiFactoryInterface
14+
{
15+
/**
16+
* @param iterable<\Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface> $processors
17+
*/
18+
public function __construct(
19+
private OpenApiFactoryInterface $decorated,
20+
private iterable $processors,
21+
) {
22+
$this->processors = $this->sortProcessors($processors);
23+
}
24+
25+
public function __invoke(array $context = []): OpenApi
26+
{
27+
$openApi = ($this->decorated)($context);
28+
29+
return $this->applyProcessors($openApi);
30+
}
31+
32+
private function applyProcessors(OpenApi $openApi): OpenApi
33+
{
34+
foreach ($this->processors as $processor) {
35+
$openApi = $processor->process($openApi);
36+
}
37+
38+
return $openApi;
39+
}
40+
41+
/**
42+
* @param iterable<\Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface> $processors
43+
*
44+
* @return \Netgen\ApiPlatformExtras\OpenApi\Processor\OpenApiProcessorInterface[]
45+
*/
46+
private function sortProcessors(iterable $processors): array
47+
{
48+
$processors = iterator_to_array($processors);
49+
50+
usort(
51+
$processors,
52+
static fn ($a, $b): int => $b::getPriority() <=> $a::getPriority(),
53+
);
54+
55+
return $processors;
56+
}
57+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Netgen\ApiPlatformExtras\OpenApi\Processor;
6+
7+
use ApiPlatform\OpenApi\OpenApi;
8+
9+
interface OpenApiProcessorInterface
10+
{
11+
/**
12+
* Used in compiler pass to set the tagged items service priority.
13+
*/
14+
public static function getPriority(): int;
15+
16+
/**
17+
* Process the OpenAPI specification.
18+
* Can modify schemas, paths, operations, or any other part of the spec.
19+
*/
20+
public function process(OpenApi $openApi): OpenApi;
21+
}

0 commit comments

Comments
 (0)