gluon-web-remote

Web remote Administration for big size of gluon routers
git clone git://archive.git.mtrnord.blog/MTRNord/gluon-web-remote.git
Log | Files | Refs | README

smarty_cacheresource_custom.php (10549B)


      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 null;
     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       $name       template name
     67      * @param  string       $cache_id   cache id
     68      * @param  string       $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->filepath . $_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 = $this->fetchTimestamp($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id);
    105         if ($mtime !== null) {
    106             $cached->timestamp = $mtime;
    107             $cached->exists = !!$cached->timestamp;
    108 
    109             return;
    110         }
    111         $timestamp = null;
    112         $this->fetch($cached->filepath, $cached->source->name, $cached->cache_id, $cached->compile_id, $cached->content, $timestamp);
    113         $cached->timestamp = isset($timestamp) ? $timestamp : false;
    114         $cached->exists = !!$cached->timestamp;
    115     }
    116 
    117     /**
    118      * Read the cached template and process the header
    119      *
    120      * @param  Smarty_Internal_Template $_template template object
    121      * @param  Smarty_Template_Cached   $cached    cached object
    122      *
    123      * @return boolean                 true or false if the cached content does not exist
    124      */
    125     public function process(Smarty_Internal_Template $_template, Smarty_Template_Cached $cached = null)
    126     {
    127         if (!$cached) {
    128             $cached = $_template->cached;
    129         }
    130         $content = $cached->content ? $cached->content : null;
    131         $timestamp = $cached->timestamp ? $cached->timestamp : null;
    132         if ($content === null || !$timestamp) {
    133             $this->fetch(
    134                 $_template->cached->filepath,
    135                 $_template->source->name,
    136                 $_template->cache_id,
    137                 $_template->compile_id,
    138                 $content,
    139                 $timestamp
    140             );
    141         }
    142         if (isset($content)) {
    143             /** @var Smarty_Internal_Template $_smarty_tpl
    144              * used in evaluated code
    145              */
    146             $_smarty_tpl = $_template;
    147             eval("?>" . $content);
    148 
    149             return true;
    150         }
    151 
    152         return false;
    153     }
    154 
    155     /**
    156      * Write the rendered template output to cache
    157      *
    158      * @param  Smarty_Internal_Template $_template template object
    159      * @param  string                   $content   content to cache
    160      *
    161      * @return boolean                  success
    162      */
    163     public function writeCachedContent(Smarty_Internal_Template $_template, $content)
    164     {
    165         return $this->save(
    166             $_template->cached->filepath,
    167             $_template->source->name,
    168             $_template->cache_id,
    169             $_template->compile_id,
    170             $_template->properties['cache_lifetime'],
    171             $content
    172         );
    173     }
    174 
    175     /**
    176      * Read cached template from cache
    177      *
    178      * @param  Smarty_Internal_Template $_template template object
    179      *
    180      * @return string  content
    181      */
    182     public function readCachedContent(Smarty_Internal_Template $_template)
    183     {
    184         $content = $_template->cached->content ? $_template->cached->content : null;
    185         $timestamp = null;
    186         if ($content === null) {
    187             $timestamp = null;
    188             $this->fetch(
    189                 $_template->cached->filepath,
    190                 $_template->source->name,
    191                 $_template->cache_id,
    192                 $_template->compile_id,
    193                 $content,
    194                 $timestamp
    195             );
    196         }
    197         if (isset($content)) {
    198             return $content;
    199         }
    200         return false;
    201     }
    202 
    203     /**
    204      * Empty cache
    205      *
    206      * @param  Smarty  $smarty   Smarty object
    207      * @param  integer $exp_time expiration time (number of seconds, not timestamp)
    208      *
    209      * @return integer number of cache files deleted
    210      */
    211     public function clearAll(Smarty $smarty, $exp_time = null)
    212     {
    213         $this->cache = array();
    214 
    215         return $this->delete(null, null, null, $exp_time);
    216     }
    217 
    218     /**
    219      * Empty cache for a specific template
    220      *
    221      * @param  Smarty  $smarty        Smarty object
    222      * @param  string  $resource_name template name
    223      * @param  string  $cache_id      cache id
    224      * @param  string  $compile_id    compile id
    225      * @param  integer $exp_time      expiration time (number of seconds, not timestamp)
    226      *
    227      * @return integer number of cache files deleted
    228      */
    229     public function clear(Smarty $smarty, $resource_name, $cache_id, $compile_id, $exp_time)
    230     {
    231         $this->cache = array();
    232         $cache_name = null;
    233 
    234         if (isset($resource_name)) {
    235             $_save_stat = $smarty->caching;
    236             $smarty->caching = true;
    237             $tpl = new $smarty->template_class($resource_name, $smarty);
    238             $smarty->caching = $_save_stat;
    239 
    240             if ($tpl->source->exists) {
    241                 $cache_name = $tpl->source->name;
    242             } else {
    243                 return 0;
    244             }
    245             // remove from template cache
    246             if ($smarty->allow_ambiguous_resources) {
    247                 $_templateId = $tpl->source->unique_resource . $tpl->cache_id . $tpl->compile_id;
    248             } else {
    249                 $_templateId = $smarty->joined_template_dir . '#' . $resource_name . $tpl->cache_id . $tpl->compile_id;
    250             }
    251             if (isset($_templateId[150])) {
    252                 $_templateId = sha1($_templateId);
    253             }
    254             unset($smarty->template_objects[$_templateId]);
    255             // template object no longer needed
    256             unset($tpl);
    257         }
    258 
    259         return $this->delete($cache_name, $cache_id, $compile_id, $exp_time);
    260     }
    261 
    262     /**
    263      * Check is cache is locked for this template
    264      *
    265      * @param  Smarty                 $smarty Smarty object
    266      * @param  Smarty_Template_Cached $cached cached object
    267      *
    268      * @return boolean               true or false if cache is locked
    269      */
    270     public function hasLock(Smarty $smarty, Smarty_Template_Cached $cached)
    271     {
    272         $id = $cached->lock_id;
    273         $name = $cached->source->name . '.lock';
    274 
    275         $mtime = $this->fetchTimestamp($id, $name, $cached->cache_id, $cached->compile_id);
    276         if ($mtime === null) {
    277             $this->fetch($id, $name, $cached->cache_id, $cached->compile_id, $content, $mtime);
    278         }
    279         return $mtime && ($t = time()) - $mtime < $smarty->locking_timeout;
    280     }
    281 
    282     /**
    283      * Lock cache for this template
    284      *
    285      * @param Smarty                 $smarty Smarty object
    286      * @param Smarty_Template_Cached $cached cached object
    287      *
    288      * @return bool|void
    289      */
    290     public function acquireLock(Smarty $smarty, Smarty_Template_Cached $cached)
    291     {
    292         $cached->is_locked = true;
    293         $id = $cached->lock_id;
    294         $name = $cached->source->name . '.lock';
    295         $this->save($id, $name, $cached->cache_id, $cached->compile_id, $smarty->locking_timeout, '');
    296     }
    297 
    298     /**
    299      * Unlock cache for this template
    300      *
    301      * @param Smarty                 $smarty Smarty object
    302      * @param Smarty_Template_Cached $cached cached object
    303      *
    304      * @return bool|void
    305      */
    306     public function releaseLock(Smarty $smarty, Smarty_Template_Cached $cached)
    307     {
    308         $cached->is_locked = false;
    309         $name = $cached->source->name . '.lock';
    310         $this->delete($name, $cached->cache_id, $cached->compile_id, null);
    311     }
    312 }