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