Skip to content
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);
}
}
11 changes: 10 additions & 1 deletion src/NetgenApiPlatformExtrasBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,24 @@
namespace Netgen\ApiPlatformExtras;

use Netgen\ApiPlatformExtras\DependencyInjection\CompilerPass\IriTemplateGeneratorCompilerPass;
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
Copy link
Member

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 .

{
public function build(ContainerBuilder $container): void
{
$container->addCompilerPass(
$container
->addCompilerPass(
new IriTemplateGeneratorCompilerPass(),
)
->addCompilerPass(
new SchemaProcessorCompilerPass(),
);

$container->registerForAutoconfiguration(OpenApiProcessorInterface::class)
->addTag('netgen_api_platform_extras.open_api_processor');
}
}
57 changes: 57 additions & 0 deletions src/OpenApi/Factory/OpenApiFactory.php
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;
}
}
21 changes: 21 additions & 0 deletions src/OpenApi/Processor/OpenApiProcessorInterface.php
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;
}