Skip to content

Commit c500786

Browse files
authored
Merge pull request #657 from mvhirsch/bugfix/varnish-auto-path-resolution
Bugfix: automatically detect url type generation for varnish
2 parents 84dff29 + 7f429f3 commit c500786

2 files changed

Lines changed: 39 additions & 3 deletions

File tree

src/DependencyInjection/FOSHttpCacheExtension.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -745,9 +745,12 @@ private function determineGenerateUrlType(array $config): int
745745
}
746746

747747
$defaultClient = $this->getDefaultProxyClient($config['proxy_client']);
748-
if ('noop' !== $defaultClient
749-
&& array_key_exists('base_url', $config['proxy_client'][$defaultClient])) {
750-
return UrlGeneratorInterface::ABSOLUTE_PATH;
748+
if ('noop' !== $defaultClient) {
749+
$clientConfig = $config['proxy_client'][$defaultClient];
750+
$hasBaseUrl = !empty($clientConfig['base_url'] ?? null) || !empty($clientConfig['http']['base_url'] ?? null);
751+
if ($hasBaseUrl) {
752+
return UrlGeneratorInterface::ABSOLUTE_PATH;
753+
}
751754
}
752755
if ('cloudfront' === $defaultClient) {
753756
return UrlGeneratorInterface::ABSOLUTE_PATH;

tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Symfony\Component\DependencyInjection\Reference;
2929
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
3030
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
31+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
3132
use Symfony\Component\Routing\Router;
3233

3334
class FOSHttpCacheExtensionTest extends TestCase
@@ -53,6 +54,38 @@ public function testConfigLoadVarnish(): void
5354
$this->assertTrue($container->hasDefinition('fos_http_cache.event_listener.tag'));
5455
}
5556

57+
public function testVarnishWithBaseUrlAutoResolvesAbsolutePath(): void
58+
{
59+
$container = $this->createContainer();
60+
$this->extension->load([$this->getBaseConfig()], $container);
61+
62+
$this->assertSame(
63+
UrlGeneratorInterface::ABSOLUTE_PATH,
64+
$container->getParameter('fos_http_cache.cache_manager.generate_url_type'),
65+
'Varnish with base_url must auto-resolve to ABSOLUTE_PATH so the Host header uses base_url, not the CMS request hostname.'
66+
);
67+
}
68+
69+
public function testVarnishWithoutBaseUrlAutoResolvesAbsoluteUrl(): void
70+
{
71+
$container = $this->createContainer();
72+
$config = [
73+
'proxy_client' => [
74+
'varnish' => [
75+
'http' => [
76+
'servers' => ['127.0.0.1'],
77+
],
78+
],
79+
],
80+
];
81+
$this->extension->load([$config], $container);
82+
83+
$this->assertSame(
84+
UrlGeneratorInterface::ABSOLUTE_URL,
85+
$container->getParameter('fos_http_cache.cache_manager.generate_url_type')
86+
);
87+
}
88+
5689
public function testConfigLoadVarnishCustomClient(): void
5790
{
5891
$container = $this->createContainer();

0 commit comments

Comments
 (0)