![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/app/code/Soon/AdvancedCache/Model/Engine/Type/ |
<?php namespace Soon\AdvancedCache\Model\Engine\Type; use Magento\Framework\App\Config\ScopeConfigInterface; use Magento\Framework\App\Http\Context as HttpContext; use Magento\Framework\App\RequestInterface; use Magento\Framework\DataObject; use Magento\Framework\View\DesignInterface; use Magento\Framework\View\Element\BlockInterface; use Magento\Framework\View\LayoutInterface; use Magento\Store\Model\StoreManagerInterface; abstract class EngineTypeAbstract extends DataObject implements EngineTypeInterface { /** * @var BlockInterface|\Magento\Framework\View\Element\AbstractBlock */ public $block; /** * @var StoreManagerInterface */ public $store; /** * @var LayoutInterface */ public $layout; /** * @var array */ public $tags; /** * @var string */ public $lifetime; /** * @var string */ public $key; /** * @var RequestInterface */ public $request; /** * @var HttpContext */ public $httpContext; /** * @var DesignInterface */ public $design; /** * @var ScopeConfigInterface */ public $config; public function __construct( StoreManagerInterface $store, LayoutInterface $layout, RequestInterface $request, HttpContext $httpContext, DesignInterface $design, ScopeConfigInterface $scopeConfig, array $data = [] ) { $this->store = $store; $this->layout = $layout; $this->request = $request; $this->httpContext = $httpContext; $this->design = $design; $this->config = $scopeConfig; parent::__construct($data); } /** * {@inheritdoc} */ public function cacheBlock(BlockInterface $block) { $this->block = $block; $this->block->setData('is_advanced_cache', false); // Prepare any additional data useful for caching $this->prepareAdditional(); // Prepare cache config $this->addCacheTags(); $this->setLifetime(); $this->buildCacheKey(); // Set cache info to block if ($this->canCache()) { $this->block->setData('is_advanced_cache', $this->setCacheToBlock()); } return $block; } /** * Prepare additional info * * @return $this */ public function prepareAdditional() { return $this; } /** * Build the cache tags * * @return mixed */ abstract public function addCacheTags(); /** * Build the cache lifetime * * @return mixed */ abstract public function setLifetime(); /** * Build the cache key * * @return mixed */ abstract public function buildCacheKey(); /** * Can we cache this block? * * @return bool */ public function canCache() { if ($this->tags !== null && $this->key !== null && $this->lifetime !== null) { return true; } $blockName = $this->block->getNameInLayout(); $msg = 'Block with name "' . $blockName . '" cannot be cached because of missing information.'; throw new \RuntimeException($msg); } /** * Add real cache data to Magento block so it can be saved/loaded from caching engine * * @return bool */ public function setCacheToBlock() { // By default, block is cachable $canCache = true; // If there are exceptions, block is not cachable if (!empty($this->getExceptions()) && $this->preferExceptions()) { $canCache = false; } // If block is not cacheable, it means that is has exceptions if (!$canCache) { // So, let's cache everything except exceptions foreach ($this->block->getChildNames() as $blockName) { if (in_array($blockName, $this->getExceptions())) { continue; } if ($this->layout->getBlock($blockName)) { $this->cacheBlock($this->layout->getBlock($blockName)); } } } if ($canCache) { try { $this->saveTags($this->tags); $this->saveLifetime($this->lifetime); $this->saveKey($this->key); } catch (\Exception $e) { return false; } } return true; } /** * Retrieve items that must not be cached * What actually are exception depends on how the caching engine uses them * * @return array */ public function getExceptions() { $exceptions = []; if ($sacData = $this->block->getData('soon_advancedcache')) { if (isset($sacData['exceptions'])) { $exceptions = explode(',', $sacData['exceptions']); foreach ($exceptions as $k => $exception) { $exceptions[$k] = trim($exception); } } } return $exceptions; } /** * Do we want to avoid caching current block * if it has exceptions defined in layout? * * @return bool */ public function preferExceptions() { return true; } /** * Add actual cache tags to the block data info * * @param array $tags */ public function saveTags(array $tags) { $this->block->setData('cache_tags', $tags); } /** * Add actual cache lifetime to the block data info * * @param $lifetime */ public function saveLifetime($lifetime) { $lifetime = ($lifetime === '0') ? false : $lifetime; $this->block->setData('cache_lifetime', $lifetime); } /** * Add actual cache key to the block data info * * @param $key */ public function saveKey($key) { $this->block->setData('cache_key', $key); } /** * Retrieve the block to cache * * @return BlockInterface|\Magento\Framework\View\Element\AbstractBlock */ public function getBlock() { return $this->block; } /** * Retrieve the current store id * * @return int */ public function getStoreId() { return $this->store->getStore()->getId(); } /** * Retrieve the request * * @return RequestInterface */ public function getRequest() { return $this->request; } /** * Retrieve HttpContext * * @return HttpContext */ public function getHttpContext() { return $this->httpContext; } /** * Retrieve Design * * @return DesignInterface */ public function getDesign() { return $this->design; } /** * Implode multidimensional array * * @param string $glue * @param array $array * @return bool|string */ public function implodeArray($glue, array $array) { $return = ''; foreach ($array as $key => $value) { if (is_array($value)) { $return .= $key . $glue . $this->implodeArray($glue, $value) . $glue; } else { $return .= $key . $glue . $value . $glue; } } $return = substr($return, 0, 0 - strlen($glue)); return $return; } }