library/smarty/libs/sysplugins/smarty_resource_custom.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty Resource Plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage TemplateResources
       
     7  * @author     Rodney Rehm
       
     8  */
       
     9 
       
    10 /**
       
    11  * Smarty Resource Plugin
       
    12  * Wrapper Implementation for custom resource plugins
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage TemplateResources
       
    16  */
       
    17 abstract class Smarty_Resource_Custom extends Smarty_Resource {
       
    18     /**
       
    19      * fetch template and its modification time from data source
       
    20      *
       
    21      * @param string $name template name
       
    22      * @param string &$source template source
       
    23      * @param integer &$mtime template modification timestamp (epoch)
       
    24      */
       
    25     abstract protected function fetch($name, &$source, &$mtime);
       
    26 
       
    27     /**
       
    28      * Fetch template's modification timestamp from data source
       
    29      * {@internal implementing this method is optional.
       
    30      *  Only implement it if modification times can be accessed faster than loading the complete template source.}}
       
    31      *
       
    32      * @param  string $name template name
       
    33      *
       
    34      * @return integer|boolean timestamp (epoch) the template was modified, or false if not found
       
    35      */
       
    36     protected function fetchTimestamp($name) {
       
    37         return null;
       
    38     }
       
    39 
       
    40     /**
       
    41      * populate Source Object with meta data from Resource
       
    42      *
       
    43      * @param Smarty_Template_Source $source source object
       
    44      * @param Smarty_Internal_Template $_template template object
       
    45      */
       
    46     public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null) {
       
    47         $source->filepath = $source->type . ':' . $source->name;
       
    48         $source->uid = sha1($source->type . ':' . $source->name);
       
    49 
       
    50         $mtime = $this->fetchTimestamp($source->name);
       
    51         if ($mtime !== null) {
       
    52             $source->timestamp = $mtime;
       
    53         } else {
       
    54             $this->fetch($source->name, $content, $timestamp);
       
    55             $source->timestamp = isset($timestamp) ? $timestamp : false;
       
    56             if (isset($content)) {
       
    57                 $source->content = $content;
       
    58             }
       
    59         }
       
    60         $source->exists = !!$source->timestamp;
       
    61     }
       
    62 
       
    63     /**
       
    64      * Load template's source into current template object
       
    65      *
       
    66      * @param  Smarty_Template_Source $source source object
       
    67      *
       
    68      * @return string                 template source
       
    69      * @throws SmartyException        if source cannot be loaded
       
    70      */
       
    71     public function getContent(Smarty_Template_Source $source) {
       
    72         $this->fetch($source->name, $content, $timestamp);
       
    73         if (isset($content)) {
       
    74             return $content;
       
    75         }
       
    76 
       
    77         throw new SmartyException("Unable to read template {$source->type} '{$source->name}'");
       
    78     }
       
    79 
       
    80     /**
       
    81      * Determine basename for compiled filename
       
    82      *
       
    83      * @param  Smarty_Template_Source $source source object
       
    84      *
       
    85      * @return string                 resource's basename
       
    86      */
       
    87     public function getBasename(Smarty_Template_Source $source) {
       
    88         return basename($source->name);
       
    89     }
       
    90 }