You are here

class Storage in Express 8

Theme Storage.

A hybrid storage solution that utilizes the cache system for complex and expensive operations performed by a [base] theme.

Instead of using multiple cache identifiers, which increases the number of database calls, this storage only executes a single cache call and stores individual entries in memory as an associative array.

It also tracks when the data has been modified so it can be saved back to cache before the system fully shuts down.

This storage object can be used in `foreach` loops.

Hierarchy

Expanded class hierarchy of Storage

See also

\Drupal\bootstrap\Utility\StorageItem

1 file declares its use of Storage
Theme.php in themes/contrib/bootstrap/src/Theme.php
Contains \Drupal\bootstrap.

File

themes/contrib/bootstrap/src/Utility/Storage.php, line 32
Contains \Drupal\bootstrap\Utility\Storage.

Namespace

Drupal\bootstrap\Utility
View source
class Storage extends MemoryStorage implements \Iterator {

  /**
   * The bin (table) data should be stored in (not prefixed with "cache_").
   *
   * @var string
   */
  protected $bin;

  /**
   * Flag determining whether or not the cache should be saved to the database.
   *
   * @var bool
   */
  protected $changed;

  /**
   * The cache identifier.
   *
   * @var string
   */
  protected $cid;

  /**
   * Indicates when the cache should expire.
   *
   * @var string
   */
  protected $expire;

  /**
   * Flag determining whether or not object has been initialized yet.
   *
   * @var bool
   */
  protected $initialized = FALSE;

  /**
   * Tags to associate with the cached data so it can be properly invalidated.
   *
   * @var string
   */
  protected $tags;

  /**
   * {@inheritdoc}
   */
  public function __construct($cid, $bin = 'default', $expire = Cache::PERMANENT, $tags = [
    Bootstrap::CACHE_TAG,
  ]) {
    $this->cid = "theme_registry:storage:{$cid}";
    $this->bin = $bin;
    $this->changed = FALSE;
    $this->expire = $expire;
    $this->tags = $tags;

    // Register the cache object to save, if it's needed, on shutdown.
    drupal_register_shutdown_function([
      $this,
      'save',
    ]);

    // Retrieve the cached data.
    $data = ($cached = \Drupal::cache($bin)
      ->get($this->cid)) && !empty($cached->data) ? $cached->data : [];

    // Set the data.
    $this
      ->setMultiple($data);

    // Cache has been initialized.
    $this->initialized = TRUE;
  }

  /**
   * Notifies the object that data has changed.
   */
  public function changed() {
    if ($this->initialized) {
      $this->changed = TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function current() {
    return current($this->data);
  }

  /**
   * {@inheritdoc}
   */
  public function delete($key) {
    parent::delete($key);
    $this
      ->changed();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {
    parent::deleteAll();
    $this
      ->changed();
  }

  /**
   * {@inheritdoc}
   */
  public function deleteMultiple(array $keys) {
    parent::deleteMultiple($keys);
    $this
      ->changed();
  }

  /**
   * {@inheritdoc}
   */
  public function getAll($arrays = TRUE) {
    $data = $this->data;
    if ($arrays) {
      foreach ($data as $key => $value) {
        if ($value instanceof StorageItem) {
          $data[$key] = $value
            ->getAll();
        }
      }
    }
    return $data;
  }

  /**
   * Determines if the cache is empty.
   *
   * @return bool
   *   TRUE or FALSE
   */
  public function isEmpty() {
    return empty($this->data);
  }

  /**
   * {@inheritdoc}
   */
  public function key() {
    return key($this->data);
  }

  /**
   * {@inheritdoc}
   */
  public function next() {
    return next($this->data);
  }

  /**
   * {@inheritdoc}
   */
  public function rename($key, $new_key) {
    parent::rename($key, $new_key);
    $this
      ->changed();
  }

  /**
   * {@inheritdoc}
   */
  public function rewind() {
    return reset($this->data);
  }

  /**
   * Saves the data back to the database, if necessary, on shutdown.
   *
   * This method is automatically invoked during PHP shutdown.
   *
   * @internal
   *
   * @see \Drupal\bootstrap\Utility\Storage::__construct
   */
  public function save() {
    if ($this->changed) {
      \Drupal::cache($this->bin)
        ->set($this->cid, $this
        ->getAll(), $this->expire, $this->tags);
    }
  }

  /**
   * {@inheritdoc}
   */
  public function set($key, $value) {
    if (is_array($value)) {
      $value = new StorageItem($value, $this);
    }
    parent::set($key, $value);
    $this
      ->changed();
  }

  /**
   * {@inheritdoc}
   */
  public function setIfNotExists($key, $value) {
    if (!isset($this->data[$key])) {
      if (is_array($value)) {
        $value = new StorageItem($value, $this);
      }
      $this->data[$key] = $value;
      $this
        ->changed();
      return TRUE;
    }
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setMultiple(array $data) {
    foreach ($data as $key => $value) {
      if (is_array($value)) {
        $data[$key] = new StorageItem($value, $this);
      }
    }
    parent::setMultiple($data);
    $this
      ->changed();
  }

  /**
   * {@inheritdoc}
   */
  public function valid() {
    return key($this->data) !== NULL;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MemoryStorage::$data protected property The actual storage of key-value pairs.
MemoryStorage::get public function Returns the stored value for a given key. Overrides StorageBase::get
MemoryStorage::getMultiple public function Returns the stored key/value pairs for a given set of keys. Overrides KeyValueStoreInterface::getMultiple
MemoryStorage::has public function Returns whether a given key exists in the store. Overrides KeyValueStoreInterface::has
Storage::$bin protected property The bin (table) data should be stored in (not prefixed with "cache_").
Storage::$changed protected property Flag determining whether or not the cache should be saved to the database.
Storage::$cid protected property The cache identifier.
Storage::$expire protected property Indicates when the cache should expire.
Storage::$initialized protected property Flag determining whether or not object has been initialized yet.
Storage::$tags protected property Tags to associate with the cached data so it can be properly invalidated.
Storage::changed public function Notifies the object that data has changed.
Storage::current public function
Storage::delete public function Deletes an item from the key/value store. Overrides MemoryStorage::delete
Storage::deleteAll public function Deletes all items from the key/value store. Overrides MemoryStorage::deleteAll
Storage::deleteMultiple public function Deletes multiple items from the key/value store. Overrides MemoryStorage::deleteMultiple
Storage::getAll public function Returns all stored key/value pairs in the collection. Overrides MemoryStorage::getAll
Storage::isEmpty public function Determines if the cache is empty.
Storage::key public function
Storage::next public function
Storage::rename public function Renames a key. Overrides MemoryStorage::rename
Storage::rewind public function
Storage::save public function Saves the data back to the database, if necessary, on shutdown.
Storage::set public function Saves a value for a given key. Overrides MemoryStorage::set
Storage::setIfNotExists public function Saves a value for a given key if it does not exist yet. Overrides MemoryStorage::setIfNotExists
Storage::setMultiple public function Saves key/value pairs. Overrides MemoryStorage::setMultiple
Storage::valid public function
Storage::__construct public function Overrides StorageBase::__construct
StorageBase::$collection protected property The name of the collection holding key and value pairs.
StorageBase::getCollectionName public function Returns the name of this collection. Overrides KeyValueStoreInterface::getCollectionName