vendor/twig/twig/src/Cache/FilesystemCache.php line 46

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of Twig.
  4.  *
  5.  * (c) Fabien Potencier
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Twig\Cache;
  11. /**
  12.  * Implements a cache on the filesystem.
  13.  *
  14.  * @author Andrew Tch <andrew@noop.lv>
  15.  */
  16. class FilesystemCache implements CacheInterface
  17. {
  18.     public const FORCE_BYTECODE_INVALIDATION 1;
  19.     private $directory;
  20.     private $options;
  21.     /**
  22.      * @param string $directory The root cache directory
  23.      * @param int    $options   A set of options
  24.      */
  25.     public function __construct($directory$options 0)
  26.     {
  27.         $this->directory rtrim($directory'\/').'/';
  28.         $this->options $options;
  29.     }
  30.     public function generateKey($name$className)
  31.     {
  32.         $hash hash(\PHP_VERSION_ID 80100 'sha256' 'xxh128'$className);
  33.         return $this->directory.$hash[0].$hash[1].'/'.$hash.'.php';
  34.     }
  35.     public function load($key)
  36.     {
  37.         if (file_exists($key)) {
  38.             @include_once $key;
  39.         }
  40.     }
  41.     public function write($key$content)
  42.     {
  43.         $dir = \dirname($key);
  44.         if (!is_dir($dir)) {
  45.             if (false === @mkdir($dir0777true)) {
  46.                 clearstatcache(true$dir);
  47.                 if (!is_dir($dir)) {
  48.                     throw new \RuntimeException(sprintf('Unable to create the cache directory (%s).'$dir));
  49.                 }
  50.             }
  51.         } elseif (!is_writable($dir)) {
  52.             throw new \RuntimeException(sprintf('Unable to write in the cache directory (%s).'$dir));
  53.         }
  54.         $tmpFile tempnam($dirbasename($key));
  55.         if (false !== @file_put_contents($tmpFile$content) && @rename($tmpFile$key)) {
  56.             @chmod($key0666 & ~umask());
  57.             if (self::FORCE_BYTECODE_INVALIDATION == ($this->options self::FORCE_BYTECODE_INVALIDATION)) {
  58.                 // Compile cached file into bytecode cache
  59.                 if (\function_exists('opcache_invalidate') && filter_var(ini_get('opcache.enable'), \FILTER_VALIDATE_BOOLEAN)) {
  60.                     @opcache_invalidate($keytrue);
  61.                 } elseif (\function_exists('apc_compile_file')) {
  62.                     apc_compile_file($key);
  63.                 }
  64.             }
  65.             return;
  66.         }
  67.         throw new \RuntimeException(sprintf('Failed to write cache file "%s".'$key));
  68.     }
  69.     public function getTimestamp($key)
  70.     {
  71.         if (!file_exists($key)) {
  72.             return 0;
  73.         }
  74.         return (int) @filemtime($key);
  75.     }
  76. }
  77. class_alias('Twig\Cache\FilesystemCache''Twig_Cache_Filesystem');