Skip to content
This repository has been archived by the owner on Feb 24, 2023. It is now read-only.

Commit

Permalink
bug #779 Check _cache type for compatibility with Symfony 6.2 (GromNaN)
Browse files Browse the repository at this point in the history
This PR was merged into the 6.1.x-dev branch.

Discussion
----------

Check _cache type for compatibility with Symfony 6.2

The new `#[Cache()]` attribute that is added to Symfony 6.2 (symfony/symfony#46880) is stored in the same request attribute `_cache`.

Before this change, if the bundle is enabled and Symfony's Cache attribute is used, we get an error.

```
Call to a member function getSMaxAge() on array
```

This bugfix will ease migration when simultaneously usage of FrameworkExtraBundle and Symfony attributes is necessary.

Commits
-------

0fd5fdf Check _cache type for compatibility with Symfony 6.2
  • Loading branch information
fabpot committed Nov 1, 2022
2 parents bb962f8 + 0fd5fdf commit dcfac94
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
7 changes: 5 additions & 2 deletions src/EventListener/HttpCacheListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sensio\Bundle\FrameworkExtraBundle\EventListener;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -42,7 +43,8 @@ public function __construct()
public function onKernelController(KernelEvent $event)
{
$request = $event->getRequest();
if (!$configuration = $request->attributes->get('_cache')) {
$configuration = $request->attributes->get('_cache');
if (!$configuration instanceof Cache) {
return;
}

Expand Down Expand Up @@ -81,8 +83,9 @@ public function onKernelController(KernelEvent $event)
public function onKernelResponse(KernelEvent $event)
{
$request = $event->getRequest();
$configuration = $request->attributes->get('_cache');

if (!$configuration = $request->attributes->get('_cache')) {
if (!$configuration instanceof Cache) {
return;
}

Expand Down
10 changes: 10 additions & 0 deletions tests/EventListener/HttpCacheListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,16 @@ public function testWontReassignResponseWhenNoConfigurationIsPresent()
$this->assertSame($response, $this->event->getResponse());
}

public function testIgnoreUnknownCacheAttribute()
{
$response = $this->event->getResponse();

$this->request->attributes->set('_cache', new \stdClass());

$this->assertNull($this->listener->onKernelResponse($this->event));
$this->assertSame($response, $this->event->getResponse());
}

public function testResponseIsPublicIfSharedMaxAgeSetAndPublicNotOverridden()
{
$request = $this->createRequest(new Cache([
Expand Down

0 comments on commit dcfac94

Please sign in to comment.