initial commit
[namibia] / public / min / lib / Minify / Cache / ZendPlatform.php
1 <?php
2 /**
3  * Class Minify_Cache_ZendPlatform
4  * @package Minify
5  */
6
7
8 /**
9  * ZendPlatform-based cache class for Minify
10  *
11  * Based on Minify_Cache_APC, uses output_cache_get/put (currently deprecated)
12  * 
13  * <code>
14  * Minify::setCache(new Minify_Cache_ZendPlatform());
15  * </code>
16  *
17  * @package Minify
18  * @author Patrick van Dissel
19  */
20 class Minify_Cache_ZendPlatform {
21
22
23     /**
24      * Create a Minify_Cache_ZendPlatform object, to be passed to
25      * Minify::setCache().
26      *
27      * @param int $expire seconds until expiration (default = 0
28      * meaning the item will not get an expiration date)
29      *
30      * @return null
31      */
32     public function __construct($expire = 0)
33     {
34         $this->_exp = $expire;
35     }
36
37
38     /**
39      * Write data to cache.
40      *
41      * @param string $id cache id
42      *
43      * @param string $data
44      *
45      * @return bool success
46      */
47     public function store($id, $data)
48     {
49         return output_cache_put($id, "{$_SERVER['REQUEST_TIME']}|{$data}");
50     }
51
52
53     /**
54      * Get the size of a cache entry
55      *
56      * @param string $id cache id
57      *
58      * @return int size in bytes
59      */
60     public function getSize($id)
61     {
62         return $this->_fetch($id)
63             ? strlen($this->_data)
64             : false;
65     }
66
67
68     /**
69      * Does a valid cache entry exist?
70      *
71      * @param string $id cache id
72      *
73      * @param int $srcMtime mtime of the original source file(s)
74      *
75      * @return bool exists
76      */
77     public function isValid($id, $srcMtime)
78     {
79         $ret = ($this->_fetch($id) && ($this->_lm >= $srcMtime));
80         return $ret;
81     }
82
83
84     /**
85      * Send the cached content to output
86      *
87      * @param string $id cache id
88      */
89     public function display($id)
90     {
91         echo $this->_fetch($id)
92             ? $this->_data
93             : '';
94     }
95
96
97     /**
98      * Fetch the cached content
99      *
100      * @param string $id cache id
101      *
102      * @return string
103      */
104     public function fetch($id)
105     {
106         return $this->_fetch($id)
107             ? $this->_data
108             : '';
109     }
110
111
112     private $_exp = null;
113
114
115     // cache of most recently fetched id
116     private $_lm = null;
117     private $_data = null;
118     private $_id = null;
119
120
121     /**
122      * Fetch data and timestamp from ZendPlatform, store in instance
123      *
124      * @param string $id
125      *
126      * @return bool success
127      */
128     private function _fetch($id)
129     {
130         if ($this->_id === $id) {
131             return true;
132         }
133         $ret = output_cache_get($id, $this->_exp);
134         if (false === $ret) {
135             $this->_id = null;
136             return false;
137         }
138         list($this->_lm, $this->_data) = explode('|', $ret, 2);
139         $this->_id = $id;
140         return true;
141     }
142 }