You are here

class TwigPhpStorageCache in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/lib/Drupal/Core/Template/TwigPhpStorageCache.php \Drupal\Core\Template\TwigPhpStorageCache

Provides an alternate cache storage for Twig using PhpStorage.

This class is designed to work on setups with multiple webheads using a local filesystem for the twig cache. When generating the cache key, a hash value depending on the enabled extensions is included. This prevents stale templates from being reused when twig extensions are enabled or disabled.

Hierarchy

  • class \Drupal\Core\Template\TwigPhpStorageCache implements \Drupal\Core\Template\Twig_CacheInterface

Expanded class hierarchy of TwigPhpStorageCache

See also

\Drupal\Core\DependencyInjection\Compiler\TwigExtensionPass

File

core/lib/Drupal/Core/Template/TwigPhpStorageCache.php, line 23
Contains \Drupal\Core\Template\TwigPhpStorageCache.

Namespace

Drupal\Core\Template
View source
class TwigPhpStorageCache implements \Twig_CacheInterface {

  /**
   * The cache object used for auto-refresh via mtime.
   *
   * @var \Drupal\Core\Cache\CacheBackendInterface
   */
  protected $cache;

  /**
   * The PhpStorage object used for storing the templates.
   *
   * @var \Drupal\Component\PhpStorage\PhpStorageInterface
   */
  protected $storage;

  /**
   * The template cache filename prefix.
   *
   * @var string
   */
  protected $templateCacheFilenamePrefix;

  /**
   * Store cache backend and other information internally.
   *
   * @param \Drupal\Core\Cache\CacheBackendInterface $cache
   *   The cache bin.
   * @param string $twig_extension_hash
   *   The Twig extension hash.
   */
  public function __construct(CacheBackendInterface $cache, $twig_extension_hash) {
    $this->cache = $cache;
    $this->templateCacheFilenamePrefix = $twig_extension_hash;
  }

  /**
   * Gets the PHP code storage object to use for the compiled Twig files.
   *
   * @return \Drupal\Component\PhpStorage\PhpStorageInterface
   */
  protected function storage() {
    if (!isset($this->storage)) {
      $this->storage = PhpStorageFactory::get('twig');
    }
    return $this->storage;
  }

  /**
   * {@inheritdoc}
   */
  public function generateKey($name, $className) {
    $hash = hash('sha256', $className);
    if (strpos($name, '{# inline_template_start #}') === 0) {

      // $name is an inline template, and can have characters that are not valid
      // for a filename. $hash is unique for each inline template so we just use
      // the generic name 'inline-template' here.
      $name = 'inline-template';
    }
    else {
      $name = basename($name);
    }

    // The first part is what is invalidated.
    return $this->templateCacheFilenamePrefix . '_' . $name . '_' . $hash;
  }

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

  /**
   * {@inheritdoc}
   */
  public function write($key, $content) {
    $this
      ->storage()
      ->save($key, $content);

    // Save the last mtime.
    $cid = 'twig:' . $key;
    $this->cache
      ->set($cid, REQUEST_TIME);
  }

  /**
   * {@inheritdoc}
   */
  public function getTimestamp($key) {
    $cid = 'twig:' . $key;
    if ($cache = $this->cache
      ->get($cid)) {
      return $cache->data;
    }
    else {
      return 0;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TwigPhpStorageCache::$cache protected property The cache object used for auto-refresh via mtime.
TwigPhpStorageCache::$storage protected property The PhpStorage object used for storing the templates.
TwigPhpStorageCache::$templateCacheFilenamePrefix protected property The template cache filename prefix.
TwigPhpStorageCache::generateKey public function
TwigPhpStorageCache::getTimestamp public function
TwigPhpStorageCache::load public function
TwigPhpStorageCache::storage protected function Gets the PHP code storage object to use for the compiled Twig files.
TwigPhpStorageCache::write public function
TwigPhpStorageCache::__construct public function Store cache backend and other information internally.