library/smarty/libs/sysplugins/smarty_internal_extension_config.php
changeset 46 f11c31f7fa3e
parent 45 a56e7f9a0463
child 47 03388ec805b4
equal deleted inserted replaced
45:a56e7f9a0463 46:f11c31f7fa3e
     1 <?php
       
     2 
       
     3 /**
       
     4  * @package    Smarty
       
     5  * @subpackage PluginsInternal
       
     6  */
       
     7 class Smarty_Internal_Extension_Config {
       
     8     /**
       
     9      * @param        $obj
       
    10      * @param        $config_file
       
    11      * @param null $sections
       
    12      * @param string $scope
       
    13      */
       
    14     static function configLoad($obj, $config_file, $sections = null, $scope = 'local') {
       
    15         $smarty = isset($obj->smarty) ? $obj->smarty : $obj;
       
    16         $confObj = new $smarty->template_class($config_file, $smarty, $obj);
       
    17         $confObj->caching = Smarty::CACHING_OFF;
       
    18         $confObj->source = Smarty_Template_Config::load($confObj);
       
    19         $confObj->source->config_sections = $sections;
       
    20         $confObj->source->scope = $scope;
       
    21         $confObj->compiled = Smarty_Template_Compiled::load($confObj);
       
    22         if ($confObj->smarty->debugging) {
       
    23             Smarty_Internal_Debug::start_render($confObj);
       
    24         }
       
    25         $confObj->compiled->render($confObj);
       
    26         if ($confObj->smarty->debugging) {
       
    27             Smarty_Internal_Debug::end_render($confObj);
       
    28         }
       
    29         if ($obj instanceof Smarty_Internal_Template) {
       
    30             $obj->properties['file_dependency'][$confObj->source->uid] = array($confObj->source->filepath, $confObj->source->timestamp, $confObj->source->type);
       
    31         }
       
    32     }
       
    33 
       
    34     /**
       
    35      * load config variables
       
    36      *
       
    37      * @param mixed $sections array of section names, single section or null
       
    38      * @param string $scope global,parent or local
       
    39      *
       
    40      * @throws Exception
       
    41      */
       
    42     static function loadConfigVars($_template, $_config_vars) {
       
    43         $scope = $_template->source->scope;
       
    44         // pointer to scope (local scope is parent of template object
       
    45         $scope_ptr = $_template->parent;
       
    46         if ($scope == 'parent') {
       
    47             if (isset($_template->parent->parent)) {
       
    48                 $scope_ptr = $_template->parent->parent;
       
    49             }
       
    50         } elseif ($scope == 'root' || $scope == 'global') {
       
    51             while (isset($scope_ptr->parent)) {
       
    52                 $scope_ptr = $scope_ptr->parent;
       
    53             }
       
    54         }
       
    55         // copy global config vars
       
    56         foreach ($_config_vars['vars'] as $variable => $value) {
       
    57             if ($_template->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
       
    58                 $scope_ptr->config_vars[$variable] = $value;
       
    59             } else {
       
    60                 $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
       
    61             }
       
    62         }
       
    63         // scan sections
       
    64         $sections = $_template->source->config_sections;
       
    65         if (!empty($sections)) {
       
    66             foreach ((array)$sections as $_template_section) {
       
    67                 if (isset($_config_vars['sections'][$_template_section])) {
       
    68                     foreach ($_config_vars['sections'][$_template_section]['vars'] as $variable => $value) {
       
    69                         if ($_template->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
       
    70                             $scope_ptr->config_vars[$variable] = $value;
       
    71                         } else {
       
    72                             $scope_ptr->config_vars[$variable] = array_merge((array)$scope_ptr->config_vars[$variable], (array)$value);
       
    73                         }
       
    74                     }
       
    75                 }
       
    76             }
       
    77         }
       
    78     }
       
    79 
       
    80     /**
       
    81      * Returns a single or all config variables
       
    82      *
       
    83      * @param  string $varname variable name or null
       
    84      * @param bool $search_parents
       
    85      *
       
    86      * @return string variable value or or array of variables
       
    87      */
       
    88     static function getConfigVars($obj, $varname = null, $search_parents = true) {
       
    89         $_ptr = $obj;
       
    90         $var_array = array();
       
    91         while ($_ptr !== null) {
       
    92             if (isset($varname)) {
       
    93                 if (isset($_ptr->config_vars[$varname])) {
       
    94                     return $_ptr->config_vars[$varname];
       
    95                 }
       
    96             } else {
       
    97                 $var_array = array_merge($_ptr->config_vars, $var_array);
       
    98             }
       
    99             // not found, try at parent
       
   100             if ($search_parents) {
       
   101                 $_ptr = $_ptr->parent;
       
   102             } else {
       
   103                 $_ptr = null;
       
   104             }
       
   105         }
       
   106         if (isset($varname)) {
       
   107             return '';
       
   108         } else {
       
   109             return $var_array;
       
   110         }
       
   111     }
       
   112 
       
   113     /**
       
   114      * gets  a config variable
       
   115      *
       
   116      * @param  string $variable the name of the config variable
       
   117      * @param bool $error_enable
       
   118      *
       
   119      * @return mixed  the value of the config variable
       
   120      */
       
   121     static function getConfigVariable($obj, $variable, $error_enable = true) {
       
   122         $_ptr = $obj;
       
   123         while ($_ptr !== null) {
       
   124             if (isset($_ptr->config_vars[$variable])) {
       
   125                 // found it, return it
       
   126                 return $_ptr->config_vars[$variable];
       
   127             }
       
   128             // not found, try at parent
       
   129             $_ptr = $_ptr->parent;
       
   130         }
       
   131         if ($obj->error_unassigned && $error_enable) {
       
   132             // force a notice
       
   133             $x = $$variable;
       
   134         }
       
   135 
       
   136         return null;
       
   137     }
       
   138 
       
   139     /**
       
   140      * remove a single or all config variables
       
   141      *
       
   142      * @param  string $name variable name or null
       
   143      *
       
   144      * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for chaining
       
   145      */
       
   146     static function clearConfig($obj, $name = null) {
       
   147         if (isset($name)) {
       
   148             unset($obj->config_vars[$name]);
       
   149         } else {
       
   150             $obj->config_vars = array();
       
   151         }
       
   152         return $obj;
       
   153     }
       
   154 }