4 * Smarty Method ConfigLoad
6 * Smarty::configLoad() method
9 * @subpackage PluginsInternal
12 class Smarty_Internal_Method_ConfigLoad
15 * Valid for all objects
22 * load a config file, optionally load just selected sections
24 * @api Smarty::configLoad()
25 * @link http://www.smarty.net/docs/en/api.config.load.tpl
27 * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
28 * @param string $config_file filename
29 * @param mixed $sections array of section names, single
32 * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
33 * @throws \SmartyException
35 public function configLoad(Smarty_Internal_Data $data, $config_file, $sections = null)
37 $this->_loadConfigFile($data, $config_file, $sections, null);
42 * load a config file, optionally load just selected sections
44 * @api Smarty::configLoad()
45 * @link http://www.smarty.net/docs/en/api.config.load.tpl
47 * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
48 * @param string $config_file filename
49 * @param mixed $sections array of section names, single
51 * @param int $scope scope into which config variables
54 * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
55 * @throws \SmartyException
57 public function _loadConfigFile(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 0)
59 /* @var \Smarty $smarty */
60 $smarty = $data->_getSmartyObj();
61 /* @var \Smarty_Internal_Template $confObj */
62 $confObj = new Smarty_Internal_Template($config_file, $smarty, $data, null, null, null, null, true);
63 $confObj->caching = Smarty::CACHING_OFF;
64 $confObj->source->config_sections = $sections;
65 $confObj->source->scope = $scope;
66 $confObj->compiled = Smarty_Template_Compiled::load($confObj);
67 $confObj->compiled->render($confObj);
68 if ($data->_isTplObj()) {
69 $data->compiled->file_dependency[ $confObj->source->uid ] =
70 array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
75 * load config variables into template object
77 * @param \Smarty_Internal_Template $tpl
78 * @param array $new_config_vars
81 public function _loadConfigVars(Smarty_Internal_Template $tpl, $new_config_vars)
83 $this->_assignConfigVars($tpl->parent->config_vars, $tpl, $new_config_vars);
84 $tagScope = $tpl->source->scope;
86 if ($tagScope == Smarty::SCOPE_LOCAL) {
87 $this->_updateVarStack($tpl, $new_config_vars);
93 if ($tpl->parent->_isTplObj() && ($tagScope || $tpl->parent->scope)) {
94 $mergedScope = $tagScope | $tpl->scope;
97 foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) {
98 $this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars);
99 if ($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
100 $this->_updateVarStack($tpl, $new_config_vars);
109 * Assign all config variables in given scope
111 * @param array $config_vars config variables in scope
112 * @param \Smarty_Internal_Template $tpl
113 * @param array $new_config_vars loaded config variables
115 public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars)
117 // copy global config vars
118 foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
119 if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
120 $config_vars[ $variable ] = $value;
122 $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
126 $sections = $tpl->source->config_sections;
127 if (!empty($sections)) {
128 foreach ((array) $sections as $tpl_section) {
129 if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) {
130 foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) {
131 if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
132 $config_vars[ $variable ] = $value;
134 $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
143 * Update config variables in template local variable stack
145 * @param \Smarty_Internal_Template $tpl
146 * @param array $config_vars
148 public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars)
151 while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
152 $this->_assignConfigVars($tpl->_cache[ 'varStack' ][ $i ][ 'config' ], $tpl, $config_vars);
158 * gets a config variable value
160 * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
161 * @param string $varName the name of the config variable
162 * @param bool $errorEnable
164 * @return null|string the value of the config variable
166 public function _getConfigVariable(Smarty_Internal_Data $data, $varName, $errorEnable = true)
169 while ($_ptr !== null) {
170 if (isset($_ptr->config_vars[ $varName ])) {
171 // found it, return it
172 return $_ptr->config_vars[ $varName ];
174 // not found, try at parent
175 $_ptr = $_ptr->parent;
177 if ($data->smarty->error_unassigned && $errorEnable) {