3 * Class Minify_Cache_File
7 class Minify_Cache_File {
9 public function __construct($path = '', $fileLocking = false)
14 $this->_locking = $fileLocking;
19 * Write data to cache.
21 * @param string $id cache id (e.g. a filename)
25 * @return bool success
27 public function store($id, $data)
29 $flag = $this->_locking
32 $file = $this->_path . '/' . $id;
33 if (! @file_put_contents($file, $data, $flag)) {
34 $this->_log("Minify_Cache_File: Write failed to '$file'");
37 if ($data !== $this->fetch($id)) {
39 $this->_log("Minify_Cache_File: Post-write read failed for '$file'");
46 * Get the size of a cache entry
48 * @param string $id cache id (e.g. a filename)
50 * @return int size in bytes
52 public function getSize($id)
54 return filesize($this->_path . '/' . $id);
58 * Does a valid cache entry exist?
60 * @param string $id cache id (e.g. a filename)
62 * @param int $srcMtime mtime of the original source file(s)
66 public function isValid($id, $srcMtime)
68 $file = $this->_path . '/' . $id;
69 return (is_file($file) && (filemtime($file) >= $srcMtime));
73 * Send the cached content to output
75 * @param string $id cache id (e.g. a filename)
77 public function display($id)
79 if ($this->_locking) {
80 $fp = fopen($this->_path . '/' . $id, 'rb');
86 readfile($this->_path . '/' . $id);
91 * Fetch the cached content
93 * @param string $id cache id (e.g. a filename)
97 public function fetch($id)
99 if ($this->_locking) {
100 $fp = fopen($this->_path . '/' . $id, 'rb');
102 $ret = stream_get_contents($fp);
107 return @file_get_contents($this->_path . '/' . $id);
112 * Fetch the cache path used
116 public function getPath()
122 * Get a usable temp directory
124 * Adapted from Solar/Dir.php
125 * @author Paul M. Jones <pmjones@solarphp.com>
126 * @license http://opensource.org/licenses/bsd-license.php BSD
127 * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
131 public static function tmp()
135 $tmp = function_exists('sys_get_temp_dir')
138 $tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
144 * Returns the OS-specific directory for temporary files
146 * @author Paul M. Jones <pmjones@solarphp.com>
147 * @license http://opensource.org/licenses/bsd-license.php BSD
148 * @link http://solarphp.com/trac/core/browser/trunk/Solar/Dir.php
152 protected static function _tmp()
154 // non-Windows system?
155 if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
156 $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
164 $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
169 $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
174 $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
178 // final fallback for Windows
179 return getenv('SystemRoot') . '\\temp';
183 * Send message to the Minify logger
187 protected function _log($msg)
189 Minify_Logger::log($msg);
192 private $_path = null;
193 private $_locking = null;