library/smarty/libs/sysplugins/smarty_resource.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  * Base implementation for resource plugins
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage TemplateResources
       
    16  */
       
    17 abstract class Smarty_Resource {
       
    18     /**
       
    19      * Source is bypassing compiler
       
    20      *
       
    21      * @var boolean
       
    22      */
       
    23     public $uncompiled = false;
       
    24 
       
    25     /**
       
    26      * Source must be recompiled on every occasion
       
    27      *
       
    28      * @var boolean
       
    29      */
       
    30     public $recompiled = false;
       
    31     /**
       
    32      * resource handler object
       
    33      *
       
    34      * @var Smarty_Resource
       
    35      */
       
    36     public $handler = null;
       
    37     /**
       
    38      * cache for Smarty_Template_Source instances
       
    39      *
       
    40      * @var array
       
    41      */
       
    42     public static $sources = array();
       
    43     /**
       
    44      * cache for Smarty_Template_Compiled instances
       
    45      *
       
    46      * @var array
       
    47      */
       
    48     public static $compileds = array();
       
    49     /**
       
    50      * resource types provided by the core
       
    51      *
       
    52      * @var array
       
    53      */
       
    54     protected static $sysplugins = array(
       
    55         'file' => 'smarty_internal_resource_file.php',
       
    56         'string' => 'smarty_internal_resource_string.php',
       
    57         'extends' => 'smarty_internal_resource_extends.php',
       
    58         'stream' => 'smarty_internal_resource_stream.php',
       
    59         'eval' => 'smarty_internal_resource_eval.php',
       
    60         'php' => 'smarty_internal_resource_php.php'
       
    61     );
       
    62 
       
    63     /**
       
    64      * Name of the Class to compile this resource's contents with
       
    65      *
       
    66      * @var string
       
    67      */
       
    68     public $compiler_class = 'Smarty_Internal_SmartyTemplateCompiler';
       
    69 
       
    70     /**
       
    71      * Name of the Class to tokenize this resource's contents with
       
    72      *
       
    73      * @var string
       
    74      */
       
    75     public $template_lexer_class = 'Smarty_Internal_Templatelexer';
       
    76 
       
    77     /**
       
    78      * Name of the Class to parse this resource's contents with
       
    79      *
       
    80      * @var string
       
    81      */
       
    82     public $template_parser_class = 'Smarty_Internal_Templateparser';
       
    83 
       
    84     /**
       
    85      * Load template's source into current template object
       
    86      * {@internal The loaded source is assigned to $_template->source->content directly.}}
       
    87      *
       
    88      * @param  Smarty_Template_Source $source source object
       
    89      *
       
    90      * @return string                 template source
       
    91      * @throws SmartyException        if source cannot be loaded
       
    92      */
       
    93     abstract public function getContent(Smarty_Template_Source $source);
       
    94 
       
    95     /**
       
    96      * populate Source Object with meta data from Resource
       
    97      *
       
    98      * @param Smarty_Template_Source $source source object
       
    99      * @param Smarty_Internal_Template $_template template object
       
   100      */
       
   101     abstract public function populate(Smarty_Template_Source $source, Smarty_Internal_Template $_template = null);
       
   102 
       
   103     /**
       
   104      * populate Source Object with timestamp and exists from Resource
       
   105      *
       
   106      * @param Smarty_Template_Source $source source object
       
   107      */
       
   108     public function populateTimestamp(Smarty_Template_Source $source) {
       
   109         // intentionally left blank
       
   110     }
       
   111 
       
   112     /**
       
   113      * modify resource_name according to resource handlers specifications
       
   114      *
       
   115      * @param  Smarty $smarty Smarty instance
       
   116      * @param  string $resource_name resource_name to make unique
       
   117      * @param  boolean $isConfig flag for config resource
       
   118      *
       
   119      * @return string unique resource name
       
   120      */
       
   121     public function buildUniqueResourceName(Smarty $smarty, $resource_name, $isConfig = false) {
       
   122         if ($isConfig) {
       
   123             return get_class($this) . '#' . $smarty->joined_config_dir . '#' . $resource_name;
       
   124         } else {
       
   125             return get_class($this) . '#' . $smarty->joined_template_dir . '#' . $resource_name;
       
   126         }
       
   127     }
       
   128 
       
   129     /**
       
   130      * Determine basename for compiled filename
       
   131      *
       
   132      * @param  Smarty_Template_Source $source source object
       
   133      *
       
   134      * @return string                 resource's basename
       
   135      */
       
   136     public function getBasename(Smarty_Template_Source $source) {
       
   137         return null;
       
   138     }
       
   139 
       
   140     /**
       
   141      * Load Resource Handler
       
   142      *
       
   143      * @param  Smarty $smarty smarty object
       
   144      * @param  string $type name of the resource
       
   145      *
       
   146      * @throws SmartyException
       
   147      * @return Smarty_Resource Resource Handler
       
   148      */
       
   149     public static function load(Smarty $smarty, $type) {
       
   150         // try smarty's cache
       
   151         if (isset($smarty->_resource_handlers[$type])) {
       
   152             return $smarty->_resource_handlers[$type];
       
   153         }
       
   154 
       
   155         // try registered resource
       
   156         if (isset($smarty->registered_resources[$type])) {
       
   157             if ($smarty->registered_resources[$type] instanceof Smarty_Resource) {
       
   158                 $smarty->_resource_handlers[$type] = $smarty->registered_resources[$type];
       
   159             } else {
       
   160                 $smarty->_resource_handlers[$type] = new Smarty_Internal_Resource_Registered();
       
   161             }
       
   162 
       
   163             return $smarty->_resource_handlers[$type];
       
   164         }
       
   165 
       
   166         // try sysplugins dir
       
   167         if (isset(self::$sysplugins[$type])) {
       
   168             $_resource_class = 'Smarty_Internal_Resource_' . ucfirst($type);
       
   169             if (!class_exists($_resource_class, false)) {
       
   170                 require SMARTY_SYSPLUGINS_DIR . self::$sysplugins[$type];
       
   171             }
       
   172             return $smarty->_resource_handlers[$type] = new $_resource_class();
       
   173         }
       
   174 
       
   175         // try plugins dir
       
   176         $_resource_class = 'Smarty_Resource_' . ucfirst($type);
       
   177         if ($smarty->loadPlugin($_resource_class)) {
       
   178             if (class_exists($_resource_class, false)) {
       
   179                 return $smarty->_resource_handlers[$type] = new $_resource_class();
       
   180             } else {
       
   181                 $smarty->registerResource($type, array(
       
   182                     "smarty_resource_{$type}_source",
       
   183                     "smarty_resource_{$type}_timestamp",
       
   184                     "smarty_resource_{$type}_secure",
       
   185                     "smarty_resource_{$type}_trusted"
       
   186                 ));
       
   187                 // give it another try, now that the resource is registered properly
       
   188                 return self::load($smarty, $type);
       
   189             }
       
   190         }
       
   191 
       
   192         // try streams
       
   193         $_known_stream = stream_get_wrappers();
       
   194         if (in_array($type, $_known_stream)) {
       
   195             // is known stream
       
   196             if (is_object($smarty->security_policy)) {
       
   197                 $smarty->security_policy->isTrustedStream($type);
       
   198             }
       
   199             return $smarty->_resource_handlers[$type] = new Smarty_Internal_Resource_Stream();;
       
   200         }
       
   201 
       
   202         // TODO: try default_(template|config)_handler
       
   203 
       
   204         // give up
       
   205         throw new SmartyException("Unknown resource type '{$type}'");
       
   206     }
       
   207 
       
   208     /**
       
   209      * extract resource_type and resource_name from template_resource and config_resource
       
   210      * @note "C:/foo.tpl" was forced to file resource up till Smarty 3.1.3 (including).
       
   211      *
       
   212      * @param  string $resource_name template_resource or config_resource to parse
       
   213      * @param  string $default_resource the default resource_type defined in $smarty
       
   214      *
       
   215      * @return array with parsed resource name and type
       
   216      */
       
   217     public static function parseResourceName($resource_name, $default_resource) {
       
   218         if (preg_match('/^([A-Za-z0-9_\-]{2,})[:]/', $resource_name, $match)) {
       
   219             $type = $match[1];
       
   220             $name = substr($resource_name, strlen($match[0]));
       
   221         } else {
       
   222             // no resource given, use default
       
   223             // or single character before the colon is not a resource type, but part of the filepath
       
   224             $type = $default_resource;
       
   225             $name = $resource_name;
       
   226 
       
   227         }
       
   228         return array($name, $type);
       
   229     }
       
   230 
       
   231     /**
       
   232      * modify resource_name according to resource handlers specifications
       
   233      *
       
   234      * @param  Smarty $smarty Smarty instance
       
   235      * @param  string $resource_name resource_name to make unique
       
   236      *
       
   237      * @return string unique resource name
       
   238      */
       
   239 
       
   240     /**
       
   241      * modify template_resource according to resource handlers specifications
       
   242      *
       
   243      * @param  Smarty_Internal_template $template Smarty instance
       
   244      * @param  string $template_resource template_resource to extract resource handler and name of
       
   245      *
       
   246      * @return string unique resource name
       
   247      */
       
   248     public static function getUniqueTemplateName($template, $template_resource) {
       
   249         $smarty = isset($template->smarty) ? $template->smarty : $template;
       
   250         list($name, $type) = self::parseResourceName($template_resource, $smarty->default_resource_type);
       
   251         // TODO: optimize for Smarty's internal resource types
       
   252         $resource = Smarty_Resource::load($smarty, $type);
       
   253         // go relative to a given template?
       
   254         $_file_is_dotted = $name[0] == '.' && ($name[1] == '.' || $name[1] == '/');
       
   255         if ($template instanceof Smarty_Internal_Template && $_file_is_dotted && ($template->source->type == 'file' || $template->parent->source->type == 'extends')) {
       
   256             $name = dirname($template->source->filepath) . DS . $name;
       
   257         }
       
   258         return $resource->buildUniqueResourceName($smarty, $name);
       
   259     }
       
   260 
       
   261     /**
       
   262      * initialize Source Object for given resource
       
   263      * wrapper for backward compatibility to versions < 3.1.22
       
   264      * Either [$_template] or [$smarty, $template_resource] must be specified
       
   265      *
       
   266      * @param  Smarty_Internal_Template $_template template object
       
   267      * @param  Smarty $smarty smarty object
       
   268      * @param  string $template_resource resource identifier
       
   269      *
       
   270      * @return Smarty_Template_Source   Source Object
       
   271      */
       
   272     public static function source(Smarty_Internal_Template $_template = null, Smarty $smarty = null, $template_resource = null) {
       
   273         return Smarty_Template_Source::load($_template, $smarty, $template_resource);
       
   274     }
       
   275 }
       
   276