|
1 <?php |
|
2 /** |
|
3 * Smarty Internal Plugin |
|
4 * |
|
5 * @package Smarty |
|
6 * @subpackage Cacher |
|
7 */ |
|
8 |
|
9 /** |
|
10 * Cache Handler API |
|
11 * |
|
12 * @package Smarty |
|
13 * @subpackage Cacher |
|
14 * @author Rodney Rehm |
|
15 */ |
|
16 abstract class Smarty_CacheResource { |
|
17 /** |
|
18 * resource types provided by the core |
|
19 * |
|
20 * @var array |
|
21 */ |
|
22 protected static $sysplugins = array( |
|
23 'file' => 'smarty_internal_cacheresource_file.php', |
|
24 ); |
|
25 |
|
26 /** |
|
27 * populate Cached Object with meta data from Resource |
|
28 * |
|
29 * @param Smarty_Template_Cached $cached cached object |
|
30 * @param Smarty_Internal_Template $_template template object |
|
31 * |
|
32 * @return void |
|
33 */ |
|
34 abstract public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template); |
|
35 |
|
36 /** |
|
37 * populate Cached Object with timestamp and exists from Resource |
|
38 * |
|
39 * @param Smarty_Template_Cached $cached |
|
40 * |
|
41 * @return void |
|
42 */ |
|
43 abstract public function populateTimestamp(Smarty_Template_Cached $cached); |
|
44 |
|
45 /** |
|
46 * Read the cached template and process header |
|
47 * |
|
48 * @param Smarty_Internal_Template $_template template object |
|
49 * @param Smarty_Template_Cached $cached cached object |
|
50 * |
|
51 * @return boolean true or false if the cached content does not exist |
|
52 */ |
|
53 abstract public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null); |
|
54 |
|
55 /** |
|
56 * Write the rendered template output to cache |
|
57 * |
|
58 * @param Smarty_Internal_Template $_template template object |
|
59 * @param string $content content to cache |
|
60 * |
|
61 * @return boolean success |
|
62 */ |
|
63 abstract public function writeCachedContent(Smarty_Internal_Template $_template, $content); |
|
64 |
|
65 /** |
|
66 * Return cached content |
|
67 * |
|
68 * @param Smarty_Internal_Template $_template template object |
|
69 * |
|
70 * @return null|string |
|
71 */ |
|
72 public function getCachedContent(Smarty_Internal_Template $_template) { |
|
73 if ($_template->cached->handler->process($_template)) { |
|
74 ob_start(); |
|
75 $_template->properties['unifunc']($_template); |
|
76 |
|
77 return ob_get_clean(); |
|
78 } |
|
79 |
|
80 return null; |
|
81 } |
|
82 |
|
83 /** |
|
84 * Empty cache |
|
85 * |
|
86 * @param Smarty $smarty Smarty object |
|
87 * @param integer $exp_time expiration time (number of seconds, not timestamp) |
|
88 * |
|
89 * @return integer number of cache files deleted |
|
90 */ |
|
91 abstract public function clearAll(Smarty $smarty, $exp_time = null); |
|
92 |
|
93 /** |
|
94 * Empty cache for a specific template |
|
95 * |
|
96 * @param Smarty $smarty Smarty object |
|
97 * @param string $resource_name template name |
|
98 * @param string $cache_id cache id |
|
99 * @param string $compile_id compile id |
|
100 * @param integer $exp_time expiration time (number of seconds, not timestamp) |
|
101 * |
|
102 * @return integer number of cache files deleted |
|
103 */ |
|
104 abstract public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time); |
|
105 |
|
106 /** |
|
107 * @param Smarty $smarty |
|
108 * @param Smarty_Template_Cached $cached |
|
109 * |
|
110 * @return bool|null |
|
111 */ |
|
112 public function locked(Smarty $smarty, Smarty_Template_Cached $cached) { |
|
113 // theoretically locking_timeout should be checked against time_limit (max_execution_time) |
|
114 $start = microtime(true); |
|
115 $hadLock = null; |
|
116 while ($this->hasLock($smarty, $cached)) { |
|
117 $hadLock = true; |
|
118 if (microtime(true) - $start > $smarty->locking_timeout) { |
|
119 // abort waiting for lock release |
|
120 return false; |
|
121 } |
|
122 sleep(1); |
|
123 } |
|
124 |
|
125 return $hadLock; |
|
126 } |
|
127 |
|
128 /** |
|
129 * Check is cache is locked for this template |
|
130 * |
|
131 * @param Smarty $smarty |
|
132 * @param Smarty_Template_Cached $cached |
|
133 * |
|
134 * @return bool |
|
135 */ |
|
136 public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached) { |
|
137 // check if lock exists |
|
138 return false; |
|
139 } |
|
140 |
|
141 /** |
|
142 * Lock cache for this template |
|
143 * |
|
144 * @param Smarty $smarty |
|
145 * @param Smarty_Template_Cached $cached |
|
146 * |
|
147 * @return bool |
|
148 */ |
|
149 public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached) { |
|
150 // create lock |
|
151 return true; |
|
152 } |
|
153 |
|
154 /** |
|
155 * Unlock cache for this template |
|
156 * |
|
157 * @param Smarty $smarty |
|
158 * @param Smarty_Template_Cached $cached |
|
159 * |
|
160 * @return bool |
|
161 */ |
|
162 public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached) { |
|
163 // release lock |
|
164 return true; |
|
165 } |
|
166 |
|
167 /** |
|
168 * Load Cache Resource Handler |
|
169 * |
|
170 * @param Smarty $smarty Smarty object |
|
171 * @param string $type name of the cache resource |
|
172 * |
|
173 * @throws SmartyException |
|
174 * @return Smarty_CacheResource Cache Resource Handler |
|
175 */ |
|
176 public static function load(Smarty $smarty, $type = null) { |
|
177 if (!isset($type)) { |
|
178 $type = $smarty->caching_type; |
|
179 } |
|
180 |
|
181 // try smarty's cache |
|
182 if (isset($smarty->_cacheresource_handlers[$type])) { |
|
183 return $smarty->_cacheresource_handlers[$type]; |
|
184 } |
|
185 |
|
186 // try registered resource |
|
187 if (isset($smarty->registered_cache_resources[$type])) { |
|
188 // do not cache these instances as they may vary from instance to instance |
|
189 return $smarty->_cacheresource_handlers[$type] = $smarty->registered_cache_resources[$type]; |
|
190 } |
|
191 // try sysplugins dir |
|
192 if (isset(self::$sysplugins[$type])) { |
|
193 $cache_resource_class = 'Smarty_Internal_CacheResource_' . ucfirst($type); |
|
194 if (!class_exists($cache_resource_class, false)) { |
|
195 require SMARTY_SYSPLUGINS_DIR . self::$sysplugins[$type]; |
|
196 } |
|
197 return $smarty->_cacheresource_handlers[$type] = new $cache_resource_class(); |
|
198 } |
|
199 // try plugins dir |
|
200 $cache_resource_class = 'Smarty_CacheResource_' . ucfirst($type); |
|
201 if ($smarty->loadPlugin($cache_resource_class)) { |
|
202 return $smarty->_cacheresource_handlers[$type] = new $cache_resource_class(); |
|
203 } |
|
204 // give up |
|
205 throw new SmartyException("Unable to load cache resource '{$type}'"); |
|
206 } |
|
207 |
|
208 /** |
|
209 * Invalid Loaded Cache Files |
|
210 * |
|
211 * @param Smarty $smarty Smarty object |
|
212 */ |
|
213 public static function invalidLoadedCache(Smarty $smarty) { |
|
214 foreach ($smarty->template_objects as $tpl) { |
|
215 if (isset($tpl->cached)) { |
|
216 $tpl->cached->valid = false; |
|
217 $tpl->cached->processed = false; |
|
218 } |
|
219 } |
|
220 } |
|
221 } |