You are here

abstract class crumbs_Container_AbstractLazyDataCached in Crumbs, the Breadcrumbs suite 7.2

Hierarchy

Expanded class hierarchy of crumbs_Container_AbstractLazyDataCached

File

lib/Container/AbstractLazyDataCached.php, line 3

View source
abstract class crumbs_Container_AbstractLazyDataCached {

  /**
   * Lazy-initialized data.
   *
   * @var mixed[]
   */
  private $data = array();

  /**
   * Keys whose data should be remembered in persistent cached.
   *
   * @var true[]
   *   Format: $[$key] = TRUE.
   */
  private $keysToCache = array();

  /**
   * The constructor.
   */
  function __construct() {
    $this->keysToCache = array_fill_keys($this
      ->keysToCache(), TRUE);
  }

  /**
   * @return string[]
   */
  protected abstract function keysToCache();

  /**
   * Flush cached data.
   */
  function flushCaches() {
    $this->data = array();
    cache_clear_all('crumbs:', 'cache', TRUE);
  }

  /**
   * @param string $key
   *
   * @return mixed
   * @throws Exception
   */
  function __get($key) {
    if (array_key_exists($key, $this->data)) {
      return $this->data[$key];
    }
    return $this->data[$key] = empty($this->keysToCache[$key]) ? $this
      ->get($key) : $this
      ->getCached($key);
  }

  /**
   * Load data from persistent cache, or calls $this->get() if not in cache.
   *
   * @param string $key
   *
   * @return mixed|false
   *   Any value except NULL.
   *
   * @throws Exception
   */
  private function getCached($key) {
    $cache = cache_get("crumbs:{$key}");
    if (isset($cache->data)) {

      // We do the serialization manually,
      // to prevent Drupal from intercepting exceptions.
      // However, from previous versions we might still have non-serialized data.
      return is_array($cache->data) ? $cache->data : unserialize($cache->data);
    }
    $data = $this
      ->get($key);
    if (!is_array($data)) {
      throw new Exception("Only arrays can be cached in crumbs_CachedLazyPluginInfo.");
    }
    cache_set("crumbs:{$key}", serialize($data));
    return $data;
  }

  /**
   * Calculate a piece of data by using methods defined in a child class.
   *
   * @param string $key
   *
   * @return mixed|false
   *   Any value except NULL.
   *
   * @throws Exception
   */
  private function get($key) {
    $method = 'get_' . $key;
    if (!method_exists($this, $method)) {
      $class = get_class($this);
      throw new Exception("Key '{$key}' not supported in {$class}.");
    }
    $result = $this
      ->{$method}($this);
    return isset($result) ? $result : FALSE;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
crumbs_Container_AbstractLazyDataCached::$data private property Lazy-initialized data.
crumbs_Container_AbstractLazyDataCached::$keysToCache private property Keys whose data should be remembered in persistent cached. 1
crumbs_Container_AbstractLazyDataCached::flushCaches function Flush cached data.
crumbs_Container_AbstractLazyDataCached::get private function Calculate a piece of data by using methods defined in a child class.
crumbs_Container_AbstractLazyDataCached::getCached private function Load data from persistent cache, or calls $this->get() if not in cache.
crumbs_Container_AbstractLazyDataCached::keysToCache abstract protected function 1
crumbs_Container_AbstractLazyDataCached::__construct function The constructor.
crumbs_Container_AbstractLazyDataCached::__get function