From 7a5e617844848b5514fbb2b8b1c741c6b4ff728f Mon Sep 17 00:00:00 2001 From: Antoine Makdessi Date: Wed, 8 Jan 2025 15:34:43 +0100 Subject: [PATCH] feat: allow to configure header name for reverse_proxy_ttl specific value --- src/DependencyInjection/Configuration.php | 6 +++++- src/DependencyInjection/FOSHttpCacheExtension.php | 2 ++ src/EventListener/CacheControlListener.php | 5 +++-- src/Resources/config/cache_control_listener.xml | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/DependencyInjection/Configuration.php b/src/DependencyInjection/Configuration.php index 3fb9ffdc..62c55ea1 100644 --- a/src/DependencyInjection/Configuration.php +++ b/src/DependencyInjection/Configuration.php @@ -277,6 +277,10 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void ->end() ->end() ->end() + ->scalarNode('ttl_header') + ->default('X-Reverse-Proxy-TTL') + ->info('Specify an ttl header name (works with cache_control.reverse_proxy_ttl)') + ->end() ->arrayNode('rules') ->prototype('array') ->children(); @@ -330,7 +334,7 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void ->end() ->scalarNode('reverse_proxy_ttl') ->defaultNull() - ->info('Specify an X-Reverse-Proxy-TTL header with a time in seconds for a caching proxy under your control.') + ->info('Specify an X-Reverse-Proxy-TTL (cache_control.ttl_header) header with a time in seconds for a caching proxy under your control.') ->end() ->arrayNode('vary') ->beforeNormalization()->ifString()->then(function ($v) { diff --git a/src/DependencyInjection/FOSHttpCacheExtension.php b/src/DependencyInjection/FOSHttpCacheExtension.php index 61ae698d..9b535d07 100644 --- a/src/DependencyInjection/FOSHttpCacheExtension.php +++ b/src/DependencyInjection/FOSHttpCacheExtension.php @@ -48,7 +48,9 @@ public function load(array $configs, ContainerBuilder $container): void if ($config['debug']['enabled'] || (!empty($config['cache_control']))) { $debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false; + $ttlHeader = $config['cache_control']['ttl_header'] ?? 'X-Reverse-Proxy-TTL'; $container->setParameter('fos_http_cache.debug_header', $debugHeader); + $container->setParameter('fos_http_cache.ttl_header', $ttlHeader); $loader->load('cache_control_listener.xml'); } diff --git a/src/EventListener/CacheControlListener.php b/src/EventListener/CacheControlListener.php index da040ab8..d9bfbdee 100644 --- a/src/EventListener/CacheControlListener.php +++ b/src/EventListener/CacheControlListener.php @@ -56,6 +56,7 @@ public function __construct( * @var string|false Name of the header or false to add no header */ private readonly string|false $debugHeader = false, + private readonly string $ttlHeader = 'X-Reverse-Proxy-TTL', ) { } @@ -115,9 +116,9 @@ public function onKernelResponse(ResponseEvent $event): void if (array_key_exists('reverse_proxy_ttl', $options) && null !== $options['reverse_proxy_ttl'] - && !$response->headers->has('X-Reverse-Proxy-TTL') + && !$response->headers->has($this->ttlHeader) ) { - $response->headers->set('X-Reverse-Proxy-TTL', $options['reverse_proxy_ttl'], false); + $response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false); } if (!empty($options['vary'])) { diff --git a/src/Resources/config/cache_control_listener.xml b/src/Resources/config/cache_control_listener.xml index cc1129d6..3249d0a3 100644 --- a/src/Resources/config/cache_control_listener.xml +++ b/src/Resources/config/cache_control_listener.xml @@ -9,6 +9,7 @@ class="FOS\HttpCacheBundle\EventListener\CacheControlListener" public="true"> %fos_http_cache.debug_header% + %fos_http_cache.ttl_header%