smarty_internal_data.php 8.9 KB

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