class CacheDecorator

Enables static and persistent caching of discovered plugin definitions.

Hierarchy

Expanded class hierarchy of CacheDecorator

7 files declare their use of CacheDecorator
CacheDecoratorTest.php in drupal/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php
Contains \Drupal\system\Tests\Plugin\CacheDecoratorTest.
FormatterPluginManager.php in drupal/core/modules/field/lib/Drupal/field/Plugin/Type/Formatter/FormatterPluginManager.php
Definition of Drupal\field\Plugin\Type\Formatter\FormatterPluginManager..
JoinManager.php in drupal/core/modules/views/lib/Drupal/views/Plugin/Type/JoinManager.php
Definition of Drupal\views\Plugin\Type\JoinManager.
PluginManager.php in drupal/core/modules/views/lib/Drupal/views/Plugin/Type/PluginManager.php
Definition of Drupal\views\Plugin\Type\PluginManager.
TypedDataManager.php in drupal/core/lib/Drupal/Core/TypedData/TypedDataManager.php
Definition of Drupal\Core\TypedData\TypedDataManager.

... See full list

1 string reference to 'CacheDecorator'
CacheDecoratorTest::getInfo in drupal/core/modules/system/lib/Drupal/system/Tests/Plugin/CacheDecoratorTest.php

File

drupal/core/lib/Drupal/Core/Plugin/Discovery/CacheDecorator.php, line 16
Definition of Drupal\Core\Plugin\Discovery\CacheDecorator.

Namespace

Drupal\Core\Plugin\Discovery
View source
class CacheDecorator implements CachedDiscoveryInterface {

  /**
   * The cache key used to store the definition list.
   *
   * @var string
   */
  protected $cacheKey;

  /**
   * The cache bin used to store the definition list.
   *
   * @var string
   */
  protected $cacheBin;

  /**
   * The plugin definitions of the decorated discovery class.
   *
   * @var array
   */
  protected $definitions;

  /**
   * The Discovery object being decorated.
   *
   * @var Drupal\Component\Plugin\Discovery\DiscoveryInterface
   */
  protected $decorated;

  /**
   * Constructs a Drupal\Core\Plugin\Discovery\CacheDecorator object.
   *
   * It uses the DiscoveryInterface object it should decorate.
   *
   * @param Drupal\Component\Plugin\Discovery\DiscoveryInterface $decorated
   *   The object implementing DiscoveryInterface that is being decorated.
   * @param string $cache_key
   *   The cache identifier used for storage of the definition list.
   * @param string $cache_bin
   *   The cache bin used for storage and retrieval of the definition list.
   */
  public function __construct(DiscoveryInterface $decorated, $cache_key, $cache_bin = 'cache') {
    $this->decorated = $decorated;
    $this->cacheKey = $cache_key;
    $this->cacheBin = $cache_bin;
  }

  /**
   * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition().
   */
  public function getDefinition($plugin_id) {
    $definitions = $this
      ->getDefinitions();
    return isset($definitions[$plugin_id]) ? $definitions[$plugin_id] : NULL;
  }

  /**
   * Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions().
   */
  public function getDefinitions() {
    $definitions = $this
      ->getCachedDefinitions();
    if (!isset($definitions)) {
      $definitions = $this->decorated
        ->getDefinitions();
      $this
        ->setCachedDefinitions($definitions);
    }
    return $definitions;
  }

  /**
   * Returns the cached plugin definitions of the decorated discovery class.
   *
   * @return mixed
   *   On success this will return an array of plugin definitions. On failure
   *   this should return NULL, indicating to other methods that this has not
   *   yet been defined. Success with no values should return as an empty array
   *   and would actually be returned by the getDefinitions() method.
   */
  protected function getCachedDefinitions() {
    if (!isset($this->definitions) && isset($this->cacheKey) && ($cache = cache($this->cacheBin)
      ->get($this->cacheKey))) {
      $this->definitions = $cache->data;
    }
    return $this->definitions;
  }

  /**
   * Sets a cache of plugin definitions for the decorated discovery class.
   *
   * @param array $definitions
   *   List of definitions to store in cache.
   */
  protected function setCachedDefinitions($definitions) {
    if (isset($this->cacheKey)) {
      cache($this->cacheBin)
        ->set($this->cacheKey, $definitions);
    }
    $this->definitions = $definitions;
  }

  /**
   * Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::clearCachedDefinitions().
   */
  public function clearCachedDefinitions() {
    if (isset($this->cacheKey)) {
      cache($this->cacheBin)
        ->delete($this->cacheKey);
    }
    $this->definitions = NULL;
  }

  /**
   * Passes through all unknown calls onto the decorated object.
   */
  public function __call($method, $args) {
    return call_user_func_array(array(
      $this->decorated,
      $method,
    ), $args);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
CacheDecorator::$cacheBin protected property The cache bin used to store the definition list.
CacheDecorator::$cacheKey protected property The cache key used to store the definition list.
CacheDecorator::$decorated protected property The Discovery object being decorated.
CacheDecorator::$definitions protected property The plugin definitions of the decorated discovery class.
CacheDecorator::clearCachedDefinitions public function Implements \Drupal\Component\Plugin\Discovery\CachedDiscoveryInterface::clearCachedDefinitions(). Overrides CachedDiscoveryInterface::clearCachedDefinitions
CacheDecorator::getCachedDefinitions protected function Returns the cached plugin definitions of the decorated discovery class.
CacheDecorator::getDefinition public function Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinition(). Overrides DiscoveryInterface::getDefinition
CacheDecorator::getDefinitions public function Implements Drupal\Component\Plugin\Discovery\DicoveryInterface::getDefinitions(). Overrides DiscoveryInterface::getDefinitions
CacheDecorator::setCachedDefinitions protected function Sets a cache of plugin definitions for the decorated discovery class.
CacheDecorator::__call public function Passes through all unknown calls onto the decorated object.
CacheDecorator::__construct public function Constructs a Drupal\Core\Plugin\Discovery\CacheDecorator object.