MODX Revolution PHP Cross Reference Content Management Systems

Source: /manager/min/lib/Minify/Cache/File.php - 199 lines - 4997 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * Class Minify_Cache_File  
   4   * @package Minify
   5   */
   6  
   7  class Minify_Cache_File {
   8      
   9      public function __construct($path = '', $fileLocking = false)
  10      {
  11          if (! $path) {
  12              $path = self::tmp();
  13          }
  14          $this->_locking = $fileLocking;
  15          $this->_path = $path;
  16      }
  17  
  18      /**
  19       * Write data to cache.
  20       *
  21       * @param string $id cache id (e.g. a filename)
  22       * 
  23       * @param string $data
  24       * 
  25       * @return bool success
  26       */
  27      public function store($id, $data)
  28      {
  29          $flag = $this->_locking
  30              ? LOCK_EX
  31              : null;
  32          $file = $this->_path . '/' . $id;
  33          if (is_file($file)) {
  34              @unlink($file);
  35          }
  36          if (! @file_put_contents($file, $data, $flag)) {
  37              $this->_log("Minify_Cache_File: Write failed to '$file'");
  38              return false;
  39          }
  40          // write control
  41          if ($data !== $this->fetch($id)) {
  42              @unlink($file);
  43              $this->_log("Minify_Cache_File: Post-write read failed for '$file'");
  44              return false;
  45          }
  46          return true;
  47      }
  48      
  49      /**
  50       * Get the size of a cache entry
  51       *
  52       * @param string $id cache id (e.g. a filename)
  53       * 
  54       * @return int size in bytes
  55       */
  56      public function getSize($id)
  57      {
  58          return filesize($this->_path . '/' . $id);
  59      }
  60      
  61      /**
  62       * Does a valid cache entry exist?
  63       *
  64       * @param string $id cache id (e.g. a filename)
  65       * 
  66       * @param int $srcMtime mtime of the original source file(s)
  67       * 
  68       * @return bool exists
  69       */
  70      public function isValid($id, $srcMtime)
  71      {
  72          $file = $this->_path . '/' . $id;
  73          return (is_file($file) && (filemtime($file) >= $srcMtime));
  74      }
  75      
  76      /**
  77       * Send the cached content to output
  78       *
  79       * @param string $id cache id (e.g. a filename)
  80       */
  81      public function display($id)
  82      {
  83          if ($this->_locking) {
  84              $fp = fopen($this->_path . '/' . $id, 'rb');
  85              flock($fp, LOCK_SH);
  86              fpassthru($fp);
  87              flock($fp, LOCK_UN);
  88              fclose($fp);
  89          } else {
  90              readfile($this->_path . '/' . $id);            
  91          }
  92      }
  93      
  94      /**
  95       * Fetch the cached content
  96       *
  97       * @param string $id cache id (e.g. a filename)
  98       * 
  99       * @return string
 100       */
 101      public function fetch($id)
 102      {
 103          if ($this->_locking) {
 104              $fp = fopen($this->_path . '/' . $id, 'rb');
 105              flock($fp, LOCK_SH);
 106              $ret = stream_get_contents($fp);
 107              flock($fp, LOCK_UN);
 108              fclose($fp);
 109              return $ret;
 110          } else {
 111              return file_get_contents($this->_path . '/' . $id);
 112          }
 113      }
 114      
 115      /**
 116       * Fetch the cache path used
 117       *
 118       * @return string
 119       */
 120      public function getPath()
 121      {
 122          return $this->_path;
 123      }
 124  
 125      /**
 126       * Get a usable temp directory
 127       *
 128       * Adapted from Solar/Dir.php
 129       * @author Paul M. Jones <pmjones@solarphp.com>
 130       * @license http://opensource.org/licenses/bsd-license.php BSD
 131       * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
 132       *
 133       * @return string
 134       */
 135      public static function tmp()
 136      {
 137          static $tmp = null;
 138          if (! $tmp) {
 139              $tmp = function_exists('sys_get_temp_dir')
 140                  ? sys_get_temp_dir()
 141                  : self::_tmp();
 142              $tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
 143          }
 144          return $tmp;
 145      }
 146  
 147      /**
 148       * Returns the OS-specific directory for temporary files
 149       *
 150       * @author Paul M. Jones <pmjones@solarphp.com>
 151       * @license http://opensource.org/licenses/bsd-license.php BSD
 152       * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
 153       *
 154       * @return string
 155       */
 156      protected static function _tmp()
 157      {
 158          // non-Windows system?
 159          if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
 160              $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
 161              if ($tmp) {
 162                  return $tmp;
 163              } else {
 164                  return '/tmp';
 165              }
 166          }
 167          // Windows 'TEMP'
 168          $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
 169          if ($tmp) {
 170              return $tmp;
 171          }
 172          // Windows 'TMP'
 173          $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
 174          if ($tmp) {
 175              return $tmp;
 176          }
 177          // Windows 'windir'
 178          $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
 179          if ($tmp) {
 180              return $tmp;
 181          }
 182          // final fallback for Windows
 183          return getenv('SystemRoot') . '\\temp';
 184      }
 185  
 186      /**
 187       * Send message to the Minify logger
 188       * @param string $msg
 189       * @return null
 190       */
 191      protected function _log($msg)
 192      {
 193          require_once  'Minify/Logger.php';
 194          Minify_Logger::log($msg);
 195      }
 196      
 197      private $_path = null;
 198      private $_locking = null;
 199  }

title

Description

title

Description

title

Description

title

title

Body