You are here

class Twig_Cache_Filesystem in Translation template extractor 7.3

Implements a cache on the filesystem.

@author Andrew Tch <andrew@noop.lv>

Hierarchy

Expanded class hierarchy of Twig_Cache_Filesystem

File

vendor/Twig/Cache/Filesystem.php, line 17

View source
class Twig_Cache_Filesystem implements Twig_CacheInterface {
  const FORCE_BYTECODE_INVALIDATION = 1;
  private $directory;
  private $options;

  /**
   * @param $directory string The root cache directory
   * @param $options   int    A set of options
   */
  public function __construct($directory, $options = 0) {
    $this->directory = $directory;
    $this->options = $options;
  }

  /**
   * {@inheritdoc}
   */
  public function generateKey($name, $className) {
    $hash = hash('sha256', $className);
    return $this->directory . '/' . $hash[0] . $hash[1] . '/' . $hash . '.php';
  }

  /**
   * {@inheritdoc}
   */
  public function load($key) {
    @(include_once $key);
  }

  /**
   * {@inheritdoc}
   */
  public function write($key, $content) {
    $dir = dirname($key);
    if (!is_dir($dir)) {
      if (false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
        throw new RuntimeException(sprintf('Unable to create the cache directory (%s).', $dir));
      }
    }
    elseif (!is_writable($dir)) {
      throw new RuntimeException(sprintf('Unable to write in the cache directory (%s).', $dir));
    }
    $tmpFile = tempnam($dir, basename($key));
    if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $key)) {
      @chmod($key, 0666 & ~umask());
      if (self::FORCE_BYTECODE_INVALIDATION == ($this->options & self::FORCE_BYTECODE_INVALIDATION)) {

        // Compile cached file into bytecode cache
        if (function_exists('opcache_invalidate')) {
          opcache_invalidate($key, true);
        }
        elseif (function_exists('apc_compile_file')) {
          apc_compile_file($key);
        }
      }
      return;
    }
    throw new RuntimeException(sprintf('Failed to write cache file "%s".', $key));
  }

  /**
   * {@inheritdoc}
   */
  public function getTimestamp($key) {
    if (!file_exists($key)) {
      return 0;
    }
    return (int) @filemtime($key);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Twig_Cache_Filesystem::$directory private property
Twig_Cache_Filesystem::$options private property
Twig_Cache_Filesystem::FORCE_BYTECODE_INVALIDATION constant
Twig_Cache_Filesystem::generateKey public function Generates a cache key for the given template class name. Overrides Twig_CacheInterface::generateKey
Twig_Cache_Filesystem::getTimestamp public function Returns the modification timestamp of a key. Overrides Twig_CacheInterface::getTimestamp
Twig_Cache_Filesystem::load public function Loads a template from the cache. Overrides Twig_CacheInterface::load
Twig_Cache_Filesystem::write public function Writes the compiled template to cache. Overrides Twig_CacheInterface::write
Twig_Cache_Filesystem::__construct public function