initial commit
[namibia] / public / min / lib / Minify / Cache / File.php
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 (! @file_put_contents($file, $data, $flag)) {
34             $this->_log("Minify_Cache_File: Write failed to '$file'");
35         }
36         // write control
37         if ($data !== $this->fetch($id)) {
38             @unlink($file);
39             $this->_log("Minify_Cache_File: Post-write read failed for '$file'");
40             return false;
41         }
42         return true;
43     }
44
45     /**
46      * Get the size of a cache entry
47      *
48      * @param string $id cache id (e.g. a filename)
49      *
50      * @return int size in bytes
51      */
52     public function getSize($id)
53     {
54         return filesize($this->_path . '/' . $id);
55     }
56
57     /**
58      * Does a valid cache entry exist?
59      *
60      * @param string $id cache id (e.g. a filename)
61      *
62      * @param int $srcMtime mtime of the original source file(s)
63      *
64      * @return bool exists
65      */
66     public function isValid($id, $srcMtime)
67     {
68         $file = $this->_path . '/' . $id;
69         return (is_file($file) && (filemtime($file) >= $srcMtime));
70     }
71
72     /**
73      * Send the cached content to output
74      *
75      * @param string $id cache id (e.g. a filename)
76      */
77     public function display($id)
78     {
79         if ($this->_locking) {
80             $fp = fopen($this->_path . '/' . $id, 'rb');
81             flock($fp, LOCK_SH);
82             fpassthru($fp);
83             flock($fp, LOCK_UN);
84             fclose($fp);
85         } else {
86             readfile($this->_path . '/' . $id);
87         }
88     }
89
90         /**
91      * Fetch the cached content
92      *
93      * @param string $id cache id (e.g. a filename)
94      *
95      * @return string
96      */
97     public function fetch($id)
98     {
99         if ($this->_locking) {
100             $fp = fopen($this->_path . '/' . $id, 'rb');
101             flock($fp, LOCK_SH);
102             $ret = stream_get_contents($fp);
103             flock($fp, LOCK_UN);
104             fclose($fp);
105             return $ret;
106         } else {
107             return @file_get_contents($this->_path . '/' . $id);
108         }
109     }
110
111     /**
112      * Fetch the cache path used
113      *
114      * @return string
115      */
116     public function getPath()
117     {
118         return $this->_path;
119     }
120
121     /**
122      * Get a usable temp directory
123      *
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
128      *
129      * @return string
130      */
131     public static function tmp()
132     {
133         static $tmp = null;
134         if (! $tmp) {
135             $tmp = function_exists('sys_get_temp_dir')
136                 ? sys_get_temp_dir()
137                 : self::_tmp();
138             $tmp = rtrim($tmp, DIRECTORY_SEPARATOR);
139         }
140         return $tmp;
141     }
142
143     /**
144      * Returns the OS-specific directory for temporary files
145      *
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
149      *
150      * @return string
151      */
152     protected static function _tmp()
153     {
154         // non-Windows system?
155         if (strtolower(substr(PHP_OS, 0, 3)) != 'win') {
156             $tmp = empty($_ENV['TMPDIR']) ? getenv('TMPDIR') : $_ENV['TMPDIR'];
157             if ($tmp) {
158                 return $tmp;
159             } else {
160                 return '/tmp';
161             }
162         }
163         // Windows 'TEMP'
164         $tmp = empty($_ENV['TEMP']) ? getenv('TEMP') : $_ENV['TEMP'];
165         if ($tmp) {
166             return $tmp;
167         }
168         // Windows 'TMP'
169         $tmp = empty($_ENV['TMP']) ? getenv('TMP') : $_ENV['TMP'];
170         if ($tmp) {
171             return $tmp;
172         }
173         // Windows 'windir'
174         $tmp = empty($_ENV['windir']) ? getenv('windir') : $_ENV['windir'];
175         if ($tmp) {
176             return $tmp;
177         }
178         // final fallback for Windows
179         return getenv('SystemRoot') . '\\temp';
180     }
181
182     /**
183      * Send message to the Minify logger
184      * @param string $msg
185      * @return null
186      */
187     protected function _log($msg)
188     {
189         Minify_Logger::log($msg);
190     }
191
192     private $_path = null;
193     private $_locking = null;
194 }