RenderCacheBackendAdapter.php in Render cache 7.2
File
src/Cache/RenderCacheBackendAdapter.php
View source
<?php
namespace Drupal\render_cache\Cache;
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Render\Element;
use RenderCache;
class RenderCacheBackendAdapter implements RenderCacheBackendAdapterInterface {
protected $renderStack;
public function __construct(RenderStackInterface $render_stack) {
$this->renderStack = $render_stack;
}
public function cache($bin = 'cache') {
return _cache_get_object($bin);
}
public function get(array $cache_info) {
$id = 42;
$cache_info_map = array(
$id => $cache_info,
);
$build = $this
->getMultiple($cache_info_map);
if (!empty($build[$id])) {
return $build[$id];
}
return array();
}
public function getMultiple(array $cache_info_map) {
$build = array();
$cid_map_per_bin = array();
foreach ($cache_info_map as $id => $cache_info) {
$bin = isset($cache_info['bin']) ? $cache_info['bin'] : 'cache';
$cid = $this
->getCacheId($cache_info);
$cid_map_per_bin[$bin][$cid] = $id;
}
foreach ($cid_map_per_bin as $bin => $cid_map) {
$cids = array_filter(array_keys($cid_map));
if (!empty($cids)) {
$cached = $this
->cache($bin)
->getMultiple($cids);
foreach ($cached as $cid => $cache) {
if (!$cache) {
continue;
}
$id = $cid_map[$cid];
$render = $this->renderStack
->convertRenderArrayFromD7($cache->data);
if (!$this
->validate($render)) {
$cache_strategy = $cache_info_map[$id]['render_cache_cache_strategy'];
if ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_LATE_RENDER) {
$this
->cache($bin)
->clear($cid);
}
continue;
}
$build[$id] = $render;
}
}
}
return $build;
}
public function set(array &$render, array $cache_info) {
$cid = $this
->getCacheId($cache_info);
$bin = 'cache';
if (isset($cache_info['bin'])) {
$bin = $cache_info['bin'];
}
$expire = RenderCache::CACHE_PERMANENT;
if (isset($cache_info['expire'])) {
$expire = $cache_info['expire'];
}
$cache_strategy = $cache_info['render_cache_cache_strategy'];
$properties = $this
->preserveProperties($render, $cache_info);
if (!empty($cache_info['render_cache_preserve_original'])) {
$properties['#render_cache_original'] = $render;
}
if ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER) {
list($markup, $original) = $this->renderStack
->render($render);
if (!empty($cache_info['render_cache_preserve_original'])) {
$properties['#render_cache_original'] = $original;
}
}
$full_render = array();
$full_render['#cache'] = $cache_info;
$full_render['render'] =& $render;
$assets = $this->renderStack
->collectAndRemoveAssets($full_render);
$render = NestedArray::mergeDeep($render, $assets);
$data = $this->renderStack
->convertRenderArrayToD7($render);
$data['#attached']['render_cache'] += $properties;
if ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_NO_RENDER) {
if ($cid) {
$this
->cache($bin)
->set($cid, $data, $expire);
}
}
elseif ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_DIRECT_RENDER) {
$attached = $this->renderStack
->collectAttached($data);
$data = array();
$data['#markup'] =& $markup;
$data['#attached'] = $attached;
if ($cid) {
$this
->cache($bin)
->set($cid, $data, $expire);
}
$render = $this->renderStack
->convertRenderArrayFromD7($data);
}
elseif ($cache_strategy == RenderCache::RENDER_CACHE_STRATEGY_LATE_RENDER) {
if ($cid) {
$render['#cache']['cid'] = $cid;
}
else {
unset($render['#cache']['cid']);
unset($render['#cache']['keys']);
}
$render['#attached']['render_cache'] = $data['#attached']['render_cache'];
}
else {
throw new \RunTimeException('Unknown caching strategy passed.');
}
}
public function setMultiple(array &$build, array $cache_info_map) {
foreach (Element::children($build) as $id) {
$this
->set($build[$id], $cache_info_map[$id]);
}
}
protected function getCacheId(array $cache_info) {
return $cache_info['cid'];
}
protected function validate(array $render) {
return TRUE;
}
protected function preserveProperties(array $render, array $cache_info) {
$properties = array();
if (empty($cache_info['render_cache_preserve_properties']) || !is_array($cache_info['render_cache_preserve_properties'])) {
return $properties;
}
foreach ($cache_info['render_cache_preserve_properties'] as $key) {
if (isset($render[$key])) {
$properties[$key] = $render[$key];
}
}
return $properties;
}
}