TraceableSmarty.php 3.15 KB
Newer Older
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
1
2
3
4
<?php

namespace Praguebest\Tools;

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
5
use Praguebest\BarPanel\Registrator;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
6
use Smarty;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
7

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
8
class TraceableSmarty extends Smarty
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
9
{
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
10
11
    public const SMARTY_VARIABLES = 'SMARTY_VARIABLES';
    public const INTERNAL_VARIABLES = 'INTERNAL_VARIABLES';
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
12
    /** @var array */
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    private $backtraces = [];
    /** @var TraceableInternalTemplate[] */
    private $generatedTemplates = [];

    private $collectVariables = false;
    private $collectBacktraces = false;

    /**
     * TraceableSmarty constructor.
     */
    public function __construct()
    {
        parent::__construct();

        if (OnOffSwitcher::shouldProfile(TraceableSmarty::INTERNAL_VARIABLES)) {
            $this->template_class = TraceableInternalTemplate::class;
            $this->collectVariables = true;
        }

        $this->collectBacktraces = OnOffSwitcher::shouldProfile(TraceableSmarty::SMARTY_VARIABLES);
    }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

    /**
     * @param array|string $tpl_var
     * @param null $value
     * @param bool $nocache
     *
     * @return Smarty_Internal_Data
     */
    public function assign($tpl_var, $value = null, $nocache = false)
    {
        if (is_array($tpl_var)) {
            foreach ($tpl_var as $var) {
                $this->assign($var, $value, $nocache);
            }
        } else {
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
49
50
51
            if ($this->collectBacktraces) {
                $this->backtraces[$tpl_var] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
            }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
52
53
54
55
56
57
58

            parent::assign($tpl_var, $value, $nocache);
        }

        return $this;
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
59
60
61
62
63
64
65
66
    public function assignByRef($tpl_var, &$value, $nocache = false)
    {
        if ($this->collectBacktraces) {
            $this->backtraces[$tpl_var] = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
        }
        parent::assignByRef($tpl_var, $value, $nocache);
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
67
68
69
70
    public function display($template = null, $cache_id = null, $compile_id = null, $parent = null)
    {
        parent::display($template, $cache_id, $compile_id, $parent);

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
        $registrator = Registrator::getInstance($this->getCompileDir(), $this->getCacheDir());
        $registrator->registerSmarty($this);
        $registrator->registerInternalSmarty($this);
    }

    /**
     * @param string $template
     * @param mixed|null $cache_id
     * @param mixed|null $compile_id
     * @param object|null $parent
     * @param bool $do_clone
     * @return Smarty_Internal_Template
     */
    public function createTemplate($template, $cache_id = null, $compile_id = null, $parent = null, $do_clone = true)
    {
        $template = parent::createTemplate($template, $cache_id, $compile_id, $parent, $do_clone);
        if ($this->collectVariables) {
            $this->generatedTemplates[] = $template;
        }

        return $template;
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
92
93
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
94
95
96
97
98
    public function getBacktraces(): array
    {
        return $this->backtraces;
    }

Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    public function getAssigns(): array
    {
        $assigns = [];
        foreach ($this->generatedTemplates as $template) {
            $assigns[] = $template->getAssigns();
        }

        $assigns = array_filter($assigns);

        if (count($assigns) > 0) {
            $assigns = array_merge_recursive(...$assigns);
        }

        return $assigns;
    }
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
114
}