library/smarty/libs/sysplugins/smarty_template_config.php
changeset 0 4869aea77e21
equal deleted inserted replaced
-1:000000000000 0:4869aea77e21
       
     1 <?php
       
     2 /**
       
     3  * Smarty Config Source Plugin
       
     4  *
       
     5  * @package    Smarty
       
     6  * @subpackage TemplateResources
       
     7  * @author     Uwe Tews
       
     8  */
       
     9 
       
    10 /**
       
    11  * Smarty Connfig Resource Data Object
       
    12  * Meta Data Container for Template Files
       
    13  *
       
    14  * @package    Smarty
       
    15  * @subpackage TemplateResources
       
    16  * @author     Uwe Tews
       
    17  * @property integer $timestamp Source Timestamp
       
    18  * @property boolean $exists    Source Existence
       
    19  * @property boolean $template  Extended Template reference
       
    20  * @property string $content   Source Content
       
    21  */
       
    22 class Smarty_Template_Config extends Smarty_Template_Source {
       
    23     /**
       
    24      * Name of the Class to compile this resource's contents with
       
    25      *
       
    26      * @var string
       
    27      */
       
    28     public $compiler_class = 'Smarty_Internal_Config_File_Compiler';
       
    29 
       
    30     /**
       
    31      * Name of the Class to tokenize this resource's contents with
       
    32      *
       
    33      * @var string
       
    34      */
       
    35     public $template_lexer_class = 'Smarty_Internal_Configfilelexer';
       
    36 
       
    37     /**
       
    38      * Name of the Class to parse this resource's contents with
       
    39      *
       
    40      * @var string
       
    41      */
       
    42     public $template_parser_class = 'Smarty_Internal_Configfileparser';
       
    43 
       
    44     /**
       
    45      * array of section names, single section or null
       
    46      *
       
    47      * @var null|string|array
       
    48      */
       
    49     public $config_sections = null;
       
    50 
       
    51     /**
       
    52      * scope into which the config variables shall be loaded
       
    53      *
       
    54      * @var string
       
    55      */
       
    56     public $scope = 'local';
       
    57 
       
    58     /**
       
    59      * Flag that source is a config file
       
    60      *
       
    61      * @var bool
       
    62      */
       
    63     public $isConfig = true;
       
    64 
       
    65     /**
       
    66      * create Source Object container
       
    67      *
       
    68      * @param Smarty_Resource $handler Resource Handler this source object communicates with
       
    69      * @param Smarty $smarty Smarty instance this source object belongs to
       
    70      * @param string $resource full template_resource
       
    71      * @param string $type type of resource
       
    72      * @param string $name resource name
       
    73      */
       
    74     public function __construct(Smarty_Resource $handler, Smarty $smarty, $resource, $type, $name) {
       
    75         $this->handler = clone $handler; // Note: prone to circular references
       
    76         $this->resource = $resource;
       
    77         $this->type = $type;
       
    78         $this->name = $name;
       
    79         $this->smarty = $smarty;
       
    80     }
       
    81 
       
    82     /**
       
    83      * initialize Source Object for given resource
       
    84      * Either [$_template] or [$smarty, $template_resource] must be specified
       
    85      *
       
    86      * @param  Smarty_Internal_Template $_template template object
       
    87      * @param  Smarty $smarty smarty object
       
    88      * @param  string $template_resource resource identifier
       
    89      *
       
    90      * @return Smarty_Template_Source Source Object
       
    91      * @throws SmartyException
       
    92      */
       
    93     public static function load(Smarty_Internal_Template $_template = null, Smarty $smarty = null, $template_resource = null) {
       
    94         static $_incompatible_resources = array('extends' => true, 'php' => true);
       
    95         $smarty = $_template->smarty;
       
    96         $template_resource = $_template->template_resource;
       
    97         if (empty($template_resource)) {
       
    98             throw new SmartyException('Missing config name');
       
    99         }
       
   100         // parse resource_name, load resource handler
       
   101         list($name, $type) = Smarty_Resource::parseResourceName($template_resource, $smarty->default_config_type);
       
   102         // make sure configs are not loaded via anything smarty can't handle
       
   103         if (isset($_incompatible_resources[$type])) {
       
   104             throw new SmartyException ("Unable to use resource '{$type}' for config");
       
   105         }
       
   106         $resource = Smarty_Resource::load($smarty, $type);
       
   107         $source = new Smarty_Template_Config($resource, $smarty, $template_resource, $type, $name);
       
   108         $resource->populate($source, $_template);
       
   109         if ((!isset($source->exists) || !$source->exists) && isset($_template->smarty->default_config_handler_func)) {
       
   110             Smarty_Internal_Extension_DefaultTemplateHandler::_getDefault($_template, $source, $resource);
       
   111         }
       
   112         $source->unique_resource = $resource->buildUniqueResourceName($smarty, $name, true);
       
   113         return $source;
       
   114     }
       
   115 }