diff --git a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index bca6f7107d..5a6c408a4f 100644 --- a/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -870,6 +870,13 @@ private function registerHttpCacheConfiguration(ContainerBuilder $container, arr { $loader->load('http_cache.php'); + // Concrete purger services are always registered when the http-cache package is present, + // so userland can decorate or inject them even when invalidation is disabled (#8095). + // The invalidation listener, the AddTagsProcessor and the HTTP-client wiring stay gated below. + if (interface_exists(\ApiPlatform\HttpCache\PurgerInterface::class)) { + $loader->load('http_cache_purger.php'); + } + if (!$this->isConfigEnabled($container, $config['http_cache']['invalidation'])) { return; } @@ -879,7 +886,6 @@ private function registerHttpCacheConfiguration(ContainerBuilder $container, arr } $loader->load('state/http_cache_purger.php'); - $loader->load('http_cache_purger.php'); foreach ($config['http_cache']['invalidation']['scoped_clients'] as $client) { $definition = $container->getDefinition($client); diff --git a/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index ee7e3abe7f..a782d2a0fe 100644 --- a/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/src/Symfony/Tests/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -444,4 +444,42 @@ public function testPropertyInfoExtractorsDoNotLeakIntoFrameworkPropertyInfo(): } } } + + /** + * @see https://github.com/api-platform/core/issues/8095 + */ + public function testHttpCachePurgersRegisteredWhenInvalidationDisabled(): void + { + $config = self::DEFAULT_CONFIG; + $config['api_platform']['http_cache']['invalidation']['enabled'] = false; + (new ApiPlatformExtension())->load($config, $this->container); + + $this->assertContainerHasService('api_platform.http_cache.purger.varnish.ban'); + $this->assertContainerHasService('api_platform.http_cache.purger.varnish.xkey'); + $this->assertContainerHasService('api_platform.http_cache.purger.souin'); + $this->assertContainerHasAlias('api_platform.http_cache.purger.varnish'); + + $this->assertNotContainerHasService('api_platform.doctrine.listener.http_cache.purge'); + $this->assertNotContainerHasService('api_platform.http_cache_purger.processor.add_tags'); + $this->assertFalse($this->container->hasAlias('api_platform.http_cache.purger')); + } + + /** + * @see https://github.com/api-platform/core/issues/8095 + */ + public function testHttpCachePurgersAndListenerRegisteredWhenInvalidationEnabled(): void + { + $config = self::DEFAULT_CONFIG; + $config['api_platform']['http_cache']['invalidation']['enabled'] = true; + (new ApiPlatformExtension())->load($config, $this->container); + + $this->assertContainerHasService('api_platform.http_cache.purger.varnish.ban'); + $this->assertContainerHasService('api_platform.http_cache.purger.varnish.xkey'); + $this->assertContainerHasService('api_platform.http_cache.purger.souin'); + $this->assertContainerHasAlias('api_platform.http_cache.purger.varnish'); + + $this->assertContainerHasService('api_platform.doctrine.listener.http_cache.purge'); + $this->assertContainerHasService('api_platform.http_cache_purger.processor.add_tags'); + $this->assertContainerHasAlias('api_platform.http_cache.purger'); + } }