forked from Sylius/Sylius
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI] Force no-store cache directives for admin and customer account s…
…ection
- Loading branch information
1 parent
67de9e8
commit 4b6a77a
Showing
10 changed files
with
358 additions
and
1 deletion.
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/Sylius/Bundle/AdminBundle/EventListener/AdminSectionCacheControlSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\AdminBundle\EventListener; | ||
|
||
use Sylius\Bundle\AdminBundle\SectionResolver\AdminSection; | ||
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
final class AdminSectionCacheControlSubscriber implements EventSubscriberInterface | ||
{ | ||
private SectionProviderInterface $sectionProvider; | ||
|
||
public function __construct(SectionProviderInterface $sectionProvider) | ||
{ | ||
$this->sectionProvider = $sectionProvider; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::RESPONSE => 'setCacheControlDirectives', | ||
]; | ||
} | ||
|
||
public function setCacheControlDirectives(ResponseEvent $event): void | ||
{ | ||
if (!$this->sectionProvider->getSection() instanceof AdminSection) { | ||
return; | ||
} | ||
|
||
$response = $event->getResponse(); | ||
|
||
$response->headers->addCacheControlDirective('no-cache', true); | ||
$response->headers->addCacheControlDirective('max-age', '0'); | ||
$response->headers->addCacheControlDirective('must-revalidate', true); | ||
$response->headers->addCacheControlDirective('no-store', true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
96 changes: 96 additions & 0 deletions
96
src/Sylius/Bundle/AdminBundle/spec/EventListener/AdminSectionCacheControlSubscriberSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\AdminBundle\EventListener; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\AdminBundle\EmailManager\ShipmentEmailManagerInterface; | ||
use Sylius\Bundle\AdminBundle\SectionResolver\AdminSection; | ||
use Sylius\Bundle\CoreBundle\SectionResolver\SectionInterface; | ||
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface; | ||
use Sylius\Component\Core\Model\ShipmentInterface; | ||
use Symfony\Component\EventDispatcher\GenericEvent; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
final class AdminSectionCacheControlSubscriberSpec extends ObjectBehavior | ||
{ | ||
function let(SectionProviderInterface $sectionProvider): void | ||
{ | ||
$this->beConstructedWith($sectionProvider); | ||
} | ||
|
||
function it_subscribes_to_kernel_response_event() | ||
{ | ||
$this::getSubscribedEvents()->shouldReturn([KernelEvents::RESPONSE => 'setCacheControlDirectives']); | ||
} | ||
|
||
function it_adds_cache_control_directives_to_admin_requests( | ||
SectionProviderInterface $sectionProvider, | ||
HttpKernelInterface $kernel, | ||
Request $request, | ||
Response $response, | ||
ResponseHeaderBag $responseHeaderBag, | ||
AdminSection $adminSection | ||
): void { | ||
$sectionProvider->getSection()->willReturn($adminSection); | ||
|
||
$response->headers = $responseHeaderBag->getWrappedObject(); | ||
|
||
$event = new ResponseEvent( | ||
$kernel->getWrappedObject(), | ||
$request->getWrappedObject(), | ||
KernelInterface::MASTER_REQUEST, | ||
$response->getWrappedObject() | ||
); | ||
|
||
$responseHeaderBag->addCacheControlDirective('no-cache', true)->shouldBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('max-age', '0')->shouldBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('must-revalidate', true)->shouldBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('no-store', true)->shouldBeCalled(); | ||
|
||
$this->setCacheControlDirectives($event); | ||
} | ||
|
||
function it_does_nothing_if_section_is_different_then_admin( | ||
SectionProviderInterface $sectionProvider, | ||
HttpKernelInterface $kernel, | ||
Request $request, | ||
Response $response, | ||
ResponseHeaderBag $responseHeaderBag, | ||
SectionInterface $section | ||
): void { | ||
$sectionProvider->getSection()->willReturn($section); | ||
|
||
$response->headers = $responseHeaderBag->getWrappedObject(); | ||
|
||
$event = new ResponseEvent( | ||
$kernel->getWrappedObject(), | ||
$request->getWrappedObject(), | ||
KernelInterface::MASTER_REQUEST, | ||
$response->getWrappedObject() | ||
); | ||
|
||
$responseHeaderBag->addCacheControlDirective('no-cache', true)->shouldNotBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('max-age', '0')->shouldNotBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('must-revalidate', true)->shouldNotBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('no-store', true)->shouldNotBeCalled(); | ||
|
||
$this->setCacheControlDirectives($event); | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
...s/Bundle/ShopBundle/EventListener/ShopCustomerAccountSubSectionCacheControlSubscriber.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ShopBundle\EventListener; | ||
|
||
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface; | ||
use Sylius\Bundle\ShopBundle\SectionResolver\ShopCustomerAccountSubSection; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
|
||
final class ShopCustomerAccountSubSectionCacheControlSubscriber implements EventSubscriberInterface | ||
{ | ||
private SectionProviderInterface $sectionProvider; | ||
|
||
public function __construct(SectionProviderInterface $sectionProvider) | ||
{ | ||
$this->sectionProvider = $sectionProvider; | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [ | ||
KernelEvents::RESPONSE => 'setCacheControlDirectives', | ||
]; | ||
} | ||
|
||
public function setCacheControlDirectives(ResponseEvent $event): void | ||
{ | ||
if (!$this->sectionProvider->getSection() instanceof ShopCustomerAccountSubSection) { | ||
return; | ||
} | ||
|
||
$response = $event->getResponse(); | ||
|
||
$response->headers->addCacheControlDirective('no-cache', true); | ||
$response->headers->addCacheControlDirective('max-age', '0'); | ||
$response->headers->addCacheControlDirective('must-revalidate', true); | ||
$response->headers->addCacheControlDirective('no-store', true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/Sylius/Bundle/ShopBundle/SectionResolver/ShopCustomerAccountSubSection.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Sylius\Bundle\ShopBundle\SectionResolver; | ||
|
||
class ShopCustomerAccountSubSection extends ShopSection | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
93 changes: 93 additions & 0 deletions
93
...ShopBundle/spec/EventListener/ShopCustomerAccountSubSectionCacheControlSubscriberSpec.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace spec\Sylius\Bundle\ShopBundle\EventListener; | ||
|
||
use PhpSpec\ObjectBehavior; | ||
use Sylius\Bundle\CoreBundle\SectionResolver\SectionInterface; | ||
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface; | ||
use Sylius\Bundle\ShopBundle\SectionResolver\ShopCustomerAccountSubSection; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\ResponseHeaderBag; | ||
use Symfony\Component\HttpKernel\Event\ResponseEvent; | ||
use Symfony\Component\HttpKernel\HttpKernelInterface; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
final class ShopCustomerAccountSubSectionCacheControlSubscriberSpec extends ObjectBehavior | ||
{ | ||
function let(SectionProviderInterface $sectionProvider): void | ||
{ | ||
$this->beConstructedWith($sectionProvider); | ||
} | ||
|
||
function it_subscribes_to_kernel_response_event() | ||
{ | ||
$this::getSubscribedEvents()->shouldReturn([KernelEvents::RESPONSE => 'setCacheControlDirectives']); | ||
} | ||
|
||
function it_adds_cache_control_directives_to_customer_account_requests( | ||
SectionProviderInterface $sectionProvider, | ||
HttpKernelInterface $kernel, | ||
Request $request, | ||
Response $response, | ||
ResponseHeaderBag $responseHeaderBag, | ||
ShopCustomerAccountSubSection $customerAccountSubSection | ||
): void { | ||
$sectionProvider->getSection()->willReturn($customerAccountSubSection); | ||
|
||
$response->headers = $responseHeaderBag->getWrappedObject(); | ||
|
||
$event = new ResponseEvent( | ||
$kernel->getWrappedObject(), | ||
$request->getWrappedObject(), | ||
KernelInterface::MASTER_REQUEST, | ||
$response->getWrappedObject() | ||
); | ||
|
||
$responseHeaderBag->addCacheControlDirective('no-cache', true)->shouldBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('max-age', '0')->shouldBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('must-revalidate', true)->shouldBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('no-store', true)->shouldBeCalled(); | ||
|
||
$this->setCacheControlDirectives($event); | ||
} | ||
|
||
function it_does_nothing_if_section_is_different_then_customer_account( | ||
SectionProviderInterface $sectionProvider, | ||
HttpKernelInterface $kernel, | ||
Request $request, | ||
Response $response, | ||
ResponseHeaderBag $responseHeaderBag, | ||
SectionInterface $section | ||
): void { | ||
$sectionProvider->getSection()->willReturn($section); | ||
|
||
$response->headers = $responseHeaderBag->getWrappedObject(); | ||
|
||
$event = new ResponseEvent( | ||
$kernel->getWrappedObject(), | ||
$request->getWrappedObject(), | ||
KernelInterface::MASTER_REQUEST, | ||
$response->getWrappedObject() | ||
); | ||
|
||
$responseHeaderBag->addCacheControlDirective('no-cache', true)->shouldNotBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('max-age', '0')->shouldNotBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('must-revalidate', true)->shouldNotBeCalled(); | ||
$responseHeaderBag->addCacheControlDirective('no-store', true)->shouldNotBeCalled(); | ||
|
||
$this->setCacheControlDirectives($event); | ||
} | ||
} |
Oops, something went wrong.