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) { |
|
|
|
|
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) { |
|
|
|
|
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); |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|