|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @license For full copyright and license information view LICENSE file distributed with this source code. |
|
4
|
|
|
*/ |
|
5
|
|
|
namespace eZ\Bundle\EzPublishCoreBundle\EventSubscriber; |
|
6
|
|
|
|
|
7
|
|
|
use eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator\ResponseCacheConfigurator; |
|
8
|
|
|
use eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseTagger\ResponseTagger; |
|
9
|
|
|
use eZ\Publish\Core\MVC\Symfony\View\CachableView; |
|
10
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
11
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
12
|
|
|
use Symfony\Component\HttpKernel\KernelEvents; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Configures the Response HTTP cache properties. |
|
16
|
|
|
*/ |
|
17
|
|
|
class HttpCacheResponseSubscriber implements EventSubscriberInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseTagger\ResponseTagger |
|
21
|
|
|
*/ |
|
22
|
|
|
private $dispatcherTagger; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var \eZ\Publish\Core\MVC\Symfony\Cache\Http\ResponseConfigurator\ResponseCacheConfigurator |
|
26
|
|
|
*/ |
|
27
|
|
|
private $responseConfigurator; |
|
28
|
|
|
|
|
29
|
|
|
public function __construct(ResponseCacheConfigurator $responseConfigurator, ResponseTagger $dispatcherTagger) |
|
30
|
|
|
{ |
|
31
|
|
|
$this->responseConfigurator = $responseConfigurator; |
|
32
|
|
|
$this->dispatcherTagger = $dispatcherTagger; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public static function getSubscribedEvents() |
|
36
|
|
|
{ |
|
37
|
|
|
return [KernelEvents::RESPONSE => 'configureCache']; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public function configureCache(FilterResponseEvent $event) |
|
41
|
|
|
{ |
|
42
|
|
|
$view = $event->getRequest()->attributes->get('view'); |
|
43
|
|
|
if (!$view instanceof CachableView || !$view->isCacheEnabled()) { |
|
44
|
|
|
return; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
$response = $event->getResponse(); |
|
48
|
|
|
$this->responseConfigurator->enableCache($response); |
|
49
|
|
|
$this->responseConfigurator->setSharedMaxAge($response); |
|
50
|
|
|
$this->dispatcherTagger->tag($this->responseConfigurator, $response, $view); |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|