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