You are here

public function D8CacheAttachmentsCollector::set in Drupal 8 Cache Backport 7

Overrides D8Cache::set

File

./d8cache-ac.cache.inc, line 129

Class

D8CacheAttachmentsCollector
Special cache backend that tracks attachments.

Code

public function set($cid, $data, $expire = CACHE_PERMANENT, $tags = array()) {

  // Fall back to regular D8Cache if we are not in a valid state.
  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]);

    // Reset the attachments for re-use.
    $attachments_collector
      ->reset();
    $attachments_collector->count--;
    if ($attachments_collector->count == 0) {
      $this->currentAttachmentsCollector = $attachments_collector->previousCollector;
    }
  }

  // Create a pseudo render array.
  $data = array(
    '#d8cache_data' => $data,
    '#attached' => $attachments,
  );
  parent::set($cid, $data, $expire, $tags);
}