shop.balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

Filesystem.php (2619B)


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