1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Rostenkowski\Doctrine\Debugger; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
7
|
|
|
use Tracy\Dumper; |
8
|
|
|
use Tracy\IBarPanel; |
9
|
|
|
|
10
|
|
|
class TracyBar implements SQLLogger, IBarPanel |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var float |
15
|
|
|
*/ |
16
|
|
|
private $start; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var int |
20
|
|
|
*/ |
21
|
|
|
private $current; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $queries = []; |
27
|
|
|
|
28
|
|
|
private $totalTime; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* debugger panel max width |
32
|
|
|
* |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
private $width = '960px'; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* debugger panel max height |
39
|
|
|
* |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $height = '720px'; |
43
|
|
|
|
44
|
|
|
|
45
|
|
|
public function getHeight(): string |
46
|
|
|
{ |
47
|
|
|
return $this->height; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
|
51
|
|
|
public function setHeight(string $height) |
52
|
|
|
{ |
53
|
|
|
$this->height = $height; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
|
57
|
|
|
public function getPanel() |
58
|
|
|
{ |
59
|
|
|
$totalQueries = count($this->queries); |
60
|
|
|
$color = $totalQueries ? 'green' : '#555555'; |
61
|
|
|
$timeCaption = $this->formatTime($this->getTotalTime()); |
62
|
|
|
$panel = $this->getTemplate('panel'); |
63
|
|
|
$row = $this->getTemplate('query'); |
64
|
|
|
$buffer = ''; |
65
|
|
|
$colorizer = new SimpleQueryColorizer(); |
66
|
|
|
foreach ($this->queries as $query) { |
67
|
|
|
$buffer .= sprintf($row, |
68
|
|
|
$this->formatTime($query['dur']), |
69
|
|
|
$colorizer->colorize($query['sql']), |
70
|
|
|
$this->dump($query['params']) |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return sprintf($panel, $color, $timeCaption, $totalQueries, $this->width, $this->height, $buffer); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
public function getTab() |
79
|
|
|
{ |
80
|
|
|
$count = count($this->queries); |
81
|
|
|
$color = $count ? 'green' : '#555555'; |
82
|
|
|
$time = $this->formatTime($this->getTotalTime()); |
83
|
|
|
$template = $this->getTemplate('tab'); |
84
|
|
|
|
85
|
|
|
return sprintf($template, $color, $time, $count); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
|
89
|
|
|
private function formatTime($microseconds) |
90
|
|
|
{ |
91
|
|
|
return number_format($microseconds * 1000, 1, '.', ' ') . ' ms'; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
private function getTotalTime() |
96
|
|
|
{ |
97
|
|
|
if ($this->totalTime === NULL) { |
98
|
|
|
foreach ($this->queries as $query) { |
99
|
|
|
$this->totalTime += $query['dur']; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this->totalTime; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
|
107
|
|
|
private function getTemplate($name) |
108
|
|
|
{ |
109
|
|
|
return file_get_contents(__DIR__ . "/templates/$name.html"); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
|
113
|
|
|
private function dump($params) |
114
|
|
|
{ |
115
|
|
|
if ($params) { |
116
|
|
|
|
117
|
|
|
return Dumper::toHtml($params, [Dumper::COLLAPSE => 1]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return ''; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
|
124
|
|
|
public function getWidth(): string |
125
|
|
|
{ |
126
|
|
|
return $this->width; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
|
130
|
|
|
public function setWidth(string $width) |
131
|
|
|
{ |
132
|
|
|
$this->width = $width; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
|
136
|
|
|
public function startQuery($sql, array $params = NULL, array $types = NULL) |
137
|
|
|
{ |
138
|
|
|
$this->start = (float) microtime(true); |
139
|
|
|
$this->queries[++$this->current] = ['sql' => trim($sql, " \t\n\r\0\x0B\""), 'params' => $params, 'types' => $types, 'dur' => 0]; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
|
143
|
|
|
public function stopQuery() |
144
|
|
|
{ |
145
|
|
|
$this->queries[$this->current]['dur'] = (float) microtime(true) - $this->start; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
} |
149
|
|
|
|