Skip to content

Commit

Permalink
Fix before implementation with custom processors
Browse files Browse the repository at this point in the history
  • Loading branch information
bobvandevijver committed Jan 15, 2025
1 parent 13408ab commit e438007
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 12 deletions.
14 changes: 3 additions & 11 deletions src/DependencyInjection/Compiler/CustomProcessorPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\Compiler\PriorityTaggedServiceTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

/**
* Compiler Pass to identify and register custom processors.
Expand All @@ -36,24 +35,17 @@ public function process(ContainerBuilder $container): void
$definition = $container->findDefinition('nelmio_api_doc.open_api.generator');

foreach ($this->findAndSortTaggedServices('nelmio_api_doc.swagger.processor', $container) as $reference) {
$id = (string) $reference;
$tags = $container->findDefinition($id)->getTags();

/**
* Before which processor should this processor be run?
*
* @var string|null
*/
$before = null;
$tags = $container->findDefinition((string) $reference)->getTag('nelmio_api_doc.swagger.processor');

// See if the processor has a 'before' attribute.
$before = null;
foreach ($tags as $tag) {
if (isset($tag['before'])) {
$before = $tag['before'];
}
}

$definition->addMethodCall('addProcessor', [new Reference($id), $before]);
$definition->addMethodCall('addProcessor', [$reference, $before]);
}
}
}
4 changes: 4 additions & 0 deletions tests/Functional/Configs/StubProcessor.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
services:
Nelmio\ApiDocBundle\Tests\Functional\StubProcessor:
tags:
- { name: 'nelmio_api_doc.swagger.processor', priority: -100, before: OpenApi\Processors\CleanUnusedComponents }
2 changes: 1 addition & 1 deletion tests/Functional/ControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public static function provideUniversalTestCases(): \Generator
null,
'VendorExtension',
[],
[__DIR__.'/Configs/VendorExtension.yaml'],
[__DIR__.'/Configs/VendorExtension.yaml', __DIR__.'/Configs/StubProcessor.yaml'],
];
}

Expand Down
22 changes: 22 additions & 0 deletions tests/Functional/StubProcessor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Nelmio\ApiDocBundle\Tests\Functional;

use OpenApi\Analysis;

class StubProcessor
{
public function __invoke(Analysis $analysis): void
{
// Does nothing
}
}

0 comments on commit e438007

Please sign in to comment.