Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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',
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 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)

),
])
->setDecoratedService('api_platform.openapi.factory', 'api_platform.openapi.factory.inner', -25);
}
}
16 changes: 15 additions & 1 deletion src/NetgenApiPlatformExtrasBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
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(
new SchemaProcessorCompilerPass(),
);

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unnecessary readonly.

{
/**
* @param iterable<OpenApiProcessorInterface> $processors
Copy link
Member

Choose a reason for hiding this comment

The 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;
}
}
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;
}