helper = $helper; $this->lazyLoad = $LazyLoad; $this->request = $request; $this->_cacheState = $cacheState; $this->_cache = $cache ?: $helper->_loadObject(Type::class); $this->serializer = $serializer ?: $helper->_loadObject(Json::class); } /** * @return bool */ public function useCache() { if (!is_bool($this->_useCache)) { // $this->_useCache = (bool) $this->helper->getModuleConfig(static::LAZY_USE_CACHE); $this->_useCache = false; } return $this->_useCache; } /** * @param Observer $observer * @return void */ public function execute(Observer $observer) { if (!$this->lazyLoad->isEnabled() || ( $this->request->isXmlHttpRequest() && $this->request->isAjax() ) ) { return; } /** @var ResponseHttp $response */ $response = $observer->getEvent()->getData('response'); if (!$response) { return; } $html = $response->getBody(); if (empty($html)) { return; } if ($this->useCache()) { $_html = $this->_loadCache(); if (!empty($_html)) { $response->setBody($_html); return; } } $html = $this->lazyLoad->replaceImageToLazy($html); if ($this->useCache()) { $this->_saveCache($html, $response); } $response->setBody($html); } /** * Load response from cache storage * * @return string */ protected function _loadCache() { $cacheKey = $this->getCacheKey(); $html = $this->_cache->load($cacheKey); return $html; } /** * Get Key for caching block content * * @return string */ public function getCacheKey() { $data = [ $this->request->isSecure(), $this->request->getUriString(), $this->request->get(ResponseHttp::COOKIE_VARY_STRING), ]; $key = sha1($this->serializer->serialize($data)); return static::CACHE_KEY_PREFIX . $key; } /** * Save response content to cache storage * * @param string $html * @param ResponseHttp $response * @return void */ protected function _saveCache($html, ResponseHttp $response) { if ($this->_cacheState->isEnabled(self::TYPE_IDENTIFIER) && ( $response->getHttpResponseCode() == 200 || $response->getHttpResponseCode() == 404 ) && ( $this->request->isGet() || $this->request->isHead() ) ) { $tagsHeader = $response->getHeader('X-Magento-Tags'); $tags = $tagsHeader ? explode(',', $tagsHeader->getFieldValue()) : []; $cacheKey = $this->getCacheKey(); $tags[] = self::CACHE_TAG; $this->_cache->save($html, $cacheKey, array_unique($tags)); } } }