Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
LayoutPlugin.php
Go to the documentation of this file.
1 <?php
7 
12 {
16  protected $config;
17 
21  protected $response;
22 
29  public function __construct(
30  \Magento\Framework\App\ResponseInterface $response,
31  \Magento\PageCache\Model\Config $config
32  ) {
33  $this->response = $response;
34  $this->config = $config;
35  }
36 
45  public function afterGenerateXml(\Magento\Framework\View\Layout $subject, $result)
46  {
47  if ($subject->isCacheable() && $this->config->isEnabled()) {
48  $this->response->setPublicHeaders($this->config->getTtl());
49  }
50  return $result;
51  }
52 
60  public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result)
61  {
62  if ($subject->isCacheable() && $this->config->isEnabled()) {
63  $tags = [];
64  foreach ($subject->getAllBlocks() as $block) {
65  if ($block instanceof \Magento\Framework\DataObject\IdentityInterface) {
66  $isEsiBlock = $block->getTtl() > 0;
67  $isVarnish = $this->config->getType() == \Magento\PageCache\Model\Config::VARNISH;
68  if ($isVarnish && $isEsiBlock) {
69  continue;
70  }
71  $tags = array_merge($tags, $block->getIdentities());
72  }
73  }
74  $tags = array_unique($tags);
75  $this->response->setHeader('X-Magento-Tags', implode(',', $tags));
76  }
77  return $result;
78  }
79 }
$block
Definition: block.php:8
afterGetOutput(\Magento\Framework\View\Layout $subject, $result)
afterGenerateXml(\Magento\Framework\View\Layout $subject, $result)
__construct(\Magento\Framework\App\ResponseInterface $response, \Magento\PageCache\Model\Config $config)