Skip to content

Commit b9aeb42

Browse files
committed
Moved cache configuration to compiler pass, so any expressions used in the metadata_cache_pool config setting can be resolved
1 parent 4edb0b8 commit b9aeb42

File tree

4 files changed

+41
-16
lines changed

4 files changed

+41
-16
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ before_script:
2323
- export XDEBUG_MODE=coverage
2424

2525
script:
26-
- wget -q --waitretry=1 --retry-connrefused -T 30 -O - http://127.0.0.1:9200
26+
- wget -nv --waitretry=1 --retry-connrefused -T 30 -O - http://127.0.0.1:9200
2727
- vendor/bin/simple-phpunit --coverage-clover=Tests/App/build/clover.xml 2>/dev/null
2828
- vendor/bin/phpcs -np --standard=PSR2 --ignore=vendor/,Tests/App/,var/ ./
2929

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Sineflow\ElasticsearchBundle\DependencyInjection\Compiler;
4+
5+
use Sineflow\ElasticsearchBundle\Mapping\DocumentMetadataCollector;
6+
use Symfony\Component\Cache\Adapter\NullAdapter;
7+
use Symfony\Component\DependencyInjection\ChildDefinition;
8+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
9+
use Symfony\Component\DependencyInjection\ContainerBuilder;
10+
use Symfony\Component\DependencyInjection\Definition;
11+
use Symfony\Component\DependencyInjection\Reference;
12+
13+
class SetCachePass implements CompilerPassInterface
14+
{
15+
/**
16+
* {@inheritdoc}
17+
*/
18+
public function process(ContainerBuilder $container)
19+
{
20+
$customCachePool = $container->resolveEnvPlaceholders($container->getParameter('sfes.cache_pool'), true);
21+
22+
if ($customCachePool) {
23+
// Use the custom metadata cache pool, if one is defined
24+
$cachePoolDefinition = new Reference($customCachePool);
25+
} else {
26+
if (!$container->getParameter('kernel.debug')) {
27+
// Use a service extending cache.system when kernel.debug is false (typically in prod and stage)
28+
$cachePoolDefinition = new ChildDefinition('cache.system');
29+
$cachePoolDefinition->addTag('cache.pool');
30+
} else {
31+
// By default, don't use cache if kernel.debug is set (typically in dev and test)
32+
$cachePoolDefinition = new Definition(NullAdapter::class);
33+
}
34+
}
35+
$container->getDefinition(DocumentMetadataCollector::class)->setArgument('$cache', $cachePoolDefinition);
36+
}
37+
}

DependencyInjection/SineflowElasticsearchExtension.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@
44

55
use Sineflow\ElasticsearchBundle\Document\Provider\ProviderInterface;
66
use Sineflow\ElasticsearchBundle\Document\Repository\ServiceRepositoryInterface;
7-
use Sineflow\ElasticsearchBundle\Mapping\DocumentMetadataCollector;
87
use Symfony\Component\Config\FileLocator;
9-
use Symfony\Component\DependencyInjection\ChildDefinition;
108
use Symfony\Component\DependencyInjection\ContainerBuilder;
119
use Symfony\Component\DependencyInjection\Loader;
12-
use Symfony\Component\DependencyInjection\Reference;
1310
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1411

1512
/**
@@ -43,6 +40,7 @@ public function load(array $configs, ContainerBuilder $container)
4340
$container->setParameter('sfes.connections', $config['connections']);
4441
$container->setParameter('sfes.indices', $config['indices']);
4542
$container->setParameter('sfes.languages', $config['languages']);
43+
$container->setParameter('sfes.cache_pool', $config['metadata_cache_pool'] ?? null);
4644

4745
$container
4846
->registerForAutoconfiguration(ServiceRepositoryInterface::class)
@@ -51,17 +49,5 @@ public function load(array $configs, ContainerBuilder $container)
5149
$container
5250
->registerForAutoconfiguration(ProviderInterface::class)
5351
->addTag('sfes.provider');
54-
55-
// Set cache pool
56-
if (isset($config['metadata_cache_pool'])) {
57-
// Use the configured metadata cache pool, if one is defined
58-
$cachePoolDefinition = new Reference($config['metadata_cache_pool']);
59-
} else {
60-
// Use a service extending cache.system
61-
$cachePoolDefinition = new ChildDefinition('cache.system');
62-
$cachePoolDefinition->addTag('cache.pool');
63-
$container->setDefinition('cache.sfes', $cachePoolDefinition);
64-
}
65-
$container->getDefinition(DocumentMetadataCollector::class)->setArgument('$cache', $cachePoolDefinition);
6652
}
6753
}

SineflowElasticsearchBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Sineflow\ElasticsearchBundle\DependencyInjection\Compiler\AddConnectionsPass;
66
use Sineflow\ElasticsearchBundle\DependencyInjection\Compiler\AddIndexManagersPass;
7+
use Sineflow\ElasticsearchBundle\DependencyInjection\Compiler\SetCachePass;
78
use Symfony\Component\DependencyInjection\ContainerBuilder;
89
use Symfony\Component\HttpKernel\Bundle\Bundle;
910

@@ -21,5 +22,6 @@ public function build(ContainerBuilder $container)
2122

2223
$container->addCompilerPass(new AddConnectionsPass());
2324
$container->addCompilerPass(new AddIndexManagersPass());
25+
$container->addCompilerPass(new SetCachePass());
2426
}
2527
}

0 commit comments

Comments
 (0)