LayoutPlugin.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\PageCache\Model\Layout;
  7. /**
  8. * Class LayoutPlugin
  9. */
  10. class LayoutPlugin
  11. {
  12. /**
  13. * @var \Magento\PageCache\Model\Config
  14. */
  15. protected $config;
  16. /**
  17. * @var \Magento\Framework\App\ResponseInterface
  18. */
  19. protected $response;
  20. /**
  21. * Constructor
  22. *
  23. * @param \Magento\Framework\App\ResponseInterface $response
  24. * @param \Magento\PageCache\Model\Config $config
  25. */
  26. public function __construct(
  27. \Magento\Framework\App\ResponseInterface $response,
  28. \Magento\PageCache\Model\Config $config
  29. ) {
  30. $this->response = $response;
  31. $this->config = $config;
  32. }
  33. /**
  34. * Set appropriate Cache-Control headers
  35. * We have to set public headers in order to tell Varnish and Builtin app that page should be cached
  36. *
  37. * @param \Magento\Framework\View\Layout $subject
  38. * @param mixed $result
  39. * @return mixed
  40. */
  41. public function afterGenerateXml(\Magento\Framework\View\Layout $subject, $result)
  42. {
  43. if ($subject->isCacheable() && $this->config->isEnabled()) {
  44. $this->response->setPublicHeaders($this->config->getTtl());
  45. }
  46. return $result;
  47. }
  48. /**
  49. * Retrieve all identities from blocks for further cache invalidation
  50. *
  51. * @param \Magento\Framework\View\Layout $subject
  52. * @param mixed $result
  53. * @return mixed
  54. */
  55. public function afterGetOutput(\Magento\Framework\View\Layout $subject, $result)
  56. {
  57. if ($subject->isCacheable() && $this->config->isEnabled()) {
  58. $tags = [];
  59. foreach ($subject->getAllBlocks() as $block) {
  60. if ($block instanceof \Magento\Framework\DataObject\IdentityInterface) {
  61. $isEsiBlock = $block->getTtl() > 0;
  62. $isVarnish = $this->config->getType() == \Magento\PageCache\Model\Config::VARNISH;
  63. if ($isVarnish && $isEsiBlock) {
  64. continue;
  65. }
  66. $tags = array_merge($tags, $block->getIdentities());
  67. }
  68. }
  69. $tags = array_unique($tags);
  70. $this->response->setHeader('X-Magento-Tags', implode(',', $tags));
  71. }
  72. return $result;
  73. }
  74. }