Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
/**
3
 * Smarty Internal Plugin Smarty Template  Base
4
 * This file contains the basic shared methods for template handling
5
 *
6
 * @package    Smarty
7
 * @subpackage Template
8
 * @author     Uwe Tews
9
 */
10
 
11
/**
12
 * Class with shared smarty/template methods
13
 *
14
 * @package    Smarty
15
 * @subpackage Template
16
 *
17
 * @property int $_objType
18
 *
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 Smarty_Internal_TemplateBase addAutoloadFilters(mixed $filters, string $type = null)
23
 * @method Smarty_Internal_TemplateBase addDefaultModifiers(mixed $modifiers)
24
 * @method Smarty_Internal_TemplateBase addLiterals(mixed $literals)
25
 * @method Smarty_Internal_TemplateBase createData(Smarty_Internal_Data $parent = null, string $name = null)
26
 * @method array getAutoloadFilters(string $type = null)
27
 * @method string getDebugTemplate()
28
 * @method array getDefaultModifier()
29
 * @method array getLiterals()
30
 * @method array getTags(mixed $template = null)
31
 * @method object getRegisteredObject(string $object_name)
32
 * @method Smarty_Internal_TemplateBase registerCacheResource(string $name, Smarty_CacheResource $resource_handler)
33
 * @method Smarty_Internal_TemplateBase registerClass(string $class_name, string $class_impl)
34
 * @method Smarty_Internal_TemplateBase registerDefaultConfigHandler(callback $callback)
35
 * @method Smarty_Internal_TemplateBase registerDefaultPluginHandler(callback $callback)
36
 * @method Smarty_Internal_TemplateBase registerDefaultTemplateHandler(callback $callback)
37
 * @method Smarty_Internal_TemplateBase registerResource(string $name, mixed $resource_handler)
38
 * @method Smarty_Internal_TemplateBase setAutoloadFilters(mixed $filters, string $type = null)
39
 * @method Smarty_Internal_TemplateBase setDebugTemplate(string $tpl_name)
40
 * @method Smarty_Internal_TemplateBase setDefaultModifiers(mixed $modifiers)
41
 * @method Smarty_Internal_TemplateBase setLiterals(mixed $literals)
42
 * @method Smarty_Internal_TemplateBase unloadFilter(string $type, string $name)
43
 * @method Smarty_Internal_TemplateBase unregisterCacheResource(string $name)
44
 * @method Smarty_Internal_TemplateBase unregisterObject(string $object_name)
45
 * @method Smarty_Internal_TemplateBase unregisterPlugin(string $type, string $name)
46
 * @method Smarty_Internal_TemplateBase unregisterFilter(string $type, mixed $callback)
47
 * @method Smarty_Internal_TemplateBase unregisterResource(string $name)
48
 */
49
abstract class Smarty_Internal_TemplateBase extends Smarty_Internal_Data
50
{
51
    /**
52
     * Set this if you want different sets of cache files for the same
53
     * templates.
54
     *
55
     * @var string
56
     */
57
    public $cache_id = null;
58
 
59
    /**
60
     * Set this if you want different sets of compiled files for the same
61
     * templates.
62
     *
63
     * @var string
64
     */
65
    public $compile_id = null;
66
 
67
    /**
68
     * caching enabled
69
     *
70
     * @var int
71
     */
72
    public $caching = Smarty::CACHING_OFF;
73
 
74
    /**
75
     * check template for modifications?
76
     *
77
     * @var int
78
     */
79
    public $compile_check = Smarty::COMPILECHECK_ON;
80
 
81
    /**
82
     * cache lifetime in seconds
83
     *
84
     * @var integer
85
     */
86
    public $cache_lifetime = 3600;
87
 
88
    /**
89
     * Array of source information for known template functions
90
     *
91
     * @var array
92
     */
93
    public $tplFunctions = array();
94
 
95
    /**
96
     * universal cache
97
     *
98
     * @var array()
99
     */
100
    public $_cache = array();
101
 
102
    /**
103
     * fetches a rendered Smarty template
104
     *
105
     * @param string $template   the resource handle of the template file or template object
106
     * @param mixed  $cache_id   cache id to be used with this template
107
     * @param mixed  $compile_id compile id to be used with this template
108
     * @param object $parent     next higher level of Smarty variables
109
     *
110
     * @throws Exception
111
     * @throws SmartyException
112
     * @return string rendered template output
113
     */
114
    public function fetch($template = null, $cache_id = null, $compile_id = null, $parent = null)
115
    {
116
        $result = $this->_execute($template, $cache_id, $compile_id, $parent, 0);
117
        return $result === null ? ob_get_clean() : $result;
118
    }
119
 
120
    /**
121
     * displays a Smarty template
122
     *
123
     * @param string $template   the resource handle of the template file or template object
124
     * @param mixed  $cache_id   cache id to be used with this template
125
     * @param mixed  $compile_id compile id to be used with this template
126
     * @param object $parent     next higher level of Smarty variables
127
     *
128
     * @throws \Exception
129
     * @throws \SmartyException
130
     */
131
    public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
132
    {
133
        // display template
134
        $this->_execute($template, $cache_id, $compile_id, $parent, 1);
135
    }
136
 
137
    /**
138
     * test if cache is valid
139
     *
140
     * @api  Smarty::isCached()
141
     * @link https://www.smarty.net/docs/en/api.is.cached.tpl
142
     *
143
     * @param null|string|\Smarty_Internal_Template $template   the resource handle of the template file or template
144
     *                                                          object
145
     * @param mixed                                 $cache_id   cache id to be used with this template
146
     * @param mixed                                 $compile_id compile id to be used with this template
147
     * @param object                                $parent     next higher level of Smarty variables
148
     *
149
     * @return bool cache status
150
     * @throws \Exception
151
     * @throws \SmartyException
152
     */
153
    public function isCached($template = null, $cache_id = null, $compile_id = null, $parent = null)
154
    {
155
        return $this->_execute($template, $cache_id, $compile_id, $parent, 2);
156
    }
157
 
158
    /**
159
     * fetches a rendered Smarty template
160
     *
161
     * @param string $template   the resource handle of the template file or template object
162
     * @param mixed  $cache_id   cache id to be used with this template
163
     * @param mixed  $compile_id compile id to be used with this template
164
     * @param object $parent     next higher level of Smarty variables
165
     * @param string $function   function type 0 = fetch,  1 = display, 2 = isCache
166
     *
167
     * @return mixed
168
     * @throws \Exception
169
     * @throws \SmartyException
170
     */
171
    private function _execute($template, $cache_id, $compile_id, $parent, $function)
172
    {
173
        $smarty = $this->_getSmartyObj();
174
        $saveVars = true;
175
        if ($template === null) {
176
            if (!$this->_isTplObj()) {
177
                throw new SmartyException($function . '():Missing \'$template\' parameter');
178
            } else {
179
                $template = $this;
180
            }
181
        } elseif (is_object($template)) {
182
            /* @var Smarty_Internal_Template $template */
183
            if (!isset($template->_objType) || !$template->_isTplObj()) {
184
                throw new SmartyException($function . '():Template object expected');
185
            }
186
        } else {
187
            // get template object
188
            $saveVars = false;
189
            $template = $smarty->createTemplate($template, $cache_id, $compile_id, $parent ? $parent : $this, false);
190
            if ($this->_objType === 1) {
191
                // set caching in template object
192
                $template->caching = $this->caching;
193
            }
194
        }
195
        // make sure we have integer values
196
        $template->caching = (int)$template->caching;
197
        // fetch template content
198
        $level = ob_get_level();
199
        try {
200
            $_smarty_old_error_level =
201
                isset($smarty->error_reporting) ? error_reporting($smarty->error_reporting) : null;
202
 
203
            if ($smarty->isMutingUndefinedOrNullWarnings()) {
204
                $errorHandler = new Smarty_Internal_ErrorHandler();
205
                $errorHandler->activate();
206
            }
207
 
208
            if ($this->_objType === 2) {
209
                /* @var Smarty_Internal_Template $this */
210
                $template->tplFunctions = $this->tplFunctions;
211
                $template->inheritance = $this->inheritance;
212
            }
213
            /* @var Smarty_Internal_Template $parent */
214
            if (isset($parent->_objType) && ($parent->_objType === 2) && !empty($parent->tplFunctions)) {
215
                $template->tplFunctions = array_merge($parent->tplFunctions, $template->tplFunctions);
216
            }
217
            if ($function === 2) {
218
                if ($template->caching) {
219
                    // return cache status of template
220
                    if (!isset($template->cached)) {
221
                        $template->loadCached();
222
                    }
223
                    $result = $template->cached->isCached($template);
224
                    Smarty_Internal_Template::$isCacheTplObj[ $template->_getTemplateId() ] = $template;
225
                } else {
226
                    return false;
227
                }
228
            } else {
229
                if ($saveVars) {
230
                    $savedTplVars = $template->tpl_vars;
231
                    $savedConfigVars = $template->config_vars;
232
                }
233
                ob_start();
234
                $template->_mergeVars();
235
                if (!empty(Smarty::$global_tpl_vars)) {
236
                    $template->tpl_vars = array_merge(Smarty::$global_tpl_vars, $template->tpl_vars);
237
                }
238
                $result = $template->render(false, $function);
239
                $template->_cleanUp();
240
                if ($saveVars) {
241
                    $template->tpl_vars = $savedTplVars;
242
                    $template->config_vars = $savedConfigVars;
243
                } else {
244
                    if (!$function && !isset(Smarty_Internal_Template::$tplObjCache[ $template->templateId ])) {
245
                        $template->parent = null;
246
                        $template->tpl_vars = $template->config_vars = array();
247
                        Smarty_Internal_Template::$tplObjCache[ $template->templateId ] = $template;
248
                    }
249
                }
250
            }
251
 
252
            if (isset($errorHandler)) {
253
                $errorHandler->deactivate();
254
            }
255
 
256
            if (isset($_smarty_old_error_level)) {
257
                error_reporting($_smarty_old_error_level);
258
            }
259
            return $result;
260
        } catch (Throwable $e) {
261
            while (ob_get_level() > $level) {
262
                ob_end_clean();
263
            }
264
            if (isset($errorHandler)) {
265
                $errorHandler->deactivate();
266
            }
267
 
268
            if (isset($_smarty_old_error_level)) {
269
                error_reporting($_smarty_old_error_level);
270
            }
271
            throw $e;
272
        }
273
    }
274
 
275
    /**
276
     * Registers plugin to be used in templates
277
     *
278
     * @api  Smarty::registerPlugin()
279
     * @link https://www.smarty.net/docs/en/api.register.plugin.tpl
280
     *
281
     * @param string   $type       plugin type
282
     * @param string   $name       name of template tag
283
     * @param callable $callback   PHP callback to register
284
     * @param bool     $cacheable  if true (default) this function is cache able
285
     * @param mixed    $cache_attr caching attributes if any
286
     *
287
     * @return \Smarty|\Smarty_Internal_Template
288
     * @throws \SmartyException
289
     */
290
    public function registerPlugin($type, $name, $callback, $cacheable = true, $cache_attr = null)
291
    {
292
        return $this->ext->registerPlugin->registerPlugin($this, $type, $name, $callback, $cacheable, $cache_attr);
293
    }
294
 
295
    /**
296
     * load a filter of specified type and name
297
     *
298
     * @api  Smarty::loadFilter()
299
     * @link https://www.smarty.net/docs/en/api.load.filter.tpl
300
     *
301
     * @param string $type filter type
302
     * @param string $name filter name
303
     *
304
     * @return bool
305
     * @throws \SmartyException
306
     */
307
    public function loadFilter($type, $name)
308
    {
309
        return $this->ext->loadFilter->loadFilter($this, $type, $name);
310
    }
311
 
312
    /**
313
     * Registers a filter function
314
     *
315
     * @api  Smarty::registerFilter()
316
     * @link https://www.smarty.net/docs/en/api.register.filter.tpl
317
     *
318
     * @param string      $type filter type
319
     * @param callable    $callback
320
     * @param string|null $name optional filter name
321
     *
322
     * @return \Smarty|\Smarty_Internal_Template
323
     * @throws \SmartyException
324
     */
325
    public function registerFilter($type, $callback, $name = null)
326
    {
327
        return $this->ext->registerFilter->registerFilter($this, $type, $callback, $name);
328
    }
329
 
330
    /**
331
     * Registers object to be used in templates
332
     *
333
     * @api  Smarty::registerObject()
334
     * @link https://www.smarty.net/docs/en/api.register.object.tpl
335
     *
336
     * @param string $object_name
337
     * @param object $object                     the referenced PHP object to register
338
     * @param array  $allowed_methods_properties list of allowed methods (empty = all)
339
     * @param bool   $format                     smarty argument format, else traditional
340
     * @param array  $block_methods              list of block-methods
341
     *
342
     * @return \Smarty|\Smarty_Internal_Template
343
     * @throws \SmartyException
344
     */
345
    public function registerObject(
346
        $object_name,
347
        $object,
348
        $allowed_methods_properties = array(),
349
        $format = true,
350
        $block_methods = array()
351
    ) {
352
        return $this->ext->registerObject->registerObject(
353
            $this,
354
            $object_name,
355
            $object,
356
            $allowed_methods_properties,
357
            $format,
358
            $block_methods
359
        );
360
    }
361
 
362
    /**
363
     * @param int $compile_check
364
     */
365
    public function setCompileCheck($compile_check)
366
    {
367
        $this->compile_check = (int)$compile_check;
368
    }
369
 
370
    /**
371
     * @param int $caching
372
     */
373
    public function setCaching($caching)
374
    {
375
        $this->caching = (int)$caching;
376
    }
377
 
378
    /**
379
     * @param int $cache_lifetime
380
     */
381
    public function setCacheLifetime($cache_lifetime)
382
    {
383
        $this->cache_lifetime = $cache_lifetime;
384
    }
385
 
386
    /**
387
     * @param string $compile_id
388
     */
389
    public function setCompileId($compile_id)
390
    {
391
        $this->compile_id = $compile_id;
392
    }
393
 
394
    /**
395
     * @param string $cache_id
396
     */
397
    public function setCacheId($cache_id)
398
    {
399
        $this->cache_id = $cache_id;
400
    }
401
}