Skip to content

[HttpKernel] Fix using entities with the #[Cache()] attribute #48509

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 1 commit into from
Dec 6, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\Cache;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

Expand Down Expand Up @@ -47,7 +47,7 @@ public function __construct(
/**
* Handles HTTP validation headers.
*/
public function onKernelController(ControllerEvent $event)
public function onKernelControllerArguments(ControllerArgumentsEvent $event)
{
$request = $event->getRequest();

Expand All @@ -63,12 +63,12 @@ public function onKernelController(ControllerEvent $event)
/** @var Cache[] $attributes */
foreach ($attributes as $cache) {
if (null !== $cache->lastModified) {
$lastModified = $this->getExpressionLanguage()->evaluate($cache->lastModified, $request->attributes->all());
$lastModified = $this->getExpressionLanguage()->evaluate($cache->lastModified, array_merge($request->attributes->all(), $event->getNamedArguments()));
($response ??= new Response())->setLastModified($lastModified);
}

if (null !== $cache->etag) {
$etag = hash('sha256', $this->getExpressionLanguage()->evaluate($cache->etag, $request->attributes->all()));
$etag = hash('sha256', $this->getExpressionLanguage()->evaluate($cache->etag, array_merge($request->attributes->all(), $event->getNamedArguments())));
($response ??= new Response())->setEtag($etag);
}
}
Expand Down Expand Up @@ -169,7 +169,7 @@ public function onKernelResponse(ResponseEvent $event)
public static function getSubscribedEvents(): array
{
return [
KernelEvents::CONTROLLER => ['onKernelController', 10],
KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 10],
KernelEvents::RESPONSE => ['onKernelResponse', -10],
];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\Cache;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\EventListener\CacheAttributeListener;
use Symfony\Component\HttpKernel\HttpKernelInterface;
Expand Down Expand Up @@ -170,35 +170,48 @@ public function testCacheMaxAgeSupportsStrtotimeFormat()
$this->assertSame('86400', $this->response->headers->getCacheControlDirective('stale-if-error'));
}

public function testLastModifiedNotModifiedResponse()
/**
* @testWith ["test.getDate()"]
* ["date"]
*/
public function testLastModifiedNotModifiedResponse(string $expression)
{
$request = $this->createRequest(new Cache(lastModified: 'test.getDate()'));
$request->attributes->set('test', new TestEntity());
$entity = new TestEntity();

$request = $this->createRequest(new Cache(lastModified: $expression));
$request->attributes->set('date', new \DateTimeImmutable('Fri, 23 Aug 2013 00:00:00 GMT'));
$request->headers->add(['If-Modified-Since' => 'Fri, 23 Aug 2013 00:00:00 GMT']);

$listener = new CacheAttributeListener();
$controllerEvent = new ControllerEvent($this->getKernel(), function () {
$controllerArgumentsEvent = new ControllerArgumentsEvent($this->getKernel(), function (TestEntity $test) {
return new Response();
}, $request, null);
}, [$entity], $request, null);

$listener->onKernelController($controllerEvent);
$response = \call_user_func($controllerEvent->getController());
$listener->onKernelControllerArguments($controllerArgumentsEvent);
$response = $controllerArgumentsEvent->getController()($entity);

$this->assertSame(304, $response->getStatusCode());
}

public function testLastModifiedHeader()
/**
* @testWith ["test.getDate()"]
* ["date"]
*/
public function testLastModifiedHeader(string $expression)
{
$request = $this->createRequest(new Cache(lastModified: 'test.getDate()'));
$request->attributes->set('test', new TestEntity());
$entity = new TestEntity();

$request = $this->createRequest(new Cache(lastModified: $expression));
$request->attributes->set('date', new \DateTimeImmutable('Fri, 23 Aug 2013 00:00:00 GMT'));

$listener = new CacheAttributeListener();
$controllerEvent = new ControllerEvent($this->getKernel(), function () {
$controllerArgumentsEvent = new ControllerArgumentsEvent($this->getKernel(), function (TestEntity $test) {
return new Response();
}, $request, null);
$listener->onKernelController($controllerEvent);
}, [$entity], $request, null);
$listener->onKernelControllerArguments($controllerArgumentsEvent);

$responseEvent = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, \call_user_func($controllerEvent->getController()));
$controllerResponse = $controllerArgumentsEvent->getController()($entity);
$responseEvent = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $controllerResponse);
$listener->onKernelResponse($responseEvent);

$response = $responseEvent->getResponse();
Expand All @@ -208,35 +221,48 @@ public function testLastModifiedHeader()
$this->assertSame('Fri, 23 Aug 2013 00:00:00 GMT', $response->headers->get('Last-Modified'));
}

public function testEtagNotModifiedResponse()
/**
* @testWith ["test.getId()"]
* ["id"]
*/
public function testEtagNotModifiedResponse(string $expression)
{
$request = $this->createRequest(new Cache(etag: 'test.getId()'));
$request->attributes->set('test', $entity = new TestEntity());
$entity = new TestEntity();

$request = $this->createRequest(new Cache(etag: $expression));
$request->attributes->set('id', '12345');
$request->headers->add(['If-None-Match' => sprintf('"%s"', hash('sha256', $entity->getId()))]);

$listener = new CacheAttributeListener();
$controllerEvent = new ControllerEvent($this->getKernel(), function () {
$controllerArgumentsEvent = new ControllerArgumentsEvent($this->getKernel(), function (TestEntity $test) {
return new Response();
}, $request, null);
}, [$entity], $request, null);

$listener->onKernelController($controllerEvent);
$response = \call_user_func($controllerEvent->getController());
$listener->onKernelControllerArguments($controllerArgumentsEvent);
$response = $controllerArgumentsEvent->getController()($entity);

$this->assertSame(304, $response->getStatusCode());
}

public function testEtagHeader()
/**
* @testWith ["test.getId()"]
* ["id"]
*/
public function testEtagHeader(string $expression)
{
$request = $this->createRequest(new Cache(etag: 'test.getId()'));
$request->attributes->set('test', $entity = new TestEntity());
$entity = new TestEntity();

$request = $this->createRequest(new Cache(etag: $expression));
$request->attributes->set('id', '12345');

$listener = new CacheAttributeListener();
$controllerEvent = new ControllerEvent($this->getKernel(), function () {
$controllerArgumentsEvent = new ControllerArgumentsEvent($this->getKernel(), function (TestEntity $test) {
return new Response();
}, $request, null);
$listener->onKernelController($controllerEvent);
}, [$entity], $request, null);
$listener->onKernelControllerArguments($controllerArgumentsEvent);

$responseEvent = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, \call_user_func($controllerEvent->getController()));
$controllerResponse = $controllerArgumentsEvent->getController()($entity);
$responseEvent = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $controllerResponse);
$listener->onKernelResponse($responseEvent);

$response = $responseEvent->getResponse();
Expand Down Expand Up @@ -280,8 +306,8 @@ public function testConfigurationDoesNotOverrideAlreadySetResponseHeaders()
public function testAttribute()
{
$request = new Request();
$event = new ControllerEvent($this->getKernel(), [new CacheAttributeController(), 'foo'], $request, null);
$this->listener->onKernelController($event);
$event = new ControllerArgumentsEvent($this->getKernel(), [new CacheAttributeController(), 'foo'], [], $request, null);
$this->listener->onKernelControllerArguments($event);

$response = new Response();
$event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
Expand All @@ -290,8 +316,8 @@ public function testAttribute()
$this->assertSame(CacheAttributeController::METHOD_SMAXAGE, $response->getMaxAge());

$request = new Request();
$event = new ControllerEvent($this->getKernel(), [new CacheAttributeController(), 'bar'], $request, null);
$this->listener->onKernelController($event);
$event = new ControllerArgumentsEvent($this->getKernel(), [new CacheAttributeController(), 'bar'], [], $request, null);
$this->listener->onKernelControllerArguments($event);

$response = new Response();
$event = new ResponseEvent($this->getKernel(), $request, HttpKernelInterface::MAIN_REQUEST, $response);
Expand Down