smarty_internal_method_configload.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <?php
  2. /**
  3. * Smarty Method ConfigLoad
  4. *
  5. * Smarty::configLoad() method
  6. *
  7. * @package Smarty
  8. * @subpackage PluginsInternal
  9. * @author Uwe Tews
  10. */
  11. class Smarty_Internal_Method_ConfigLoad
  12. {
  13. /**
  14. * Valid for all objects
  15. *
  16. * @var int
  17. */
  18. public $objMap = 7;
  19. /**
  20. * load a config file, optionally load just selected sections
  21. *
  22. * @api Smarty::configLoad()
  23. * @link http://www.smarty.net/docs/en/api.config.load.tpl
  24. *
  25. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $data
  26. * @param string $config_file filename
  27. * @param mixed $sections array of section names, single
  28. * section or null
  29. *
  30. * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
  31. * @throws \SmartyException
  32. */
  33. public function configLoad(Smarty_Internal_Data $data, $config_file, $sections = null)
  34. {
  35. $this->_loadConfigFile($data, $config_file, $sections, null);
  36. return $data;
  37. }
  38. /**
  39. * load a config file, optionally load just selected sections
  40. *
  41. * @api Smarty::configLoad()
  42. * @link http://www.smarty.net/docs/en/api.config.load.tpl
  43. *
  44. * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
  45. * @param string $config_file filename
  46. * @param mixed $sections array of section names, single
  47. * section or null
  48. * @param int $scope scope into which config variables
  49. * shall be loaded
  50. *
  51. * @return \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template
  52. * @throws \SmartyException
  53. */
  54. public function _loadConfigFile(Smarty_Internal_Data $data, $config_file, $sections = null, $scope = 0)
  55. {
  56. /* @var \Smarty $smarty */
  57. $smarty = $data->_getSmartyObj();
  58. /* @var \Smarty_Internal_Template $confObj */
  59. $confObj = new Smarty_Internal_Template($config_file, $smarty, $data, null, null, null, null, true);
  60. $confObj->caching = Smarty::CACHING_OFF;
  61. $confObj->source->config_sections = $sections;
  62. $confObj->source->scope = $scope;
  63. $confObj->compiled = Smarty_Template_Compiled::load($confObj);
  64. $confObj->compiled->render($confObj);
  65. if ($data->_isTplObj()) {
  66. $data->compiled->file_dependency[ $confObj->source->uid ] =
  67. array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
  68. }
  69. }
  70. /**
  71. * load config variables into template object
  72. *
  73. * @param \Smarty_Internal_Template $tpl
  74. * @param array $new_config_vars
  75. *
  76. */
  77. public function _loadConfigVars(Smarty_Internal_Template $tpl, $new_config_vars)
  78. {
  79. $this->_assignConfigVars($tpl->parent->config_vars, $tpl, $new_config_vars);
  80. $tagScope = $tpl->source->scope;
  81. if ($tagScope >= 0) {
  82. if ($tagScope == Smarty::SCOPE_LOCAL) {
  83. $this->_updateVarStack($tpl, $new_config_vars);
  84. $tagScope = 0;
  85. if (!$tpl->scope) {
  86. return;
  87. }
  88. }
  89. if ($tpl->parent->_isTplObj() && ($tagScope || $tpl->parent->scope)) {
  90. $mergedScope = $tagScope | $tpl->scope;
  91. if ($mergedScope) {
  92. // update scopes
  93. foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) {
  94. $this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars);
  95. if ($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
  96. $this->_updateVarStack($tpl, $new_config_vars);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. }
  103. /**
  104. * Assign all config variables in given scope
  105. *
  106. * @param array $config_vars config variables in scope
  107. * @param \Smarty_Internal_Template $tpl
  108. * @param array $new_config_vars loaded config variables
  109. */
  110. public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars)
  111. {
  112. // copy global config vars
  113. foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
  114. if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
  115. $config_vars[ $variable ] = $value;
  116. } else {
  117. $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
  118. }
  119. }
  120. // scan sections
  121. $sections = $tpl->source->config_sections;
  122. if (!empty($sections)) {
  123. foreach ((array) $sections as $tpl_section) {
  124. if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) {
  125. foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) {
  126. if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
  127. $config_vars[ $variable ] = $value;
  128. } else {
  129. $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
  130. }
  131. }
  132. }
  133. }
  134. }
  135. }
  136. /**
  137. * Update config variables in template local variable stack
  138. *
  139. * @param \Smarty_Internal_Template $tpl
  140. * @param array $config_vars
  141. */
  142. public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars)
  143. {
  144. $i = 0;
  145. while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
  146. $this->_assignConfigVars($tpl->_cache[ 'varStack' ][ $i ][ 'config' ], $tpl, $config_vars);
  147. $i ++;
  148. }
  149. }
  150. /**
  151. * gets a config variable value
  152. *
  153. * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
  154. * @param string $varName the name of the config variable
  155. * @param bool $errorEnable
  156. *
  157. * @return null|string the value of the config variable
  158. */
  159. public function _getConfigVariable(Smarty_Internal_Data $data, $varName, $errorEnable = true)
  160. {
  161. $_ptr = $data;
  162. while ($_ptr !== null) {
  163. if (isset($_ptr->config_vars[ $varName ])) {
  164. // found it, return it
  165. return $_ptr->config_vars[ $varName ];
  166. }
  167. // not found, try at parent
  168. $_ptr = $_ptr->parent;
  169. }
  170. if ($data->smarty->error_unassigned && $errorEnable) {
  171. // force a notice
  172. $x = $$varName;
  173. }
  174. return null;
  175. }
  176. }