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