d8cache-ac.cache.inc in Drupal 8 Cache Backport 7
File
d8cache-ac.cache.inc
View source
<?php
require_once __DIR__ . '/d8cache.cache.inc';
require_once __DIR__ . '/includes/core-attachments-collector.inc';
class D8CacheDrupalAttachmentsCollector extends DrupalAttachmentsCollector {
public $count = 0;
public $previousCollector = NULL;
public function reset() {
$this->attachments = array();
}
}
class D8CacheAttachmentsCollector extends D8Cache {
protected $attachmentsCollectors;
protected $currentAttachmentsCollector;
protected $isValid;
public function __construct($bin) {
$this->isValid = TRUE;
if (!function_exists('drupal_process_attached')) {
$this->isValid = FALSE;
$t = get_t();
drupal_set_message($t('D8CacheAttachmentsCollector is not designed for early page cache! Please switch cache_page to D8Cache in settings.php!'), 'error');
}
parent::__construct($bin);
}
public function getMultiple(&$cids, $allow_invalid = FALSE) {
$cache = parent::getMultiple($cids, $allow_invalid);
if (!$this->isValid) {
return $cache;
}
foreach ($cache as $cid => $item) {
if (is_array($item->data) && isset($item->data['#d8cache_data'])) {
drupal_process_attached($item->data);
$cache[$cid]->data = $item->data['#d8cache_data'];
}
}
if (empty($cids)) {
return $cache;
}
$attachments_collector = new D8CacheDrupalAttachmentsCollector();
$attachments_collector->count = count($cids);
$attachments_collector->previousCollector = $this->currentAttachmentsCollector;
$this->currentAttachmentsCollector = $attachments_collector;
foreach ($cids as $cid) {
$this->attachmentsCollectors[$cid] = $attachments_collector;
}
return $cache;
}
public function set($cid, $data, $expire = CACHE_PERMANENT, $tags = array()) {
if (!$this->isValid) {
parent::set($cid, $data, $expire, $tags);
return;
}
$attachments = array();
if (isset($this->attachmentsCollectors[$cid])) {
$attachments_collector = $this->attachmentsCollectors[$cid];
$attachments = $attachments_collector
->getAttachments();
unset($this->attachmentsCollectors[$cid]);
$attachments_collector
->reset();
$attachments_collector->count--;
if ($attachments_collector->count == 0) {
$this->currentAttachmentsCollector = $attachments_collector->previousCollector;
}
}
$data = array(
'#d8cache_data' => $data,
'#attached' => $attachments,
);
parent::set($cid, $data, $expire, $tags);
}
public function resetCurrentAttachmentsCollector() {
if ($this->currentAttachmentsCollector) {
$this->currentAttachmentsCollector
->reset();
}
}
}