]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Minify/extlib/minify/min/lib/Minify/Cache/File.php
Added minify plugin
[quix0rs-gnu-social.git] / plugins / Minify / extlib / minify / 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             require_once 'Solar/Dir.php';
13             $path = rtrim(Solar_Dir::tmp(), DIRECTORY_SEPARATOR);
14         }\r
15         $this->_locking = $fileLocking;
16         $this->_path = $path;
17     }
18     
19     /**
20      * Write data to cache.
21      *
22      * @param string $id cache id (e.g. a filename)
23      * 
24      * @param string $data
25      * 
26      * @return bool success
27      */
28     public function store($id, $data)
29     {
30         $flag = $this->_locking\r
31             ? LOCK_EX\r
32             : null;\r
33         if (is_file($this->_path . '/' . $id)) {\r
34             @unlink($this->_path . '/' . $id);\r
35         }\r
36         if (! @file_put_contents($this->_path . '/' . $id, $data, $flag)) {\r
37             return false;\r
38         }
39         // write control\r
40         if ($data !== $this->fetch($id)) {\r
41             @unlink($file);\r
42             return false;\r
43         }\r
44         return true;
45     }
46     
47     /**
48      * Get the size of a cache entry
49      *
50      * @param string $id cache id (e.g. a filename)
51      * 
52      * @return int size in bytes
53      */
54     public function getSize($id)
55     {
56         return filesize($this->_path . '/' . $id);
57     }
58     
59     /**
60      * Does a valid cache entry exist?
61      *
62      * @param string $id cache id (e.g. a filename)
63      * 
64      * @param int $srcMtime mtime of the original source file(s)
65      * 
66      * @return bool exists
67      */
68     public function isValid($id, $srcMtime)
69     {
70         $file = $this->_path . '/' . $id;
71         return (is_file($file) && (filemtime($file) >= $srcMtime));
72     }
73     
74     /**
75      * Send the cached content to output
76      *
77      * @param string $id cache id (e.g. a filename)
78      */
79     public function display($id)
80     {
81         if ($this->_locking) {\r
82             $fp = fopen($this->_path . '/' . $id, 'rb');\r
83             flock($fp, LOCK_SH);\r
84             fpassthru($fp);\r
85             flock($fp, LOCK_UN);\r
86             fclose($fp);\r
87         } else {\r
88             readfile($this->_path . '/' . $id);            \r
89         }\r
90     }
91     
92         /**
93      * Fetch the cached content
94      *
95      * @param string $id cache id (e.g. a filename)
96      * 
97      * @return string
98      */
99     public function fetch($id)
100     {
101         if ($this->_locking) {\r
102             $fp = fopen($this->_path . '/' . $id, 'rb');\r
103             flock($fp, LOCK_SH);\r
104             $ret = stream_get_contents($fp);\r
105             flock($fp, LOCK_UN);\r
106             fclose($fp);\r
107             return $ret;\r
108         } else {\r
109             return file_get_contents($this->_path . '/' . $id);\r
110         }
111     }
112     
113     /**
114      * Fetch the cache path used
115      *
116      * @return string
117      */
118     public function getPath()
119     {
120         return $this->_path;
121     }
122     
123     private $_path = null;
124     private $_locking = null;\r
125 }