TraceableInternalTemplate.php 2.07 KB
Newer Older
Tomáš Pospíšil's avatar
Tomáš Pospíšil committed
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
<?php

namespace Praguebest\Tools;

class TraceableInternalTemplate extends \Smarty_Internal_Template
{
    private $assignedByScope = [];

    /**
     * @param string $varName
     * @param mixed $value
     * @param bool $nocache
     * @param int $scope
     */
    public function _assignInScope($varName, $value, $nocache = false, $scope = 0)
    {
        parent::_assignInScope($varName, $value, $nocache, $scope);

        $this->addAssignedByScope($varName, $value);
    }

    public function addAssignedByScope($varName, $value, array $backtrace = [])
    {
        $backtrace[] = reset($this->compiled->file_dependency)[0];
        if ($this->parent instanceof TraceableInternalTemplate) {
            $this->parent->addAssignedByScope($varName, $value, $backtrace);
        } else {
            $this->assignedByScope[$varName] = $this->assignedByScope[$varName] ?? [];
            $this->assignedByScope[$varName][] = [
                'value' => $value,
                'backtrace' => $backtrace,
            ];
        }
    }

    /**
     * @param string $template
     * @param mixed $cache_id
     * @param mixed $compile_id
     * @param int $caching
     * @param int $cache_lifetime
     * @param array $data
     * @param int $scope
     * @param bool $forceTplCache
     * @param string|null $uid
     * @param string|null $content_func
     */
    public function _subTemplateRender(
        $template,
        $cache_id,
        $compile_id,
        $caching,
        $cache_lifetime,
        $data,
        $scope,
        $forceTplCache,
        $uid = null,
        $content_func = null
    ) {
        parent::_subTemplateRender(
            $template,
            $cache_id,
            $compile_id,
            $caching,
            $cache_lifetime,
            $data,
            $scope,
            $forceTplCache,
            $uid,
            $content_func
        );

        foreach ($data as $key => $value) {
            $this->addAssignedByScope($key, $value);
        }
    }

    public function getAssigns(): array
    {
        return $this->assignedByScope;
    }

}