Skip to content
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

Fix for using cache pools with multiple areas #2232

Merged
merged 12 commits into from
Mar 6, 2024
22 changes: 22 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultFalse()
->end()
->arrayNode('cache')
->setDeprecated(
'nelmio/api-doc-bundle',
'4.23',
'Using global cache config for all areas is deprecated. Define it on area level instead.'
)
->validate()
->ifTrue(function ($v) { return null !== $v['item_id'] && null === $v['pool']; })
->thenInvalid('Can not set cache.item_id if cache.pool is null')
Expand Down Expand Up @@ -71,6 +76,7 @@ public function getConfigTreeBuilder(): TreeBuilder
'documentation' => [],
'name_patterns' => [],
'disable_default_routes' => false,
'cache' => [],
],
]
)
Expand Down Expand Up @@ -122,6 +128,22 @@ public function getConfigTreeBuilder(): TreeBuilder
->example(['info' => ['title' => 'My App']])
->prototype('variable')->end()
->end()
->arrayNode('cache')
->validate()
->ifTrue(function ($v) { return null !== $v['pool'] && null === $v['item_id']; })
->thenInvalid('Must set cache.item_id if cache.pool is set')
Brajk19 marked this conversation as resolved.
Show resolved Hide resolved
->end()
->children()
->scalarNode('pool')
->info('define cache pool to use')
->defaultNull()
->end()
->scalarNode('item_id')
->info('define cache item id')
->defaultNull()
->end()
->end()
->end()
->end()
->end()
->end()
Expand Down
12 changes: 10 additions & 2 deletions src/DependencyInjection/NelmioApiDocExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ public function load(array $configs, ContainerBuilder $container): void
$cacheItemId = $config['cache']['item_id'] ?? null;

foreach ($config['areas'] as $area => $areaConfig) {
$areaCachePool = $cachePool;
$areaCacheItemId = $cacheItemId;

if (isset($areaConfig['cache']['pool'])) {
$areaCachePool = $areaConfig['cache']['pool'];
$areaCacheItemId = $areaConfig['cache']['item_id'];
}

$nameAliases = $this->findNameAliases($config['models']['names'], $area);
$container->register(sprintf('nelmio_api_doc.generator.%s', $area), ApiDocGenerator::class)
->setPublic(true)
Expand All @@ -99,8 +107,8 @@ public function load(array $configs, ContainerBuilder $container): void
->setArguments([
new TaggedIteratorArgument(sprintf('nelmio_api_doc.describer.%s', $area)),
new TaggedIteratorArgument('nelmio_api_doc.model_describer'),
$cachePool,
$cacheItemId,
$areaCachePool,
$areaCacheItemId,
new Reference('nelmio_api_doc.open_api.generator'),
]);

Expand Down
26 changes: 23 additions & 3 deletions tests/DependencyInjection/NelmioApiDocExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,21 @@ public function testApiDocGeneratorWithCachePool()
$container->setParameter('kernel.bundles', []);

$extension = new NelmioApiDocExtension();
$extension->load([
[
'documentation' => [
'info' => [
'title' => 'API documentation',
'description' => 'This is the api documentation, use it wisely',
],
],
'areas' => [
'default' => [
],
'area1' => [],
],
],
], $container);
$extension->load([
[
'documentation' => [
Expand All @@ -170,20 +185,25 @@ public function testApiDocGeneratorWithCachePool()
'item_id' => 'nelmio.docs',
],
'areas' => [
'default' => [],
'default' => [
'cache' => [
'pool' => 'test.cache.default',
'item_id' => 'nelmio.docs.default',
],
],
'area1' => [],
],
],
], $container);

$reference = $container->getDefinition('nelmio_api_doc.generator.default')->getArgument(2);
$this->assertSame('test.cache', (string) $reference);
$this->assertSame('test.cache.default', (string) $reference);

$reference = $container->getDefinition('nelmio_api_doc.generator.area1')->getArgument(2);
$this->assertSame('test.cache', (string) $reference);

$cacheItemId = $container->getDefinition('nelmio_api_doc.generator.default')->getArgument(3);
$this->assertSame('nelmio.docs', $cacheItemId);
$this->assertSame('nelmio.docs.default', $cacheItemId);

$cacheItemId = $container->getDefinition('nelmio_api_doc.generator.area1')->getArgument(3);
$this->assertSame('nelmio.docs', $cacheItemId);
Expand Down