Skip to content

#643: Fix cache control expression Symfony DI configuration #644

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/DependencyInjection/FOSHttpCacheExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,10 +259,10 @@ private function parseResponseMatcher(ContainerBuilder $container, array $config
$id = 'fos_http_cache.cache_control.expression.'.md5($config['match_response']);
if (!$container->hasDefinition($id)) {
$childDefinition = (new ChildDefinition('fos_http_cache.response_matcher.cache_control.expression'))
->replaceArgument(0, $config['match_response'])
->setArgument(0, $config['match_response'])
;
if (!empty($config['match_response_expression_service'])) {
$childDefinition->replaceArgument(1, new Reference($config['match_response_expression_service']));
if (!empty($config['expression_language'])) {
$childDefinition->setArgument(1, new Reference($config['expression_language']));
}
$container
->setDefinition($id, $childDefinition)
Expand Down
55 changes: 55 additions & 0 deletions tests/Unit/DependencyInjection/FOSHttpCacheExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use Symfony\Component\DependencyInjection\Exception\RuntimeException;
use Symfony\Component\DependencyInjection\ParameterBag\EnvPlaceholderParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpKernel\Controller\ControllerResolverInterface;
use Symfony\Component\Routing\Router;

Expand Down Expand Up @@ -489,6 +490,38 @@ public function testConfigLoadCacheControlExpression(): void
$this->assertListenerHasRule($container, 'fos_http_cache.event_listener.cache_control');
}

public function testConfigLoadCacheControlExpressionWithOverriddenExpressionLanguage(): void
{
$config = $this->getCacheControlExpressionFullConfig();

$container = $this->createContainer();
$this->extension->load([$config], $container);

$id = 'fos_http_cache.cache_control.expression.'.md5('foobar');
$this->assertTrue($container->hasDefinition($id), 'expression child definition not created as expected');
$this->assertEquals(
[
'foobar',
new Reference('app.expression_language'),
],
$container->getDefinition($id)->getArguments()
);
}

public function testContainerCompilesWithCacheControlExpressionConfig(): void
{
$config = $this->getCacheControlExpressionFullConfig();

$container = $this->createContainer();
$this->extension->load([$config], $container);

$container->addDefinitions(['app.expression_language' => new Definition(ExpressionLanguage::class)]);

$container->compile();

$this->expectNotToPerformAssertions();
}

/**
* Check if comma separated strings are parsed as expected.
*/
Expand Down Expand Up @@ -813,6 +846,28 @@ private function getBaseConfig(): array
];
}

/**
* @return array<string, mixed>
*/
private function getCacheControlExpressionFullConfig(): array
{
return [
'cache_control' => [
'rules' => [
[
'match' => [
'match_response' => 'foobar',
'expression_language' => 'app.expression_language',
],
'headers' => [
'cache_control' => ['public' => true],
],
],
],
],
];
}

/**
* @param array $methods List of methods for the matcher. Empty array to not check.
*
Expand Down
Loading