Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ProcessLayoutRenderElement.php
Go to the documentation of this file.
1 <?php
8 
13 
15 {
21  private $_config;
22 
28  private $isVarnishEnabled;
29 
35  private $isFullPageCacheEnabled;
36 
40  private $entitySpecificHandlesList;
41 
45  private $base64jsonSerializer;
46 
50  private $jsonSerializer;
51 
60  public function __construct(
61  \Magento\PageCache\Model\Config $config,
62  EntitySpecificHandlesList $entitySpecificHandlesList = null,
63  Json $jsonSerializer = null,
64  Base64Json $base64jsonSerializer = null
65  ) {
66  $this->_config = $config;
67  $this->entitySpecificHandlesList = $entitySpecificHandlesList
68  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(EntitySpecificHandlesList::class);
69  $this->jsonSerializer = $jsonSerializer
71  $this->base64jsonSerializer = $base64jsonSerializer
72  ?: \Magento\Framework\App\ObjectManager::getInstance()->get(Base64Json::class);
73  }
74 
82  private function _wrapEsi(
83  \Magento\Framework\View\Element\AbstractBlock $block,
84  \Magento\Framework\View\Layout $layout
85  ) {
86  $handles = $layout->getUpdate()->getHandles();
87  $pageSpecificHandles = $this->entitySpecificHandlesList->getHandles();
88  $url = $block->getUrl(
89  'page_cache/block/esi',
90  [
91  'blocks' => $this->jsonSerializer->serialize([$block->getNameInLayout()]),
92  'handles' => $this->base64jsonSerializer->serialize(
93  array_values(array_diff($handles, $pageSpecificHandles))
94  )
95  ]
96  );
97  // Varnish does not support ESI over HTTPS must change to HTTP
98  $url = substr($url, 0, 5) === 'https' ? 'http' . substr($url, 5) : $url;
99  return sprintf('<esi:include src="%s" />', $url);
100  }
101 
107  private function isFullPageCacheEnabled()
108  {
109  if ($this->isFullPageCacheEnabled === null) {
110  $this->isFullPageCacheEnabled = $this->_config->isEnabled();
111  }
112  return $this->isFullPageCacheEnabled;
113  }
114 
120  private function isVarnishEnabled()
121  {
122  if ($this->isVarnishEnabled === null) {
123  $this->isVarnishEnabled = ($this->_config->getType() == \Magento\PageCache\Model\Config::VARNISH);
124  }
125  return $this->isVarnishEnabled;
126  }
127 
135  public function execute(\Magento\Framework\Event\Observer $observer)
136  {
137  $event = $observer->getEvent();
139  $layout = $event->getLayout();
140  if ($this->isFullPageCacheEnabled() && $layout->isCacheable()) {
141  $name = $event->getElementName();
143  $block = $layout->getBlock($name);
144  $transport = $event->getTransport();
145  if ($block instanceof \Magento\Framework\View\Element\AbstractBlock) {
146  $blockTtl = $block->getTtl();
147  $output = $transport->getData('output');
148  if (isset($blockTtl) && $this->isVarnishEnabled()) {
149  $output = $this->_wrapEsi($block, $layout);
150  } elseif ($block->isScopePrivate()) {
151  $output = sprintf(
152  '<!-- BLOCK %1$s -->%2$s<!-- /BLOCK %1$s -->',
153  $block->getNameInLayout(),
154  $output
155  );
156  }
157  $transport->setData('output', $output);
158  }
159  }
160  }
161 }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$config
Definition: fraud_order.php:17
$block
Definition: block.php:8
__construct(\Magento\PageCache\Model\Config $config, EntitySpecificHandlesList $entitySpecificHandlesList=null, Json $jsonSerializer=null, Base64Json $base64jsonSerializer=null)
if(!isset($_GET['name'])) $name
Definition: log.php:14