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_data.php 8.9KB

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