Passed
Push — master ( 5ce973...cc4e99 )
by Thomas
02:37
created

SSViewerProxy::resetTemplatesUsed()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace LeKoala\DebugBar\Proxy;
4
5
use LeKoala\DebugBar\DebugBar;
6
use SilverStripe\View\SSViewer;
7
8
/**
9
 * The template parser proxy will monitor the templates that are used during a page request. Since the
10
 * use of the template parser is behind cache checks, this will only execute during a cache flush.
11
 */
12
class SSViewerProxy extends SSViewer
13
{
14
    /**
15
     * Tracks all templates used in the current request
16
     *
17
     * @var array
18
     */
19
    protected static $allTemplates = array();
20
21
    /**
22
     * Whether the class has been used, meaning whether the page has been cached
23
     *
24
     * @var boolean
25
     */
26
    protected static $cached = true;
27
28
    /**
29
     * Overloaded to track all templates used in the current request
30
     *
31
     * {@inheritDoc}
32
     */
33
    public function process($item, $arguments = null, $inheritedScope = null)
34
    {
35
        $templateName = self::normalizeTemplateName($this->chosen);
36
        self::trackTemplateUsed($templateName);
37
38
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($templateName) {
39
            /** @var $timeData DebugBar\DataCollector\TimeDataCollector */
40
            $timeData = $debugBar->getCollector('time');
41
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
42
                return;
43
            }
44
            $timeData->startMeasure($templateName, $templateName);
45
        });
46
47
        $result = parent::process($item, $arguments, $inheritedScope);
48
49
        DebugBar::withDebugBar(function (\DebugBar\DebugBar $debugBar) use ($templateName) {
50
            /** @var $timeData DebugBar\DataCollector\TimeDataCollector */
51
            $timeData = $debugBar->getCollector('time');
52
            if (!$timeData) {
0 ignored issues
show
introduced by
$timeData is of type LeKoala\DebugBar\DebugBa...ector\TimeDataCollector, thus it always evaluated to true.
Loading history...
53
                return;
54
            }
55
            if ($timeData->hasStartedMeasure($templateName)) {
56
                $timeData->stopMeasure($templateName);
57
            }
58
        });
59
60
        return $result;
61
    }
62
63
    /**
64
     * Get the templates used in the current request and the number of times they were called
65
     *
66
     * @return array
67
     */
68
    public static function getTemplatesUsed()
69
    {
70
        return static::$allTemplates;
71
    }
72
73
    /**
74
     * Reset the array
75
     *
76
     * @return void
77
     */
78
    public static function resetTemplatesUsed()
79
    {
80
        static::$allTemplates = [];
81
    }
82
83
    /**
84
     * Helps tracking the use of templates
85
     *
86
     * @param string $templateName
87
     */
88
    protected static function trackTemplateUsed($templateName)
89
    {
90
        static::$allTemplates[] = $templateName;
91
    }
92
93
    /**
94
     * Remove base path from template
95
     *
96
     * @param string $templateName
97
     * @return string
98
     */
99
    protected static function normalizeTemplateName($templateName)
100
    {
101
        return  str_ireplace(BASE_PATH, '', $templateName);
0 ignored issues
show
Bug Best Practice introduced by
The expression return str_ireplace(LeKo...ATH, '', $templateName) also could return the type array which is incompatible with the documented return type string.
Loading history...
102
    }
103
}
104