Skip to content

Commit 553a717

Browse files
committed
feat: allow to configure header name for reverse_proxy_ttl specific value
1 parent cfb93c3 commit 553a717

File tree

10 files changed

+39
-5
lines changed

10 files changed

+39
-5
lines changed

CHANGELOG.md

+7
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ Changelog
44
3.x
55
===
66

7+
3.1.0
8+
-----
9+
10+
### Added
11+
12+
* New Feature: allow configuring the TTL header name for the special `reverse_proxy_ttl` config value. #638
13+
714
3.0.2
815
-----
916

src/DependencyInjection/Configuration.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,10 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
277277
->end()
278278
->end()
279279
->end()
280+
->scalarNode('ttl_header')
281+
->defaultValue('X-Reverse-Proxy-TTL')
282+
->info('Specify the header name to use with the cache_control.reverse_proxy_ttl setting')
283+
->end()
280284
->arrayNode('rules')
281285
->prototype('array')
282286
->children();
@@ -330,7 +334,7 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
330334
->end()
331335
->scalarNode('reverse_proxy_ttl')
332336
->defaultNull()
333-
->info('Specify an X-Reverse-Proxy-TTL header with a time in seconds for a caching proxy under your control.')
337+
->info('Specify a custom time to live in seconds for your caching proxy. This value is sent in the custom header configured in cache_control.ttl_header.')
334338
->end()
335339
->arrayNode('vary')
336340
->beforeNormalization()->ifString()->then(function ($v) {

src/DependencyInjection/FOSHttpCacheExtension.php

+2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,9 @@ public function load(array $configs, ContainerBuilder $container): void
4848

4949
if ($config['debug']['enabled'] || (!empty($config['cache_control']))) {
5050
$debugHeader = $config['debug']['enabled'] ? $config['debug']['header'] : false;
51+
$ttlHeader = $config['cache_control']['ttl_header'] ?? '';
5152
$container->setParameter('fos_http_cache.debug_header', $debugHeader);
53+
$container->setParameter('fos_http_cache.ttl_header', $ttlHeader);
5254
$loader->load('cache_control_listener.xml');
5355
}
5456

src/EventListener/CacheControlListener.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public function __construct(
5656
* @var string|false Name of the header or false to add no header
5757
*/
5858
private readonly string|false $debugHeader = false,
59+
private readonly string $ttlHeader = 'X-Reverse-Proxy-TTL',
5960
) {
6061
}
6162

@@ -115,9 +116,9 @@ public function onKernelResponse(ResponseEvent $event): void
115116

116117
if (array_key_exists('reverse_proxy_ttl', $options)
117118
&& null !== $options['reverse_proxy_ttl']
118-
&& !$response->headers->has('X-Reverse-Proxy-TTL')
119+
&& !$response->headers->has($this->ttlHeader)
119120
) {
120-
$response->headers->set('X-Reverse-Proxy-TTL', $options['reverse_proxy_ttl'], false);
121+
$response->headers->set($this->ttlHeader, $options['reverse_proxy_ttl'], false);
121122
}
122123

123124
if (!empty($options['vary'])) {

src/Resources/config/cache_control_listener.xml

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
class="FOS\HttpCacheBundle\EventListener\CacheControlListener"
1010
public="true">
1111
<argument>%fos_http_cache.debug_header%</argument>
12+
<argument>%fos_http_cache.ttl_header%</argument>
1213
<tag name="kernel.event_subscriber" />
1314
</service>
1415
<service id="FOS\HttpCacheBundle\EventListener\CacheControlListener" alias="fos_http_cache.event_listener.cache_control" public="true"/>

tests/Resources/Fixtures/config/split.php

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
$container->loadFromExtension('fos_http_cache', [
1313
'cache_control' => [
14+
'ttl_header' => 'X-My-Header',
1415
'rules' => [
1516
[
1617
'match' => [

tests/Resources/Fixtures/config/split.xml

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
<config xmlns="http://example.org/schema/dic/fos_http_cache">
55
<cache-control>
6+
<ttl-header>X-My-Header</ttl-header>
67
<rule>
78
<match>
89
<methods>GET,POST</methods>

tests/Resources/Fixtures/config/split.yml

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
fos_http_cache:
22

33
cache_control:
4+
ttl_header: X-My-Header
45
rules:
56
-
67
match:

tests/Unit/DependencyInjection/ConfigurationTest.php

+1
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@ public function testSplitOptions(): void
474474
{
475475
$expectedConfiguration = $this->getEmptyConfig();
476476
$expectedConfiguration['cache_control'] = [
477+
'ttl_header' => 'X-My-Header',
477478
'rules' => [
478479
[
479480
'match' => [

tests/Unit/EventListener/CacheControlListenerTest.php

+17-2
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,21 @@ public function testReverseProxyTtl(): void
334334
$this->assertEquals(600, $newHeaders['x-reverse-proxy-ttl'][0]);
335335
}
336336

337+
public function testReverseProxyTtlHeader(): void
338+
{
339+
$event = $this->buildEvent();
340+
$headers = [
341+
'reverse_proxy_ttl' => 700,
342+
];
343+
$listener = $this->getCacheControl($headers, 'X-My-Header');
344+
345+
$listener->onKernelResponse($event);
346+
$newHeaders = $event->getResponse()->headers->all();
347+
348+
$this->assertTrue(isset($newHeaders['x-my-header']), implode(',', array_keys($newHeaders)));
349+
$this->assertEquals(700, $newHeaders['x-my-header'][0]);
350+
}
351+
337352
public function testDebugHeader(): void
338353
{
339354
$listener = new CacheControlListener('X-Cache-Debug');
@@ -426,9 +441,9 @@ protected function buildEvent(string $method = 'GET'): ResponseEvent
426441
*
427442
* @param array $headers The headers to return from the matcher
428443
*/
429-
protected function getCacheControl(array $headers): CacheControlListener|MockObject
444+
protected function getCacheControl(array $headers, string $ttlHeader = 'X-Reverse-Proxy-TTL'): CacheControlListener|MockObject
430445
{
431-
$listener = new CacheControlListener();
446+
$listener = new CacheControlListener(false, $ttlHeader);
432447

433448
$matcher = \Mockery::mock(RuleMatcherInterface::class)
434449
->shouldReceive(['matches' => true])

0 commit comments

Comments
 (0)