]> git.mxchange.org Git - friendica.git/blob - vendor/smarty/smarty/libs/sysplugins/smarty_cacheresource_custom.php
Merge pull request #4233 from MrPetovan/task/4116-move-smarty-to-composer
[friendica.git] / vendor / smarty / smarty / libs / sysplugins / smarty_cacheresource_custom.php
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_Custom extends Smarty_CacheResource
17 {
18     /**
19      * fetch cached content and its modification time from data source
20      *
21      * @param  string  $id         unique cache content identifier
22      * @param  string  $name       template name
23      * @param  string  $cache_id   cache id
24      * @param  string  $compile_id compile id
25      * @param  string  $content    cached content
26      * @param  integer $mtime      cache modification timestamp (epoch)
27      *
28      * @return void
29      */
30     abstract protected function fetch($id, $name, $cache_id, $compile_id, &$content, &$mtime);
31
32     /**
33      * Fetch cached content's modification timestamp from data source
34      * {@internal implementing this method is optional.
35      *  Only implement it if modification times can be accessed faster than loading the complete cached content.}}
36      *
37      * @param  string $id         unique cache content identifier
38      * @param  string $name       template name
39      * @param  string $cache_id   cache id
40      * @param  string $compile_id compile id
41      *
42      * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
43      */
44     protected function fetchTimestamp($id, $name, $cache_id, $compile_id)
45     {
46         return false;
47     }
48
49     /**
50      * Save content to cache
51      *
52      * @param  string       $id         unique cache content identifier
53      * @param  string       $name       template name
54      * @param  string       $cache_id   cache id
55      * @param  string       $compile_id compile id
56      * @param  integer|null $exp_time   seconds till expiration or null
57      * @param  string       $content    content to cache
58      *
59      * @return boolean      success
60      */
61     abstract protected function save($id, $name, $cache_id, $compile_id, $exp_time, $content);
62
63     /**
64      * Delete content from cache
65      *
66      * @param  string|null  $name       template name
67      * @param  string|null  $cache_id   cache id
68      * @param  string|null  $compile_id compile id
69      * @param  integer|null $exp_time   seconds till expiration time in seconds or null
70      *
71      * @return integer      number of deleted caches
72      */
73     abstract protected function delete($name, $cache_id, $compile_id, $exp_time);
74
75     /**
76      * populate Cached Object with meta data from Resource
77      *
78      * @param  Smarty_Template_Cached   $cached    cached object
79      * @param  Smarty_Internal_Template $_template template object
80      *
81      * @return void
82      */
83     public function populate(Smarty_Template_Cached $cached, Smarty_Internal_Template $_template)
84     {
85         $_cache_id = isset($cached->cache_id) ? preg_replace('![^\w\|]+!', '_', $cached->cache_id) : null;
86         $_compile_id = isset($cached->compile_id) ? preg_replace('![^\w]+!', '_', $cached->compile_id) : null;
87         $path = $cached->source->uid . $_cache_id . $_compile_id;
88         $cached->filepath = sha1($path);
89         if ($_template->smarty->cache_locking) {
90             $cached->lock_id = sha1('lock.' . $path);
91         }
92         $this->populateTimestamp($cached);
93     }
94
95     /**
96      * populate Cached Object with timestamp and exists from Resource
97      *
98      * @param Smarty_Template_Cached $cached
99      *
100      * @return void
101      */
102     public function populateTimestamp(Smarty_Template_Cached $cached)
103     {
104         $mtime =
105             $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
106         if ($mtime !== null) {
107             $cached->timestamp = $mtime;
108             $cached->exists = !!$cached->timestamp;
109
110             return;
111         }
112         $timestamp = null;
113         $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content,
114                      $timestamp);
115         $cached->timestamp = isset($timestamp) ? $timestamp : false;
116         $cached->exists = !!$cached->timestamp;
117     }
118
119     /**
120      * Read the cached template and process the header
121      *
122      * @param \Smarty_Internal_Template $_smarty_tpl do not change variable name, is used by compiled template
123      * @param  Smarty_Template_Cached   $cached      cached object
124      * @param boolean                   $update      flag if called because cache update
125      *
126      * @return boolean                 true or false if the cached content does not exist
127      */
128     public function process(Smarty_Internal_Template $_smarty_tpl, Smarty_Template_Cached $cached = null,
129                             $update = false)
130     {
131         if (!$cached) {
132             $cached = $_smarty_tpl->cached;
133         }
134         $content = $cached->content ? $cached->content : null;
135         $timestamp = $cached->timestamp ? $cached->timestamp : null;
136         if ($content === null || !$timestamp) {
137             $this->fetch($_smarty_tpl->cached->filepath, $_smarty_tpl->source->name, $_smarty_tpl->cache_id,
138                          $_smarty_tpl->compile_id, $content, $timestamp);
139         }
140         if (isset($content)) {
141             eval("?>" . $content);
142             $cached->content = null;
143             return true;
144         }
145
146         return false;
147     }
148
149     /**
150      * Write the rendered template output to cache
151      *
152      * @param  Smarty_Internal_Template $_template template object
153      * @param  string                   $content   content to cache
154      *
155      * @return boolean                  success
156      */
157     public function writeCachedContent(Smarty_Internal_Template $_template, $content)
158     {
159         return $this->save($_template->cached->filepath, $_template->source->name, $_template->cache_id,
160                            $_template->compile_id, $_template->cache_lifetime, $content);
161     }
162
163     /**
164      * Read cached template from cache
165      *
166      * @param  Smarty_Internal_Template $_template template object
167      *
168      * @return string|boolean  content
169      */
170     public function readCachedContent(Smarty_Internal_Template $_template)
171     {
172         $content = $_template->cached->content ? $_template->cached->content : null;
173         $timestamp = null;
174         if ($content === null) {
175             $timestamp = null;
176             $this->fetch($_template->cached->filepath, $_template->source->name, $_template->cache_id,
177                          $_template->compile_id, $content, $timestamp);
178         }
179         if (isset($content)) {
180             return $content;
181         }
182         return false;
183     }
184
185     /**
186      * Empty cache
187      *
188      * @param  Smarty  $smarty   Smarty object
189      * @param  integer $exp_time expiration time (number of seconds, not timestamp)
190      *
191      * @return integer number of cache files deleted
192      */
193     public function clearAll(Smarty $smarty, $exp_time = null)
194     {
195         return $this->delete(null, null, null, $exp_time);
196     }
197
198     /**
199      * Empty cache for a specific template
200      *
201      * @param  Smarty  $smarty        Smarty object
202      * @param  string  $resource_name template name
203      * @param  string  $cache_id      cache id
204      * @param  string  $compile_id    compile id
205      * @param  integer $exp_time      expiration time (number of seconds, not timestamp)
206      *
207      * @return integer number of cache files deleted
208      */
209     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
210     {
211         $cache_name = null;
212
213         if (isset($resource_name)) {
214             $source = Smarty_Template_Source::load(null, $smarty, $resource_name);
215             if ($source->exists) {
216                 $cache_name = $source->name;
217             } else {
218                 return 0;
219             }
220         }
221
222         return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
223     }
224
225     /**
226      * Check is cache is locked for this template
227      *
228      * @param  Smarty                 $smarty Smarty object
229      * @param  Smarty_Template_Cached $cached cached object
230      *
231      * @return boolean               true or false if cache is locked
232      */
233     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
234     {
235         $id = $cached->lock_id;
236         $name = $cached->source->name . '.lock';
237
238         $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
239         if ($mtime === null) {
240             $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
241         }
242         return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
243     }
244
245     /**
246      * Lock cache for this template
247      *
248      * @param Smarty                 $smarty Smarty object
249      * @param Smarty_Template_Cached $cached cached object
250      *
251      * @return bool|void
252      */
253     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
254     {
255         $cached->is_locked = true;
256         $id = $cached->lock_id;
257         $name = $cached->source->name . '.lock';
258         $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
259     }
260
261     /**
262      * Unlock cache for this template
263      *
264      * @param Smarty                 $smarty Smarty object
265      * @param Smarty_Template_Cached $cached cached object
266      *
267      * @return bool|void
268      */
269     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
270     {
271         $cached->is_locked = false;
272         $name = $cached->source->name . '.lock';
273         $this->delete($name, $cached->cache_id, $cached->compile_id, null);
274     }
275 }