-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathHttpCacheResponseSubscriber.php
More file actions
56 lines (49 loc) · 1.99 KB
/
HttpCacheResponseSubscriber.php
File metadata and controls
56 lines (49 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
/**
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace EzSystems\PlatformHttpCacheBundle\EventSubscriber;
use EzSystems\PlatformHttpCacheBundle\ResponseConfigurator\ResponseCacheConfigurator;
use EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger;
use eZ\Publish\Core\MVC\Symfony\View\CachableView;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;
/**
* Configures the Response HTTP cache properties.
*/
class HttpCacheResponseSubscriber implements EventSubscriberInterface
{
/**
* @var \EzSystems\PlatformHttpCacheBundle\ResponseTagger\ResponseTagger
*/
private $dispatcherTagger;
/**
* @var \EzSystems\PlatformHttpCacheBundle\ResponseConfigurator\ResponseCacheConfigurator
*/
private $responseConfigurator;
public function __construct(ResponseCacheConfigurator $responseConfigurator, ResponseTagger $dispatcherTagger)
{
$this->responseConfigurator = $responseConfigurator;
$this->dispatcherTagger = $dispatcherTagger;
}
public static function getSubscribedEvents()
{
return [KernelEvents::RESPONSE => 'configureCache'];
}
public function configureCache(FilterResponseEvent $event)
{
if ($event->getRequest()->attributes->has('is_rest_request') && $event->getRequest()->attributes->get('is_rest_request') === true) {
$view = null;
} else {
$view = $event->getRequest()->attributes->get('view');
if (!$view instanceof CachableView || !$view->isCacheEnabled()) {
return;
}
}
$response = $event->getResponse();
$this->responseConfigurator->enableCache($response);
$this->responseConfigurator->setSharedMaxAge($response);
$this->dispatcherTagger->tag($this->responseConfigurator, $response, $view);
}
}