1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author @jayS-de <[email protected]> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
namespace JaySDe\HandlebarsBundle\DataCollector; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
use Symfony\Component\HttpFoundation\Request; |
11
|
|
|
use Symfony\Component\HttpFoundation\Response; |
12
|
|
|
use Symfony\Component\HttpKernel\DataCollector\DataCollector; |
13
|
|
|
use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface; |
14
|
|
|
|
15
|
|
|
class HandlebarsDataCollector extends DataCollector implements LateDataCollectorInterface |
16
|
|
|
{ |
17
|
|
|
private $profile; |
18
|
|
|
private $computed; |
19
|
|
|
|
20
|
10 |
|
public function __construct(\Twig_Profiler_Profile $profile) |
21
|
|
|
{ |
22
|
10 |
|
$this->profile = $profile; |
23
|
10 |
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* {@inheritdoc} |
27
|
|
|
*/ |
28
|
|
|
public function collect(Request $request, Response $response, \Exception $exception = null) |
29
|
|
|
{ |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function lateCollect() |
36
|
|
|
{ |
37
|
|
|
$this->data['profile'] = serialize($this->profile); |
38
|
|
|
} |
39
|
|
|
|
40
|
1 |
|
public function getTime() |
41
|
|
|
{ |
42
|
1 |
|
return $this->getProfile()->getDuration() * 1000; |
43
|
|
|
} |
44
|
|
|
|
45
|
2 |
|
public function getTemplateCount() |
46
|
|
|
{ |
47
|
2 |
|
return $this->getComputedData('template_count'); |
48
|
|
|
} |
49
|
|
|
|
50
|
1 |
|
public function getTemplates() |
51
|
|
|
{ |
52
|
1 |
|
return $this->getComputedData('templates'); |
53
|
|
|
} |
54
|
|
|
|
55
|
2 |
|
public function getBlockCount() |
56
|
|
|
{ |
57
|
2 |
|
return $this->getComputedData('block_count'); |
58
|
|
|
} |
59
|
|
|
|
60
|
2 |
|
public function getMacroCount() |
61
|
|
|
{ |
62
|
2 |
|
return $this->getComputedData('macro_count'); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function getHtmlCallGraph() |
66
|
|
|
{ |
67
|
1 |
|
$dumper = new \Twig_Profiler_Dumper_Html(); |
68
|
1 |
|
$dump = $dumper->dump($this->getProfile()); |
69
|
|
|
|
70
|
|
|
// needed to remove the hardcoded CSS styles |
71
|
1 |
|
$dump = str_replace(array( |
72
|
1 |
|
'<span style="background-color: #ffd">', |
73
|
|
|
'<span style="color: #d44">', |
74
|
|
|
'<span style="background-color: #dfd">', |
75
|
|
|
), array( |
76
|
1 |
|
'<span class="status-warning">', |
77
|
|
|
'<span class="status-error">', |
78
|
|
|
'<span class="status-success">', |
79
|
|
|
), $dump); |
80
|
|
|
|
81
|
1 |
|
return new \Twig_Markup($dump, 'UTF-8'); |
82
|
|
|
} |
83
|
|
|
|
84
|
9 |
|
public function getProfile() |
85
|
|
|
{ |
86
|
9 |
|
if (null === $this->profile) { |
87
|
|
|
$this->profile = unserialize($this->data['profile']); |
88
|
|
|
} |
89
|
|
|
|
90
|
9 |
|
return $this->profile; |
91
|
|
|
} |
92
|
|
|
|
93
|
7 |
|
private function getComputedData($index) |
94
|
|
|
{ |
95
|
7 |
|
if (null === $this->computed) { |
96
|
7 |
|
$this->computed = $this->computeData($this->getProfile()); |
97
|
|
|
} |
98
|
|
|
|
99
|
7 |
|
return $this->computed[$index]; |
100
|
|
|
} |
101
|
|
|
|
102
|
7 |
|
private function computeData(\Twig_Profiler_Profile $profile) |
103
|
|
|
{ |
104
|
|
|
$data = array( |
105
|
7 |
|
'template_count' => 0, |
106
|
|
|
'block_count' => 0, |
107
|
|
|
'macro_count' => 0, |
108
|
|
|
); |
109
|
|
|
|
110
|
7 |
|
$templates = array(); |
111
|
|
|
/** |
112
|
|
|
* @var \Twig_Profiler_Profile $p |
113
|
|
|
*/ |
114
|
7 |
|
foreach ($profile as $p) { |
115
|
7 |
|
$d = $this->computeData($p); |
116
|
|
|
|
117
|
7 |
|
$data['template_count'] += $this->sumProfileCount($p->isTemplate(), $d['template_count']); |
118
|
7 |
|
$data['block_count'] += $this->sumProfileCount($p->isBlock(), $d['block_count']); |
119
|
7 |
|
$data['macro_count'] += $this->sumProfileCount($p->isMacro(), $d['macro_count']); |
120
|
|
|
|
121
|
7 |
|
$templates = $this->incTemplateCount($p, $templates); |
122
|
7 |
|
$templates = $this->sumTemplates($d['templates'], $templates); |
123
|
|
|
} |
124
|
7 |
|
$data['templates'] = $templates; |
125
|
|
|
|
126
|
7 |
|
return $data; |
127
|
|
|
} |
128
|
|
|
|
129
|
7 |
|
protected function incTemplateCount(\Twig_Profiler_Profile $p, $templates) |
130
|
|
|
{ |
131
|
7 |
|
if ($p->isTemplate()) { |
132
|
3 |
|
if (!isset($templates[$p->getTemplate()])) { |
133
|
3 |
|
$templates[$p->getTemplate()] = 1; |
134
|
|
|
} else { |
135
|
2 |
|
++$templates[$p->getTemplate()]; |
136
|
|
|
} |
137
|
|
|
} |
138
|
7 |
|
return $templates; |
139
|
|
|
} |
140
|
|
|
|
141
|
7 |
|
protected function sumProfileCount($pIs, $count) |
142
|
|
|
{ |
143
|
7 |
|
return ($pIs ? 1 : 0) + $count; |
144
|
|
|
} |
145
|
|
|
|
146
|
7 |
|
protected function sumTemplates(array $d, array $templates) |
147
|
|
|
{ |
148
|
7 |
|
foreach ($d as $template => $count) { |
149
|
2 |
|
if (!isset($templates[$template])) { |
150
|
2 |
|
$templates[$template] = $count; |
151
|
|
|
} else { |
152
|
2 |
|
$templates[$template] += $count; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
7 |
|
return $templates; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* {@inheritdoc} |
161
|
|
|
*/ |
162
|
1 |
|
public function getName() |
163
|
|
|
{ |
164
|
1 |
|
return 'handlebars'; |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|