From ffbd31f8016e37a421e114f24c2c2dac2099966e Mon Sep 17 00:00:00 2001 From: Tony Lemke Date: Tue, 2 Oct 2018 13:22:59 +0200 Subject: [PATCH] cache_lifetime can be null to let the underlying cache handle the lifetime --- .../BazingaGeocoderExtension.php | 7 +++++- Tests/Functional/BundleInitializationTest.php | 24 +++++++++++++++++++ .../config/cache_without_lifetime.yml | 13 ++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 Tests/Functional/config/cache_without_lifetime.yml diff --git a/DependencyInjection/BazingaGeocoderExtension.php b/DependencyInjection/BazingaGeocoderExtension.php index 00284259..db197735 100644 --- a/DependencyInjection/BazingaGeocoderExtension.php +++ b/DependencyInjection/BazingaGeocoderExtension.php @@ -112,6 +112,11 @@ public function configureProviderPlugins(ContainerBuilder $container, array $con } if (isset($config['cache']) || isset($config['cache_lifetime']) || isset($config['cache_precision'])) { + $cacheLifetime = isset($config['cache_lifetime']) ? $config['cache_lifetime'] : null; + if (null !== $cacheLifetime) { + $cacheLifetime = (int) $cacheLifetime; + } + if (null === $cacheServiceId = $config['cache']) { if (!$container->has('app.cache')) { throw new \LogicException('You need to specify a service for cache.'); @@ -121,7 +126,7 @@ public function configureProviderPlugins(ContainerBuilder $container, array $con $plugins[] = $providerServiceId.'.cache'; $container->register($providerServiceId.'.cache', CachePlugin::class) ->setPublic(false) - ->setArguments([new Reference($cacheServiceId), (int) $config['cache_lifetime'], $config['cache_precision']]); + ->setArguments([new Reference($cacheServiceId), $cacheLifetime, $config['cache_precision']]); } if (isset($config['limit'])) { diff --git a/Tests/Functional/BundleInitializationTest.php b/Tests/Functional/BundleInitializationTest.php index a76b659a..f4a564f9 100644 --- a/Tests/Functional/BundleInitializationTest.php +++ b/Tests/Functional/BundleInitializationTest.php @@ -83,6 +83,30 @@ public function testBundleWithCachedProvider() $this->assertInstanceOf(CachePlugin::class, $plugins[0]); } + /** + * @test + */ + public function cache_lifetime_can_be_null_in_order_to_let_this_detail_handle_the_cache_service() + { + $kernel = $this->createKernel(); + $kernel->addConfigFile(__DIR__.'/config/cache_without_lifetime.yml'); + $this->bootKernel(); + $container = $this->getContainer(); + + self::assertTrue($container->has('bazinga_geocoder.provider.acme')); + /** @var PluginProvider $service */ + $service = $container->get('bazinga_geocoder.provider.acme'); + self::assertInstanceOf(PluginProvider::class, $service); + $plugins = NSA::getProperty($service, 'plugins'); + self::assertCount(1, $plugins); + + $cachePlugin = array_shift($plugins); + self::assertInstanceOf(CachePlugin::class, $cachePlugin); + + $cacheLifeTime = NSA::getProperty($cachePlugin, 'lifetime'); + self::assertNull($cacheLifeTime); + } + public function testBundleWithPluginsYml() { $kernel = $this->createKernel(); diff --git a/Tests/Functional/config/cache_without_lifetime.yml b/Tests/Functional/config/cache_without_lifetime.yml new file mode 100644 index 00000000..c8c7a3f9 --- /dev/null +++ b/Tests/Functional/config/cache_without_lifetime.yml @@ -0,0 +1,13 @@ + +bazinga_geocoder: + profiling: + enabled: false + providers: + acme: + factory: Bazinga\GeocoderBundle\ProviderFactory\GoogleMapsFactory + cache_precision: 4 + cache: acme.cache + +services: + acme.cache: + class: Bazinga\GeocoderBundle\Tests\Functional\Helper\CacheHelper