Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

smarty_internal_method_configload.php 7.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 \Exception
  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 \Exception
  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. /* @var \Smarty_Internal_Template|\Smarty|\Smarty_Internal_Data $ptr */
  94. foreach ($tpl->smarty->ext->_updateScope->_getAffectedScopes($tpl->parent, $mergedScope) as $ptr) {
  95. $this->_assignConfigVars($ptr->config_vars, $tpl, $new_config_vars);
  96. if ($tagScope && $ptr->_isTplObj() && isset($tpl->_cache[ 'varStack' ])) {
  97. $this->_updateVarStack($tpl, $new_config_vars);
  98. }
  99. }
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Assign all config variables in given scope
  106. *
  107. * @param array $config_vars config variables in scope
  108. * @param \Smarty_Internal_Template $tpl
  109. * @param array $new_config_vars loaded config variables
  110. */
  111. public function _assignConfigVars(&$config_vars, Smarty_Internal_Template $tpl, $new_config_vars)
  112. {
  113. // copy global config vars
  114. foreach ($new_config_vars[ 'vars' ] as $variable => $value) {
  115. if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
  116. $config_vars[ $variable ] = $value;
  117. } else {
  118. $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
  119. }
  120. }
  121. // scan sections
  122. $sections = $tpl->source->config_sections;
  123. if (!empty($sections)) {
  124. foreach ((array) $sections as $tpl_section) {
  125. if (isset($new_config_vars[ 'sections' ][ $tpl_section ])) {
  126. foreach ($new_config_vars[ 'sections' ][ $tpl_section ][ 'vars' ] as $variable => $value) {
  127. if ($tpl->smarty->config_overwrite || !isset($config_vars[ $variable ])) {
  128. $config_vars[ $variable ] = $value;
  129. } else {
  130. $config_vars[ $variable ] = array_merge((array) $config_vars[ $variable ], (array) $value);
  131. }
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Update config variables in template local variable stack
  139. *
  140. * @param \Smarty_Internal_Template $tpl
  141. * @param array $config_vars
  142. */
  143. public function _updateVarStack(Smarty_Internal_Template $tpl, $config_vars)
  144. {
  145. $i = 0;
  146. while (isset($tpl->_cache[ 'varStack' ][ $i ])) {
  147. $this->_assignConfigVars($tpl->_cache[ 'varStack' ][ $i ][ 'config' ], $tpl, $config_vars);
  148. $i ++;
  149. }
  150. }
  151. /**
  152. * gets a config variable value
  153. *
  154. * @param \Smarty|\Smarty_Internal_Data|\Smarty_Internal_Template $data
  155. * @param string $varName the name of the config variable
  156. * @param bool $errorEnable
  157. *
  158. * @return null|string the value of the config variable
  159. */
  160. public function _getConfigVariable(Smarty_Internal_Data $data, $varName, $errorEnable = true)
  161. {
  162. $_ptr = $data;
  163. while ($_ptr !== null) {
  164. if (isset($_ptr->config_vars[ $varName ])) {
  165. // found it, return it
  166. return $_ptr->config_vars[ $varName ];
  167. }
  168. // not found, try at parent
  169. $_ptr = $_ptr->parent;
  170. }
  171. if ($data->smarty->error_unassigned && $errorEnable) {
  172. // force a notice
  173. $x = $$varName;
  174. }
  175. return null;
  176. }
  177. }