smarty_internal_data.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. /**
  3. * Smarty Internal Plugin Data
  4. * This file contains the basic classes and methods for template and variable creation
  5. *
  6. * @package Smarty
  7. * @subpackage Template
  8. * @author Uwe Tews
  9. */
  10. /**
  11. * Base class with template and variable methods
  12. *
  13. * @package Smarty
  14. * @subpackage Template
  15. *
  16. * @property int $scope
  17. * The following methods will be dynamically loaded by the extension handler when they are called.
  18. * They are located in a corresponding Smarty_Internal_Method_xxxx class
  19. *
  20. * @method mixed getConfigVars(string $varName = null, bool $searchParents = true)
  21. * @method mixed getGlobal(string $varName = null)
  22. * @method mixed getStreamVariable(string $variable)
  23. * @method Smarty_Internal_Data clearAssign(mixed $tpl_var)
  24. * @method Smarty_Internal_Data clearAllAssign()
  25. * @method Smarty_Internal_Data clearConfig(string $varName = null)
  26. * @method Smarty_Internal_Data configLoad(string $config_file, mixed $sections = null, string $scope = 'local')
  27. */
  28. class Smarty_Internal_Data
  29. {
  30. /**
  31. * This object type (Smarty = 1, template = 2, data = 4)
  32. *
  33. * @var int
  34. */
  35. public $_objType = 4;
  36. /**
  37. * name of class used for templates
  38. *
  39. * @var string
  40. */
  41. public $template_class = 'Smarty_Internal_Template';
  42. /**
  43. * template variables
  44. *
  45. * @var Smarty_Variable[]
  46. */
  47. public $tpl_vars = array();
  48. /**
  49. * parent template (if any)
  50. *
  51. * @var Smarty|Smarty_Internal_Template|Smarty_Internal_Data
  52. */
  53. public $parent = null;
  54. /**
  55. * configuration settings
  56. *
  57. * @var string[]
  58. */
  59. public $config_vars = array();
  60. /**
  61. * extension handler
  62. *
  63. * @var Smarty_Internal_Extension_Handler
  64. */
  65. public $ext = null;
  66. /**
  67. * Smarty_Internal_Data constructor.
  68. *
  69. * Install extension handler
  70. */
  71. public function __construct()
  72. {
  73. $this->ext = new Smarty_Internal_Extension_Handler();
  74. $this->ext->objType = $this->_objType;
  75. }
  76. /**
  77. * assigns a Smarty variable
  78. *
  79. * @param array|string $tpl_var the template variable name(s)
  80. * @param mixed $value the value to assign
  81. * @param boolean $nocache if true any output of this variable will be not cached
  82. *
  83. * @return Smarty_Internal_Data current Smarty_Internal_Data (or Smarty or Smarty_Internal_Template) instance for
  84. * chaining
  85. */
  86. public function assign($tpl_var, $value = null, $nocache = false)
  87. {
  88. if (is_array($tpl_var)) {
  89. foreach ($tpl_var as $_key => $_val) {
  90. $this->assign($_key, $_val, $nocache);
  91. }
  92. } else {
  93. if ($tpl_var != '') {
  94. if ($this->_objType == 2) {
  95. /** @var Smarty_Internal_Template $this */
  96. $this->_assignInScope($tpl_var, $value, $nocache);
  97. } else {
  98. $this->tpl_vars[ $tpl_var ] = new Smarty_Variable($value, $nocache);
  99. }
  100. }
  101. }
  102. return $this;
  103. }
  104. /**
  105. * appends values to template variables
  106. *
  107. * @api Smarty::append()
  108. * @link http://www.smarty.net/docs/en/api.append.tpl
  109. *
  110. * @param array|string $tpl_var the template variable name(s)
  111. * @param mixed $value the value to append
  112. * @param bool $merge flag if array elements shall be merged
  113. * @param bool $nocache if true any output of this variable will
  114. * be not cached
  115. *
  116. * @return \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty
  117. */
  118. public function append($tpl_var, $value = null, $merge = false, $nocache = false)
  119. {
  120. return $this->ext->append->append($this, $tpl_var, $value, $merge, $nocache);
  121. }
  122. /**
  123. * assigns a global Smarty variable
  124. *
  125. * @param string $varName the global variable name
  126. * @param mixed $value the value to assign
  127. * @param boolean $nocache if true any output of this variable will be not cached
  128. *
  129. * @return \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty
  130. */
  131. public function assignGlobal($varName, $value = null, $nocache = false)
  132. {
  133. return $this->ext->assignGlobal->assignGlobal($this, $varName, $value, $nocache);
  134. }
  135. /**
  136. * appends values to template variables by reference
  137. *
  138. * @param string $tpl_var the template variable name
  139. * @param mixed &$value the referenced value to append
  140. * @param boolean $merge flag if array elements shall be merged
  141. *
  142. * @return \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty
  143. */
  144. public function appendByRef($tpl_var, &$value, $merge = false)
  145. {
  146. return $this->ext->appendByRef->appendByRef($this, $tpl_var, $value, $merge);
  147. }
  148. /**
  149. * assigns values to template variables by reference
  150. *
  151. * @param string $tpl_var the template variable name
  152. * @param $value
  153. * @param boolean $nocache if true any output of this variable will be not cached
  154. *
  155. * @return \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty
  156. */
  157. public function assignByRef($tpl_var, &$value, $nocache = false)
  158. {
  159. return $this->ext->assignByRef->assignByRef($this, $tpl_var, $value, $nocache);
  160. }
  161. /**
  162. * Returns a single or all template variables
  163. *
  164. * @api Smarty::getTemplateVars()
  165. * @link http://www.smarty.net/docs/en/api.get.template.vars.tpl
  166. *
  167. * @param string $varName variable name or null
  168. * @param \Smarty_Internal_Data|\Smarty_Internal_Template|\Smarty $_ptr optional pointer to data object
  169. * @param bool $searchParents include parent templates?
  170. *
  171. * @return mixed variable value or or array of variables
  172. */
  173. public function getTemplateVars($varName = null, Smarty_Internal_Data $_ptr = null, $searchParents = true)
  174. {
  175. return $this->ext->getTemplateVars->getTemplateVars($this, $varName, $_ptr, $searchParents);
  176. }
  177. /**
  178. * gets the object of a Smarty variable
  179. *
  180. * @param string $variable the name of the Smarty variable
  181. * @param Smarty_Internal_Data $_ptr optional pointer to data object
  182. * @param boolean $searchParents search also in parent data
  183. * @param bool $error_enable
  184. *
  185. * @return Smarty_Variable|Smarty_Undefined_Variable the object of the variable
  186. * @deprecated since 3.1.28 please use Smarty_Internal_Data::getTemplateVars() instead.
  187. */
  188. public function getVariable($variable = null, Smarty_Internal_Data $_ptr = null, $searchParents = true,
  189. $error_enable = true)
  190. {
  191. return $this->ext->getTemplateVars->_getVariable($this, $variable, $_ptr, $searchParents, $error_enable);
  192. }
  193. /**
  194. * Follow the parent chain an merge template and config variables
  195. *
  196. * @param \Smarty_Internal_Data|null $data
  197. */
  198. public function _mergeVars(Smarty_Internal_Data $data = null)
  199. {
  200. if (isset($data)) {
  201. if (!empty($this->tpl_vars)) {
  202. $data->tpl_vars = array_merge($this->tpl_vars, $data->tpl_vars);
  203. }
  204. if (!empty($this->config_vars)) {
  205. $data->config_vars = array_merge($this->config_vars, $data->config_vars);
  206. }
  207. } else {
  208. $data = $this;
  209. }
  210. if (isset($this->parent)) {
  211. $this->parent->_mergeVars($data);
  212. }
  213. }
  214. /**
  215. * Handle unknown class methods
  216. *
  217. * @param string $name unknown method-name
  218. * @param array $args argument array
  219. *
  220. * @return mixed
  221. * @throws SmartyException
  222. */
  223. public function __call($name, $args)
  224. {
  225. return $this->ext->_callExternalMethod($this, $name, $args);
  226. }
  227. }