You are here

public function Twig_Cache_Filesystem::write in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/twig/twig/lib/Twig/Cache/Filesystem.php \Twig_Cache_Filesystem::write()

Writes the compiled template to cache.

Parameters

string $key The cache key:

string $content The template representation as a PHP class:

Overrides Twig_CacheInterface::write

File

vendor/twig/twig/lib/Twig/Cache/Filesystem.php, line 55

Class

Twig_Cache_Filesystem
Implements a cache on the filesystem.

Code

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));
}