Skip to content

Commit

Permalink
feat: allow to configure header name for reverse_proxy_ttl specific v…
Browse files Browse the repository at this point in the history
…alue
  • Loading branch information
94noni committed Jan 8, 2025
1 parent cfb93c3 commit 7a5e617
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 3 deletions.
6 changes: 5 additions & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand Down
5 changes: 3 additions & 2 deletions src/EventListener/CacheControlListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
) {
}

Expand Down Expand Up @@ -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'])) {
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/cache_control_listener.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
class="FOS\HttpCacheBundle\EventListener\CacheControlListener"
public="true">
<argument>%fos_http_cache.debug_header%</argument>
<argument>%fos_http_cache.ttl_header%</argument>
<tag name="kernel.event_subscriber" />
</service>
<service id="FOS\HttpCacheBundle\EventListener\CacheControlListener" alias="fos_http_cache.event_listener.cache_control" public="true"/>
Expand Down

0 comments on commit 7a5e617

Please sign in to comment.