/
Data.php
521 lines (469 loc) · 12.8 KB
/
Data.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
<?php
namespace Smarty;
/**
* Smarty Internal Plugin Data
* This file contains the basic properties and methods for holding config and template variables
*/
class Data
{
/**
* define variable scopes
*/
const SCOPE_LOCAL = 1;
const SCOPE_PARENT = 2;
const SCOPE_TPL_ROOT = 4;
const SCOPE_ROOT = 8;
const SCOPE_SMARTY = 16;
const SCOPE_GLOBAL = 32;
/**
* Global smarty instance
*
* @var Smarty
*/
protected $smarty = null;
/**
* template variables
*
* @var Variable[]
*/
public $tpl_vars = array();
/**
* parent data container (if any)
*
* @var Data
*/
public $parent = null;
/**
* configuration settings
*
* @var string[]
*/
public $config_vars = array();
/**
* This variable will hold a stack of template variables.
*
* @var null|array
*/
private $_var_stack = [];
/**
* This variable will hold a stack of config variables.
*
* @var null|array
*/
private $_config_stack = [];
/**
* Default scope for new variables
* @var int
*/
protected $defaultScope = self::SCOPE_LOCAL;
/**
* create Smarty data object
*
* @param Smarty|array $_parent parent template
* @param Smarty|Template $smarty global smarty instance
* @param string $name optional data block name
*
* @throws Exception
*/
public function __construct($_parent = null, $smarty = null, $name = null) {
$this->smarty = $smarty;
if (is_object($_parent)) {
// when object set up back pointer
$this->parent = $_parent;
} elseif (is_array($_parent)) {
// set up variable values
foreach ($_parent as $_key => $_val) {
$this->assign($_key, $_val);
}
} elseif ($_parent !== null) {
throw new Exception('Wrong type for template variables');
}
}
/**
* assigns a Smarty variable
*
* @param array|string $tpl_var the template variable name(s)
* @param mixed $value the value to assign
* @param boolean $nocache if true any output of this variable will be not cached
* @param int $scope one of self::SCOPE_* constants
*
* @return Data current Data (or Smarty or \Smarty\Template) instance for
* chaining
*/
public function assign($tpl_var, $value = null, $nocache = false, $scope = null)
{
if (is_array($tpl_var)) {
foreach ($tpl_var as $_key => $_val) {
$this->assign($_key, $_val, $nocache, $scope);
}
return $this;
}
switch ($scope ?? $this->getDefaultScope()) {
case self::SCOPE_GLOBAL:
case self::SCOPE_SMARTY:
$this->getSmarty()->assign($tpl_var, $value);
break;
case self::SCOPE_TPL_ROOT:
$ptr = $this;
while (isset($ptr->parent) && ($ptr->parent instanceof Template)) {
$ptr = $ptr->parent;
}
$ptr->assign($tpl_var, $value);
break;
case self::SCOPE_ROOT:
$ptr = $this;
while (isset($ptr->parent) && !($ptr->parent instanceof Smarty)) {
$ptr = $ptr->parent;
}
$ptr->assign($tpl_var, $value);
break;
case self::SCOPE_PARENT:
if ($this->parent) {
$this->parent->assign($tpl_var, $value);
} else {
// assign local as fallback
$this->assign($tpl_var, $value);
}
break;
case self::SCOPE_LOCAL:
default:
if (isset($this->tpl_vars[$tpl_var])) {
$this->tpl_vars[$tpl_var]->setValue($value);
if ($nocache) {
$this->tpl_vars[$tpl_var]->setNocache(true);
}
} else {
$this->tpl_vars[$tpl_var] = new Variable($value, $nocache);
}
}
return $this;
}
/**
* appends values to template variables
*
* @param array|string $tpl_var the template variable name(s)
* @param mixed $value the value to append
* @param bool $merge flag if array elements shall be merged
* @param bool $nocache if true any output of this variable will
* be not cached
*
* @return Data
* @api Smarty::append()
*/
public function append($tpl_var, $value = null, $merge = false, $nocache = false)
{
if (is_array($tpl_var)) {
foreach ($tpl_var as $_key => $_val) {
$this->append($_key, $_val, $merge, $nocache);
}
} else {
$newValue = $this->getValue($tpl_var) ?? [];
if (!is_array($newValue)) {
$newValue = (array) $newValue;
}
if ($merge && is_array($value)) {
foreach ($value as $_mkey => $_mval) {
$newValue[$_mkey] = $_mval;
}
} else {
$newValue[] = $value;
}
$this->assign($tpl_var, $newValue, $nocache);
}
return $this;
}
/**
* assigns a global Smarty variable
*
* @param string $varName the global variable name
* @param mixed $value the value to assign
* @param boolean $nocache if true any output of this variable will be not cached
*
* @return Data
* @deprecated since 5.0
*/
public function assignGlobal($varName, $value = null, $nocache = false)
{
trigger_error(__METHOD__ . " is deprecated. Use \\Smarty\\Smarty::assign() to assign a variable " .
" at the Smarty level.", E_USER_DEPRECATED);
return $this->getSmarty()->assign($varName, $value, $nocache);
}
/**
* Returns a single or all template variables
*
* @param string $varName variable name or null
* @param bool $searchParents include parent templates?
*
* @return mixed variable value or or array of variables
* @api Smarty::getTemplateVars()
*
*/
public function getTemplateVars($varName = null, $searchParents = true)
{
if (isset($varName)) {
return $this->getValue($varName, $searchParents);
}
return array_merge(
$this->parent && $searchParents ? $this->parent->getTemplateVars() : [],
array_map(function(Variable $var) { return $var->getValue(); }, $this->tpl_vars)
);
}
/**
* Wrapper for ::getVariable()
*
* @deprecated since 5.0
*
* @param $varName
* @param $searchParents
* @param $errorEnable
*
* @return void
*/
public function _getVariable($varName, $searchParents = true, $errorEnable = true) {
trigger_error('Using ::_getVariable() to is deprecated and will be ' .
'removed in a future release. Use getVariable() instead.', E_USER_DEPRECATED);
return $this->getVariable($varName, $searchParents, $errorEnable);
}
/**
* Gets the object of a Smarty variable
*
* @param string $varName the name of the Smarty variable
* @param bool $searchParents search also in parent data
* @param bool $errorEnable
*
* @return Variable
*/
public function getVariable($varName, $searchParents = true, $errorEnable = true) {
if (isset($this->tpl_vars[$varName])) {
return $this->tpl_vars[$varName];
}
if ($searchParents && $this->parent) {
return $this->parent->getVariable($varName, $searchParents, $errorEnable);
}
if ($errorEnable && $this->getSmarty()->error_unassigned) {
// force a notice
$x = $$varName;
}
return new UndefinedVariable();
}
/**
* Directly sets a complete Variable object in the variable with the given name.
* @param $varName
* @param Variable $variableObject
*
* @return void
*/
public function setVariable($varName, Variable $variableObject) {
$this->tpl_vars[$varName] = $variableObject;
}
/**
* Indicates if given variable has been set.
* @param $varName
*
* @return bool
*/
public function hasVariable($varName): bool {
return !($this->getVariable($varName) instanceof UndefinedVariable);
}
/**
* Returns the value of the Smarty\Variable given by $varName, or null if the variable does not exist.
*
* @param $varName
* @param bool $searchParents
*
* @return mixed|null
*/
public function getValue($varName, $searchParents = true) {
$variable = $this->getVariable($varName, $searchParents);
return isset($variable) ? $variable->getValue() : null;
}
/**
* load config variables into template object
*
* @param array $new_config_vars
*/
public function assignConfigVars($new_config_vars, array $sections = []) {
// copy global config vars
foreach ($new_config_vars['vars'] as $variable => $value) {
if ($this->getSmarty()->config_overwrite || !isset($this->config_vars[$variable])) {
$this->config_vars[$variable] = $value;
} else {
$this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value);
}
}
foreach ($sections as $tpl_section) {
if (isset($new_config_vars['sections'][$tpl_section])) {
foreach ($new_config_vars['sections'][$tpl_section]['vars'] as $variable => $value) {
if ($this->getSmarty()->config_overwrite || !isset($this->config_vars[$variable])) {
$this->config_vars[$variable] = $value;
} else {
$this->config_vars[$variable] = array_merge((array)$this->config_vars[$variable], (array)$value);
}
}
}
}
}
/**
* Get Smarty object
*
* @return Smarty
*/
public function getSmarty()
{
return $this->smarty;
}
/**
* clear the given assigned template variable(s).
*
* @param string|array $tpl_var the template variable(s) to clear
*
* @return Data
*
* @api Smarty::clearAssign()
*/
public function clearAssign($tpl_var)
{
if (is_array($tpl_var)) {
foreach ($tpl_var as $curr_var) {
unset($this->tpl_vars[ $curr_var ]);
}
} else {
unset($this->tpl_vars[ $tpl_var ]);
}
return $this;
}
/**
* clear all the assigned template variables.
*
* @return Data
*
* @api Smarty::clearAllAssign()
*/
public function clearAllAssign()
{
$this->tpl_vars = array();
return $this;
}
/**
* clear a single or all config variables
*
* @param string|null $name variable name or null
*
* @return Data
*
* @api Smarty::clearConfig()
*/
public function clearConfig($name = null)
{
if (isset($name)) {
unset($this->config_vars[ $name ]);
} else {
$this->config_vars = array();
}
return $this;
}
/**
* Gets a config variable value
*
* @param string $varName the name of the config variable
*
* @return mixed the value of the config variable
* @throws Exception
*/
public function getConfigVariable($varName)
{
if (isset($this->config_vars[$varName])) {
return $this->config_vars[$varName];
}
$returnValue = $this->parent ? $this->parent->getConfigVariable($varName) : null;
if ($returnValue === null && $this->getSmarty()->error_unassigned) {
throw new Exception("Undefined variable $varName");
}
return $returnValue;
}
public function hasConfigVariable($varName): bool {
try {
return $this->getConfigVariable($varName) !== null;
} catch (Exception $e) {
return false;
}
}
/**
* Returns a single or all config variables
*
* @param string $varname variable name or null
*
* @return mixed variable value or or array of variables
* @throws Exception
*
* @api Smarty::getConfigVars()
*/
public function getConfigVars($varname = null)
{
if (isset($varname)) {
return $this->getConfigVariable($varname);
}
return array_merge($this->parent ? $this->parent->getConfigVars() : [], $this->config_vars);
}
/**
* load a config file, optionally load just selected sections
*
* @param string $config_file filename
* @param mixed $sections array of section names, single
* section or null
* @returns $this
* @throws \Exception
*
* @api Smarty::configLoad()
*/
public function configLoad($config_file, $sections = null)
{
$template = $this->getSmarty()->doCreateTemplate($config_file, null, null, $this, null, null, true);
$template->caching = Smarty::CACHING_OFF;
$template->assign('sections', (array) $sections ?? []);
// trigger a call to $this->assignConfigVars
$template->fetch();
return $this;
}
/**
* Sets the default scope for new variables assigned in this template.
* @param int $scope
*
* @return void
*/
protected function setDefaultScope(int $scope) {
$this->defaultScope = $scope;
}
/**
* Returns the default scope for new variables assigned in this template.
* @return int
*/
public function getDefaultScope(): int {
return $this->defaultScope;
}
/**
* @return Data|Smarty|null
*/
public function getParent() {
return $this->parent;
}
/**
* @param Data|Smarty|null $parent
*/
public function setParent($parent): void {
$this->parent = $parent;
}
public function pushStack(): void {
$stackList = [];
foreach ($this->tpl_vars as $name => $variable) {
$stackList[$name] = clone $variable; // variables are stored in Variable objects
}
$this->_var_stack[] = $this->tpl_vars;
$this->tpl_vars = $stackList;
$this->_config_stack[] = $this->config_vars;
}
public function popStack(): void {
$this->tpl_vars = array_pop($this->_var_stack);
$this->config_vars = array_pop($this->_config_stack);
}
}