/
TemplateBase.php
425 lines (388 loc) · 9.82 KB
/
TemplateBase.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
<?php
/**
* Smarty Internal Plugin Smarty Template Base
* This file contains the basic shared methods for template handling
*
* @author Uwe Tews
*/
namespace Smarty;
/**
* Class with shared smarty/template methods
*/
abstract class TemplateBase extends Data {
/**
* Set this if you want different sets of cache files for the same
* templates.
*
* @var string
*/
public $cache_id = null;
/**
* Set this if you want different sets of compiled files for the same
* templates.
*
* @var string
*/
public $compile_id = null;
/**
* caching enabled
*
* @var int
*/
public $caching = \Smarty\Smarty::CACHING_OFF;
/**
* check template for modifications?
*
* @var int
*/
public $compile_check = \Smarty\Smarty::COMPILECHECK_ON;
/**
* cache lifetime in seconds
*
* @var int
*/
public $cache_lifetime = 3600;
/**
* Array of source information for known template functions
*
* @var array
*/
public $tplFunctions = [];
/**
* @var Debug
*/
private $debug;
/**
* Registers object to be used in templates
*
* @param string $object_name
* @param object $object the referenced PHP object to register
* @param array $allowed_methods_properties list of allowed methods (empty = all)
* @param bool $format smarty argument format, else traditional
* @param array $block_methods list of block-methods
*
* @return static
* @throws \Smarty\Exception
*
* @api Smarty::registerObject()
*/
public function registerObject(
$object_name,
$object,
$allowed_methods_properties = [],
$format = true,
$block_methods = []
) {
$smarty = $this->getSmarty();
// test if allowed methods callable
if (!empty($allowed_methods_properties)) {
foreach ((array)$allowed_methods_properties as $method) {
if (!is_callable([$object, $method]) && !property_exists($object, $method)) {
throw new Exception("Undefined method or property '$method' in registered object");
}
}
}
// test if block methods callable
if (!empty($block_methods)) {
foreach ((array)$block_methods as $method) {
if (!is_callable([$object, $method])) {
throw new Exception("Undefined method '$method' in registered object");
}
}
}
// register the object
$smarty->registered_objects[$object_name] =
[$object, (array)$allowed_methods_properties, (boolean)$format, (array)$block_methods];
return $this;
}
/**
* Registers plugin to be used in templates
*
* @param string $object_name name of object
*
* @return static
* @api Smarty::unregisterObject()
*
*/
public function unregisterObject($object_name) {
$smarty = $this->getSmarty();
if (isset($smarty->registered_objects[$object_name])) {
unset($smarty->registered_objects[$object_name]);
}
return $this;
}
/**
* @return int
*/
public function getCompileCheck(): int {
return $this->compile_check;
}
/**
* @param int $compile_check
*/
public function setCompileCheck($compile_check) {
$this->compile_check = (int)$compile_check;
}
/**
* @param int $caching
*/
public function setCaching($caching) {
$this->caching = (int)$caching;
}
/**
* @param int $cache_lifetime
*/
public function setCacheLifetime($cache_lifetime) {
$this->cache_lifetime = $cache_lifetime;
}
/**
* @param string $compile_id
*/
public function setCompileId($compile_id) {
$this->compile_id = $compile_id;
}
/**
* @param string $cache_id
*/
public function setCacheId($cache_id) {
$this->cache_id = $cache_id;
}
/**
* creates a data object
*
* @param Data|null $parent next higher level of Smarty
* variables
* @param null $name optional data block name
*
* @return Data data object
* @throws Exception
* @api Smarty::createData()
*
*/
public function createData(Data $parent = null, $name = null) {
/* @var Smarty $smarty */
$smarty = $this->getSmarty();
$dataObj = new Data($parent, $smarty, $name);
if ($smarty->debugging) {
$smarty->getDebug()->register_data($dataObj);
}
return $dataObj;
}
/**
* return name of debugging template
*
* @return string
* @api Smarty::getDebugTemplate()
*
*/
public function getDebugTemplate() {
$smarty = $this->getSmarty();
return $smarty->debug_tpl;
}
/**
* @return Debug
*/
public function getDebug(): Debug {
if (!isset($this->debug)) {
$this->debug = new \Smarty\Debug();
}
return $this->debug;
}
/**
* return a reference to a registered object
*
* @param string $object_name object name
*
* @return object
* @throws \Smarty\Exception if no such object is found
*
* @api Smarty::getRegisteredObject()
*/
public function getRegisteredObject($object_name) {
$smarty = $this->getSmarty();
if (!isset($smarty->registered_objects[$object_name])) {
throw new Exception("'$object_name' is not a registered object");
}
if (!is_object($smarty->registered_objects[$object_name][0])) {
throw new Exception("registered '$object_name' is not an object");
}
return $smarty->registered_objects[$object_name][0];
}
/**
* Get literals
*
* @return array list of literals
* @api Smarty::getLiterals()
*
*/
public function getLiterals() {
$smarty = $this->getSmarty();
return (array)$smarty->literals;
}
/**
* Add literals
*
* @param array|string $literals literal or list of literals
* to addto add
*
* @return static
* @throws \Smarty\Exception
* @api Smarty::addLiterals()
*
*/
public function addLiterals($literals = null) {
if (isset($literals)) {
$this->_setLiterals($this->getSmarty(), (array)$literals);
}
return $this;
}
/**
* Set literals
*
* @param array|string $literals literal or list of literals
* to setto set
*
* @return static
* @throws \Smarty\Exception
* @api Smarty::setLiterals()
*
*/
public function setLiterals($literals = null) {
$smarty = $this->getSmarty();
$smarty->literals = [];
if (!empty($literals)) {
$this->_setLiterals($smarty, (array)$literals);
}
return $this;
}
/**
* common setter for literals for easier handling of duplicates the
* Smarty::$literals array gets filled with identical key values
*
* @param Smarty $smarty
* @param array $literals
*
* @throws \Smarty\Exception
*/
private function _setLiterals(Smarty $smarty, $literals) {
$literals = array_combine($literals, $literals);
$error = isset($literals[$smarty->getLeftDelimiter()]) ? [$smarty->getLeftDelimiter()] : [];
$error = isset($literals[$smarty->getRightDelimiter()]) ? $error[] = $smarty->getRightDelimiter() : $error;
if (!empty($error)) {
throw new Exception(
'User defined literal(s) "' . $error .
'" may not be identical with left or right delimiter'
);
}
$smarty->literals = array_merge((array)$smarty->literals, (array)$literals);
}
/**
* Registers static classes to be used in templates
*
* @param string $class_name
* @param string $class_impl the referenced PHP class to
* register
*
* @return static
* @throws \Smarty\Exception
* @api Smarty::registerClass()
*
*/
public function registerClass($class_name, $class_impl) {
$smarty = $this->getSmarty();
// test if exists
if (!class_exists($class_impl)) {
throw new Exception("Undefined class '$class_impl' in register template class");
}
// register the class
$smarty->registered_classes[$class_name] = $class_impl;
return $this;
}
/**
* Register config default handler
*
* @param callable $callback class/method name
*
* @return static
* @throws Exception if $callback is not callable
* @api Smarty::registerDefaultConfigHandler()
*
*/
public function registerDefaultConfigHandler($callback) {
$smarty = $this->getSmarty();
if (is_callable($callback)) {
$smarty->default_config_handler_func = $callback;
} else {
throw new Exception('Default config handler not callable');
}
return $this;
}
/**
* Register template default handler
*
* @param callable $callback class/method name
*
* @return static
* @throws Exception if $callback is not callable
* @api Smarty::registerDefaultTemplateHandler()
*
*/
public function registerDefaultTemplateHandler($callback) {
$smarty = $this->getSmarty();
if (is_callable($callback)) {
$smarty->default_template_handler_func = $callback;
} else {
throw new Exception('Default template handler not callable');
}
return $this;
}
/**
* Registers a resource to fetch a template
*
* @param string $name name of resource type
* @param \Smarty\Resource\BasePlugin $resource_handler instance of Smarty\Resource\BasePlugin
*
* @return static
*
* @api Smarty::registerResource()
*/
public function registerResource($name, \Smarty\Resource\BasePlugin $resource_handler) {
$smarty = $this->getSmarty();
$smarty->registered_resources[$name] = $resource_handler;
return $this;
}
/**
* Unregisters a resource to fetch a template
*
* @param string $type name of resource type
*
* @return static
* @api Smarty::unregisterResource()
*
*/
public function unregisterResource($type) {
$smarty = $this->getSmarty();
if (isset($smarty->registered_resources[$type])) {
unset($smarty->registered_resources[$type]);
}
return $this;
}
/**
* set the debug template
*
* @param string $tpl_name
*
* @return static
* @throws Exception if file is not readable
* @api Smarty::setDebugTemplate()
*
*/
public function setDebugTemplate($tpl_name) {
$smarty = $this->getSmarty();
if (!is_readable($tpl_name)) {
throw new Exception("Unknown file '{$tpl_name}'");
}
$smarty->debug_tpl = $tpl_name;
return $this;
}
}