You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

smarty_internal_method_configload.php 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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, 0);
  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 = isset($data->smarty) ? $data->smarty : $data;
  58. /* @var \Smarty_Internal_Template $confObj */
  59. $confObj = new Smarty_Internal_Template($config_file, $smarty, $data);
  60. $confObj->caching = Smarty::CACHING_OFF;
  61. $confObj->source = Smarty_Template_Config::load($confObj);
  62. $confObj->source->config_sections = $sections;
  63. $confObj->source->scope = $scope;
  64. $confObj->compiled = Smarty_Template_Compiled::load($confObj);
  65. $confObj->compiled->render($confObj);
  66. if ($data->_objType == 2) {
  67. $data->compiled->file_dependency[$confObj->source->uid] =
  68. array($confObj->source->filepath, $confObj->source->getTimeStamp(), $confObj->source->type);
  69. }
  70. }
  71. /**
  72. * load config variables into template object
  73. *
  74. * @param \Smarty_Internal_Template $tpl
  75. * @param array $_config_vars
  76. *
  77. */
  78. public function _loadConfigVars(Smarty_Internal_Template $tpl, $_config_vars)
  79. {
  80. $this->_assignConfigVars($tpl->parent, $tpl, $_config_vars);
  81. $scope = $tpl->source->scope;
  82. if (!$scope && !$tpl->scope) {
  83. return;
  84. }
  85. foreach (array($scope, $tpl->scope) as $s) {
  86. $s = ($bubble_up = $s >= Smarty::SCOPE_BUBBLE_UP) ? $s - Smarty::SCOPE_BUBBLE_UP : $s;
  87. if ($bubble_up && $s) {
  88. $ptr = $tpl->parent->parent;
  89. if (isset($ptr)) {
  90. $this->_assignConfigVars($ptr, $tpl, $_config_vars);
  91. $ptr = $ptr->parent;
  92. }
  93. if ($s == Smarty::SCOPE_PARENT) {
  94. continue;
  95. }
  96. while (isset($ptr) && $ptr->_objType == 2) {
  97. $this->_assignConfigVars($ptr, $tpl, $_config_vars);
  98. $ptr = $ptr->parent;
  99. }
  100. if ($s == Smarty::SCOPE_TPL_ROOT) {
  101. continue;
  102. } elseif ($s == Smarty::SCOPE_SMARTY) {
  103. $this->_assignConfigVars($tpl->smarty, $tpl, $_config_vars);
  104. } elseif ($s == Smarty::SCOPE_GLOBAL) {
  105. $this->_assignConfigVars($tpl->smarty, $tpl, $_config_vars);
  106. } elseif ($s == Smarty::SCOPE_ROOT) {
  107. while (isset($ptr->parent)) {
  108. $ptr = $ptr->parent;
  109. }
  110. $this->_assignConfigVars($ptr, $tpl, $_config_vars);
  111. }
  112. }
  113. }
  114. }
  115. /**
  116. * Assign all config variables in given scope
  117. *
  118. * @param \Smarty_Internal_Data $scope_ptr
  119. * @param \Smarty_Internal_Template $tpl
  120. * @param array $_config_vars
  121. */
  122. public function _assignConfigVars(Smarty_Internal_Data $scope_ptr, Smarty_Internal_Template $tpl, $_config_vars)
  123. {
  124. // copy global config vars
  125. foreach ($_config_vars['vars'] as $variable => $value) {
  126. if ($tpl->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
  127. $scope_ptr->config_vars[$variable] = $value;
  128. } else {
  129. $scope_ptr->config_vars[$variable] =
  130. array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
  131. }
  132. }
  133. // scan sections
  134. $sections = $tpl->source->config_sections;
  135. if (!empty($sections)) {
  136. foreach ((array) $sections as $tpl_section) {
  137. if (isset($_config_vars['sections'][$tpl_section])) {
  138. foreach ($_config_vars['sections'][$tpl_section]['vars'] as $variable => $value) {
  139. if ($tpl->smarty->config_overwrite || !isset($scope_ptr->config_vars[$variable])) {
  140. $scope_ptr->config_vars[$variable] = $value;
  141. } else {
  142. $scope_ptr->config_vars[$variable] =
  143. array_merge((array) $scope_ptr->config_vars[$variable], (array) $value);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. }
  150. /**
  151. * gets a config variable value
  152. *
  153. * @param \Smarty_Internal_Template $tpl template object
  154. * @param string $varName the name of the config variable
  155. * @param bool $errorEnable
  156. *
  157. * @return mixed the value of the config variable
  158. */
  159. public function _getConfigVariable(Smarty_Internal_Template $tpl, $varName, $errorEnable = true)
  160. {
  161. $_ptr = $tpl;
  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 ($tpl->smarty->error_unassigned && $errorEnable) {
  171. // force a notice
  172. $x = $$varName;
  173. }
  174. return null;
  175. }
  176. }