Adapter::fetch()   F
last analyzed

Complexity

Conditions 16
Paths 984

Duplication

Lines 0
Ratio 0 %

Size

Total Lines 70
Code Lines 47

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 16
eloc 47
nc 984
nop 4
dl 0
loc 70
rs 2.8571
c 1
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Copyright (c) 2013-2016
4
 *
5
 * @category  Library
6
 * @package   Dwoo\Smarty
7
 * @author    Jordi Boggiano <[email protected]>
8
 * @author    David Sanchez <[email protected]>
9
 * @copyright 2008-2013 Jordi Boggiano
10
 * @copyright 2013-2016 David Sanchez
11
 * @license   http://dwoo.org/LICENSE Modified BSD License
12
 * @version   1.3.0
13
 * @date      2016-09-23
14
 * @link      http://dwoo.org/
15
 */
16
17
namespace Dwoo\Smarty;
18
19
use Dwoo\Core;
20
use Dwoo\Compiler;
21
use Dwoo\Data;
22
use Dwoo\Security\Policy as SecurityPolicy;
23
use Dwoo\Exception as Exception;
24
use Dwoo\Template\File as TemplateFile;
25
use Dwoo\Smarty\Filter\Adapter as FilterAdapter;
26
use Dwoo\Smarty\Processor\Adapter as ProcessorAdapter;
27
28
if (!defined('DIR_SEP')) {
29
    define('DIR_SEP', DIRECTORY_SEPARATOR);
30
}
31
32
if (!defined('SMARTY_PHP_PASSTHRU')) {
33
    define('SMARTY_PHP_PASSTHRU', 0);
34
    define('SMARTY_PHP_QUOTE', 1);
35
    define('SMARTY_PHP_REMOVE', 2);
36
    define('SMARTY_PHP_ALLOW', 3);
37
}
38
39
/**
40
 * A Smarty compatibility layer for Dwoo.
41
 * This software is provided 'as-is', without any express or implied warranty.
42
 * In no event will the authors be held liable for any damages arising from the use of this software.
43
 */
44
class Adapter extends Core
45
{
46
    /**
47
     * Magic get/set/call functions that handle unsupported features
48
     *
49
     * @param string $p
50
     * @param string $v
51
     */
52
    public function __set($p, $v)
53
    {
54
        if ($p === 'scope') {
55
            $this->scope = $v;
56
57
            return;
58
        }
59
        if ($p === 'data') {
60
            $this->data = $v;
0 ignored issues
show
Documentation Bug introduced by DSanchez
It seems like $v of type string is incompatible with the declared type array of property $data.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
61
62
            return;
63
        }
64 View Code Duplication
        if (array_key_exists($p, $this->compat['properties']) !== false) {
0 ignored issues
show
Duplication introduced by Seldaek
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            if ($this->show_compat_errors) {
66
                $this->triggerError('Property ' . $p . ' is not available in the Dwoo\Smarty\Adapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE);
67
            }
68
            $this->compat['properties'][$p] = $v;
69
        } else {
70
            if ($this->show_compat_errors) {
71
                $this->triggerError('Property ' . $p . ' is not available in the Dwoo\Smarty\Adapter, but it is not listed as such, so you might want to tell me about it at [email protected]', E_USER_NOTICE);
72
            }
73
        }
74
    }
75
76
    /**
77
     * @param $p
78
     *
79
     * @return mixed
80
     */
81
    public function __get($p)
82
    {
83 View Code Duplication
        if (array_key_exists($p, $this->compat['properties']) !== false) {
0 ignored issues
show
Duplication introduced by Seldaek
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            if ($this->show_compat_errors) {
85
                $this->triggerError('Property ' . $p . ' is not available in the Dwoo\Smarty\Adapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE);
86
            }
87
88
            return $this->compat['properties'][$p];
89
        } else {
90
            if ($this->show_compat_errors) {
91
                $this->triggerError('Property ' . $p . ' is not available in the Dwoo\Smarty\Adapter, but it is not listed as such, so you might want to tell me about it at [email protected]', E_USER_NOTICE);
92
            }
93
        }
94
    }
95
96
    /**
97
     * @param string $m
98
     * @param array  $a
99
     *
100
     * @return mixed|void
101
     */
102
    public function __call($m, $a)
103
    {
104
        if (method_exists($this->dataProvider, $m)) {
105
            call_user_func_array(
106
                array(
107
                $this->dataProvider,
108
                $m
109
                ), $a
110
            );
111
        } elseif ($this->show_compat_errors) {
112
            if (array_search($m, $this->compat['methods']) !== false) {
113
                $this->triggerError('Method ' . $m . ' is not available in the Dwoo\Smarty\Adapter, however it might be implemented in the future, check out http://wiki.dwoo.org/index.php/SmartySupport for more details.', E_USER_NOTICE);
114
            } else {
115
                $this->triggerError('Method ' . $m . ' is not available in the Dwoo\Smarty\Adapter, but it is not listed as such, so you might want to tell me about it at [email protected]', E_USER_NOTICE);
116
            }
117
        }
118
    }
119
120
    /**
121
     * List of unsupported properties and methods
122
     */
123
    protected $compat = array(
124
        'methods'    => array(
125
            'register_resource',
126
            'unregister_resource',
127
            'load_filter',
128
            'clear_compiled_tpl',
129
            'clear_config',
130
            'get_config_vars',
131
            'config_load',
132
        ),
133
        'properties' => array(
134
            'cache_handler_func'            => null,
135
            'debugging'                     => false,
136
            'error_reporting'               => null,
137
            'debugging_ctrl'                => 'NONE',
138
            'request_vars_order'            => 'EGPCS',
139
            'request_use_auto_globals'      => true,
140
            'use_sub_dirs'                  => false,
141
            'autoload_filters'              => array(),
142
            'default_template_handler_func' => '',
143
            'debug_tpl'                     => '',
144
            'cache_modified_check'          => false,
145
            'default_modifiers'             => array(),
146
            'default_resource_type'         => 'file',
147
            'config_overwrite'              => true,
148
            'config_booleanize'             => true,
149
            'config_read_hidden'            => false,
150
            'config_fix_newlines'           => true,
151
            'config_class'                  => 'Config_File',
152
        ),
153
    );
154
155
    /**
156
     * Security vars
157
     */
158
    public $security          = false;
159
    public $trusted_dir       = array();
160
    public $secure_dir        = array();
161
    public $php_handling      = SMARTY_PHP_PASSTHRU;
162
    public $security_settings = array(
163
        'PHP_HANDLING'    => false,
164
        'IF_FUNCS'        => array(
165
            'list',
166
            'empty',
167
            'count',
168
            'sizeof',
169
            'in_array',
170
            'is_array',
171
        ),
172
        'INCLUDE_ANY'     => false,
173
        'PHP_TAGS'        => false,
174
        'MODIFIER_FUNCS'  => array(),
175
        'ALLOW_CONSTANTS' => false,
176
    );
177
178
    /**
179
     * Paths
180
     */
181
    public $template_dir = 'templates';
182
    public $compile_dir  = 'templates_c';
183
    public $config_dir   = 'configs';
184
    public $cache_dir    = 'cache';
185
    public $plugins_dir  = array();
186
187
    /**
188
     * Misc options
189
     */
190
    public $left_delimiter  = '{';
191
    public $right_delimiter = '}';
192
    public $compile_check   = true;
193
    public $force_compile   = false;
194
    public $caching         = 0;
195
    public $cache_lifetime  = 3600;
196
    public $compile_id      = null;
197
    public $compiler_file   = null;
198
    public $compiler_class  = null;
199
200
    /**
201
     * Dwoo/Smarty compat layer
202
     */
203
    public           $show_compat_errors = false;
204
    protected        $dataProvider;
205
    protected        $_filters           = array(
206
        'pre'    => array(),
207
        'post'   => array(),
208
        'output' => array()
209
    );
210
    protected static $tplCache           = array();
211
    protected        $compiler           = null;
212
213
    /**
214
     * Adapter constructor.
215
     */
216
    public function __construct()
217
    {
218
        parent::__construct();
219
        $this->charset      = 'iso-8859-1';
220
        $this->dataProvider = new Data();
221
        $this->compiler     = new Compiler();
222
    }
223
224
    /**
225
     * @param      $filename
226
     * @param null $cacheId
227
     * @param null $compileId
228
     */
229
    public function display($filename, $cacheId = null, $compileId = null)
230
    {
231
        $this->fetch($filename, $cacheId, $compileId, true);
232
    }
233
234
    /**
235
     * @param      $filename
236
     * @param null $cacheId
237
     * @param null $compileId
238
     * @param bool $display
239
     *
240
     * @return string|void
241
     */
242
    public function fetch($filename, $cacheId = null, $compileId = null, $display = false)
243
    {
244
        $this->setCacheDir($this->cache_dir);
245
        $this->setCompileDir($this->compile_dir);
246
247
        if ($this->security) {
248
            $policy = new SecurityPolicy();
249
            $policy->addPhpFunction(array_merge($this->security_settings['IF_FUNCS'], $this->security_settings['MODIFIER_FUNCS']));
0 ignored issues
show
Bug introduced by DSanchez
The method addPhpFunction() does not seem to exist on object<Dwoo\Security\Policy>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
250
251
            $phpTags = $this->security_settings['PHP_HANDLING'] ? SMARTY_PHP_ALLOW : $this->php_handling;
252
            if ($this->security_settings['PHP_TAGS']) {
253
                $phpTags = SMARTY_PHP_ALLOW;
254
            }
255
            switch ($phpTags) {
256
            case SMARTY_PHP_ALLOW:
257
            case SMARTY_PHP_PASSTHRU:
258
                $phpTags = SecurityPolicy::PHP_ALLOW;
259
                break;
260
            case SMARTY_PHP_QUOTE:
261
                $phpTags = SecurityPolicy::PHP_ENCODE;
262
                break;
263
            case SMARTY_PHP_REMOVE:
264
            default:
265
                $phpTags = SecurityPolicy::PHP_REMOVE;
266
                break;
267
            }
268
            $policy->setPhpHandling($phpTags);
269
270
            $policy->setConstantHandling($this->security_settings['ALLOW_CONSTANTS']);
271
272
            if ($this->security_settings['INCLUDE_ANY']) {
273
                $policy->allowDirectory(preg_replace('{^((?:[a-z]:)?[\\\\/]).*}i', '$1', __FILE__));
274
            } else {
275
                $policy->allowDirectory($this->secure_dir);
276
            }
277
278
            $this->setSecurityPolicy($policy);
279
        }
280
281
        if (!empty($this->plugins_dir)) {
282
            foreach ($this->plugins_dir as $dir) {
283
                $this->getLoader()->addDirectory(rtrim($dir, '\\/'));
284
            }
285
        }
286
287
        $tpl = $this->makeTemplate($filename, $cacheId, $compileId);
288
        if ($this->force_compile) {
289
            $tpl->forceCompilation();
290
        }
291
292
        if ($this->caching > 0) {
293
            $this->cacheTime = $this->cache_lifetime;
294
        } else {
295
            $this->cacheTime = 0;
296
        }
297
298
        if ($this->compiler_class !== null) {
299
            if ($this->compiler_file !== null && !class_exists($this->compiler_class)) {
300
                include $this->compiler_file;
301
            }
302
            $this->compiler = new $this->compiler_class();
303
        } else {
304
            $this->compiler->addPreProcessor('PluginSmartyCompatible', true);
305
            $this->compiler->setLooseOpeningHandling(true);
306
        }
307
308
        $this->compiler->setDelimiters($this->left_delimiter, $this->right_delimiter);
309
310
        return $this->get($tpl, $this->dataProvider, $this->compiler, $display === true);
0 ignored issues
show
Documentation introduced by DSanchez
$this->dataProvider is of type object<Dwoo\Data>, but the function expects a array.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
311
    }
312
313
    /**
314
     * @param mixed $_tpl
315
     * @param array $data
316
     * @param null  $_compiler
317
     * @param bool  $_output
318
     *
319
     * @return string|void
320
     */
321
    public function get($_tpl, $data = array(), $_compiler = null, $_output = false)
322
    {
323
        if ($_compiler === null) {
324
            $_compiler = $this->compiler;
325
        }
326
327
        return parent::get($_tpl, $data, $_compiler, $_output);
0 ignored issues
show
Bug introduced by DSanchez
It seems like $_compiler defined by $this->compiler on line 324 can also be of type object; however, Dwoo\Core::get() does only seem to accept object<Dwoo\ICompiler>|null, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
Unused Code introduced by DSanchez
The call to Core::get() has too many arguments starting with $_output.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
328
    }
329
330
    /**
331
     * @param      $name
332
     * @param      $callback
333
     * @param bool $cacheable
334
     * @param null $cache_attrs
335
     *
336
     * @throws Exception
337
     */
338 View Code Duplication
    public function register_function($name, $callback, $cacheable = true, $cache_attrs = null)
0 ignored issues
show
Unused Code introduced by Seldaek
The parameter $cacheable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Seldaek
The parameter $cache_attrs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
339
    {
340
        if (isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_FUNCTION) {
341
            throw new Exception('Multiple plugins of different types can not share the same name');
342
        }
343
        $this->plugins[$name] = array(
344
            'type'     => self::SMARTY_FUNCTION,
345
            'callback' => $callback
346
        );
347
    }
348
349
    /**
350
     * @param $name
351
     */
352
    public function unregister_function($name)
353
    {
354
        unset($this->plugins[$name]);
355
    }
356
357
    /**
358
     * @param      $name
359
     * @param      $callback
360
     * @param bool $cacheable
361
     * @param null $cache_attrs
362
     *
363
     * @throws Exception
364
     */
365 View Code Duplication
    public function register_block($name, $callback, $cacheable = true, $cache_attrs = null)
0 ignored issues
show
Unused Code introduced by Seldaek
The parameter $cacheable is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by Seldaek
The parameter $cache_attrs is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
366
    {
367
        if (isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_BLOCK) {
368
            throw new Exception('Multiple plugins of different types can not share the same name');
369
        }
370
        $this->plugins[$name] = array(
371
            'type'     => self::SMARTY_BLOCK,
372
            'callback' => $callback
373
        );
374
    }
375
376
    /**
377
     * @param $name
378
     */
379
    public function unregister_block($name)
380
    {
381
        unset($this->plugins[$name]);
382
    }
383
384
    /**
385
     * @param $name
386
     * @param $callback
387
     *
388
     * @throws Exception
389
     */
390 View Code Duplication
    public function register_modifier($name, $callback)
0 ignored issues
show
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
391
    {
392
        if (isset($this->plugins[$name]) && $this->plugins[$name][0] !== self::SMARTY_MODIFIER) {
393
            throw new Exception('Multiple plugins of different types can not share the same name');
394
        }
395
        $this->plugins[$name] = array(
396
            'type'     => self::SMARTY_MODIFIER,
397
            'callback' => $callback
398
        );
399
    }
400
401
    /**
402
     * @param $name
403
     */
404
    public function unregister_modifier($name)
405
    {
406
        unset($this->plugins[$name]);
407
    }
408
409
    /**
410
     * @param $callback
411
     */
412 View Code Duplication
    public function register_prefilter($callback)
0 ignored issues
show
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
413
    {
414
        $processor = new ProcessorAdapter($this->compiler);
415
        $processor->registerCallback($callback);
416
        $this->_filters['pre'][] = $processor;
417
        $this->compiler->addPreProcessor($processor);
418
    }
419
420
    /**
421
     * @param $callback
422
     */
423 View Code Duplication
    public function unregister_prefilter($callback)
0 ignored issues
show
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
424
    {
425
        foreach ($this->_filters['pre'] as $index => $processor) {
426
            if ($processor->callback === $callback) {
427
                $this->compiler->removePostProcessor($processor);
428
                unset($this->_filters['pre'][$index]);
429
            }
430
        }
431
    }
432
433
    /**
434
     * @param $callback
435
     */
436 View Code Duplication
    public function register_postfilter($callback)
0 ignored issues
show
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
437
    {
438
        $processor = new ProcessorAdapter($this->compiler);
439
        $processor->registerCallback($callback);
440
        $this->_filters['post'][] = $processor;
441
        $this->compiler->addPostProcessor($processor);
442
    }
443
444
    /**
445
     * @param $callback
446
     */
447 View Code Duplication
    public function unregister_postfilter($callback)
0 ignored issues
show
Duplication introduced by Seldaek
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
448
    {
449
        foreach ($this->_filters['post'] as $index => $processor) {
450
            if ($processor->callback === $callback) {
451
                $this->compiler->removePostProcessor($processor);
452
                unset($this->_filters['post'][$index]);
453
            }
454
        }
455
    }
456
457
    /**
458
     * @param $callback
459
     */
460
    public function register_outputfilter($callback)
461
    {
462
        $filter = new FilterAdapter($this);
463
        $filter->registerCallback($callback);
464
        $this->_filters['output'][] = $filter;
465
        $this->addFilter($filter);
466
    }
467
468
    /**
469
     * @param $callback
470
     */
471
    public function unregister_outputfilter($callback)
472
    {
473
        foreach ($this->_filters['output'] as $index => $filter) {
474
            if ($filter->callback === $callback) {
475
                $this->removeOutputFilter($filter);
0 ignored issues
show
Documentation Bug introduced by DSanchez
The method removeOutputFilter does not exist on object<Dwoo\Smarty\Adapter>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
476
                unset($this->_filters['output'][$index]);
477
            }
478
        }
479
    }
480
481
    /**
482
     * @param       $object
483
     * @param       $object_impl
484
     * @param array $allowed
485
     * @param bool  $smarty_args
486
     * @param array $block_methods
487
     */
488
    public function register_object($object, $object_impl, $allowed = array(), $smarty_args = false, $block_methods = array())
489
    {
490
        settype($allowed, 'array');
491
        settype($block_methods, 'array');
492
        settype($smarty_args, 'boolean');
493
494
        if (!empty($allowed) && $this->show_compat_errors) {
495
            $this->triggerError('You can register objects but can not restrict the method/properties used, this is PHP5, you have proper OOP access restrictions so use them.', E_USER_NOTICE);
496
        }
497
498
        if ($smarty_args) {
499
            $this->triggerError('You can register objects but methods will be called using method($arg1, $arg2, $arg3), not as an argument array like smarty did.', E_USER_NOTICE);
500
        }
501
502
        if (!empty($block_methods)) {
503
            $this->triggerError('You can register objects but can not use methods as being block methods, you have to build a plugin for that.', E_USER_NOTICE);
504
        }
505
506
        $this->dataProvider->assign($object, $object_impl);
507
    }
508
509
    /**
510
     * @param $object
511
     */
512
    public function unregister_object($object)
513
    {
514
        $this->dataProvider->clear($object);
515
    }
516
517
    /**
518
     * @param $name
519
     *
520
     * @return mixed
521
     */
522
    public function get_registered_object($name)
523
    {
524
        $data = $this->dataProvider->getData();
525
        if (isset($data[$name]) && is_object($data[$name])) {
526
            return $data[$name];
527
        } else {
528
            trigger_error('Dwoo_Compiler: object "' . $name . '" was not registered or is not an object', E_USER_ERROR);
529
        }
530
    }
531
532
    /**
533
     * @param $filename
534
     *
535
     * @return bool
536
     */
537
    public function template_exists($filename)
538
    {
539
        if (!is_array($this->template_dir)) {
540
            return file_exists($this->template_dir . DIRECTORY_SEPARATOR . $filename);
541
        } else {
542
            foreach ($this->template_dir as $tpl_dir) {
543
                if (file_exists($tpl_dir . DIRECTORY_SEPARATOR . $filename)) {
544
                    return true;
545
                }
546
            }
547
548
            return false;
549
        }
550
    }
551
552
    /**
553
     * @param      $tpl
554
     * @param null $cacheId
555
     * @param null $compileId
556
     *
557
     * @return bool
558
     */
559
    public function is_cached($tpl, $cacheId = null, $compileId = null)
560
    {
561
        return $this->isCached($this->makeTemplate($tpl, $cacheId, $compileId));
562
    }
563
564
    /**
565
     * @param      $var
566
     * @param      $value
567
     * @param bool $merge
568
     */
569
    public function append_by_ref($var, &$value, $merge = false)
570
    {
571
        $this->dataProvider->appendByRef($var, $value, $merge);
572
    }
573
574
    /**
575
     * @param $name
576
     * @param $val
577
     */
578
    public function assign_by_ref($name, &$val)
579
    {
580
        $this->dataProvider->assignByRef($name, $val);
581
    }
582
583
    /**
584
     * @param $var
585
     */
586
    public function clear_assign($var)
587
    {
588
        $this->dataProvider->clear($var);
589
    }
590
591
    /**
592
     *
593
     */
594
    public function clear_all_assign()
595
    {
596
        $this->dataProvider->clear();
597
    }
598
599
    /**
600
     * @param null $name
601
     *
602
     * @return array|null
603
     */
604
    public function get_template_vars($name = null)
605
    {
606
        if ($this->show_compat_errors) {
607
            trigger_error('get_template_vars does not return values by reference, if you try to modify the data that way you should modify your code.', E_USER_NOTICE);
608
        }
609
610
        $data = $this->dataProvider->getData();
611
        if ($name === null) {
612
            return $data;
613
        } elseif (isset($data[$name])) {
614
            return $data[$name];
615
        }
616
617
        return null;
618
    }
619
620
    /**
621
     * @param int $olderThan
622
     */
623
    public function clear_all_cache($olderThan = 0)
624
    {
625
        $this->clearCache($olderThan);
626
    }
627
628
    /**
629
     * @param      $template
630
     * @param null $cacheId
631
     * @param null $compileId
632
     * @param int  $olderThan
633
     */
634
    public function clear_cache($template, $cacheId = null, $compileId = null, $olderThan = 0)
635
    {
636
        $this->makeTemplate($template, $cacheId, $compileId)->clearCache($olderThan);
637
    }
638
639
    /**
640
     * @param     $error_msg
641
     * @param int $error_type
642
     */
643
    public function trigger_error($error_msg, $error_type = E_USER_WARNING)
644
    {
645
        $this->triggerError($error_msg, $error_type);
646
    }
647
648
    /**
649
     *
650
     */
651
    protected function initGlobals()
652
    {
653
        parent::initGlobals();
654
        $this->globals['ldelim'] = '{';
655
        $this->globals['rdelim'] = '}';
656
    }
657
658
    /**
659
     * @param $file
660
     * @param $cacheId
661
     * @param $compileId
662
     *
663
     * @return mixed
664
     * @throws Exception
665
     */
666
    protected function makeTemplate($file, $cacheId, $compileId)
667
    {
668
        if ($compileId === null) {
669
            $compileId = $this->compile_id;
670
        }
671
672
        $hash = bin2hex(md5($file . $cacheId . $compileId, true));
673
        if (!isset(self::$tplCache[$hash])) {
674
            // abs path
675
            if (substr($file, 0, 1) === '/' || substr($file, 1, 1) === ':') {
676
                self::$tplCache[$hash] = new TemplateFile($file, null, $cacheId, $compileId);
677
            } elseif (is_string($this->template_dir) || is_array($this->template_dir)) {
678
                self::$tplCache[$hash] = new TemplateFile($file, null, $cacheId, $compileId, $this->template_dir);
679
            } else {
680
                throw new Exception('Unable to load "' . $file . '", check the template_dir');
681
            }
682
        }
683
684
        return self::$tplCache[$hash];
685
    }
686
687
    /**
688
     * @param string $message
689
     * @param int    $level
690
     */
691
    public function triggerError($message, $level = E_USER_NOTICE)
692
    {
693
        if (is_object($this->template)) {
694
            parent::triggerError($message, $level);
695
        }
696
        trigger_error('Dwoo error : ' . $message, $level);
697
    }
698
}
699