From 8103e64bd82c2982c799242c22c6fb0acca1583a Mon Sep 17 00:00:00 2001 From: soyuka Date: Mon, 8 Jun 2026 11:58:50 +0200 Subject: [PATCH] fix(symfony): register http cache purgers independently of invalidation flag MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When `http_cache.invalidation.enabled` is false, the bundle previously skipped loading `http_cache_purger.php`, which left the concrete purger services (`api_platform.http_cache.purger.varnish.ban`, `.varnish.xkey`, `.souin` plus the `api_platform.http_cache.purger.varnish` alias) undefined. Userland that decorates or injects those purgers — common when running multiple environments where invalidation is off in dev/test — then booted with a `ServiceNotFoundException`. Move the purger services load outside the invalidation early return, gated only on `interface_exists(PurgerInterface::class)` so missing `api-platform/http-cache` still skips cleanly. The invalidation listener (`api_platform.doctrine.listener.http_cache.purge`), the `AddTagsProcessor` state processor, the `scoped_clients` tagging, the `urls`/`varnish_urls` ScopingHttpClient wiring and the `api_platform.http_cache.purger` alias remain behind the invalidation flag — only the concrete purger classes are exposed unconditionally. Fixes #8095 --- .../ApiPlatformExtension.php | 8 +++- .../ApiPlatformExtensionTest.php | 38 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) 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'); + } }