1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Rostenkowski\Doctrine\Debugger; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use Doctrine\DBAL\Logging\SQLLogger; |
7
|
|
|
use Nette\Utils\Html; |
8
|
|
|
use const PHP_EOL; |
|
|
|
|
9
|
|
|
use Tracy\Dumper; |
10
|
|
|
use Tracy\Helpers; |
11
|
|
|
use Tracy\IBarPanel; |
12
|
|
|
use const DEBUG_BACKTRACE_IGNORE_ARGS; |
|
|
|
|
13
|
|
|
use function count; |
14
|
|
|
use function debug_backtrace; |
|
|
|
|
15
|
|
|
use function dirname; |
|
|
|
|
16
|
|
|
use function strlen; |
|
|
|
|
17
|
|
|
use function substr; |
|
|
|
|
18
|
|
|
use function var_dump; |
|
|
|
|
19
|
|
|
|
20
|
|
|
class TracyBar implements SQLLogger, IBarPanel |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $appDir; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var float |
30
|
|
|
*/ |
31
|
|
|
private $start; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var int |
35
|
|
|
*/ |
36
|
|
|
private $current; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
private $queries = []; |
42
|
|
|
|
43
|
|
|
private $totalTime; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* debugger panel max width |
47
|
|
|
* |
48
|
|
|
* @var string |
49
|
|
|
*/ |
50
|
|
|
private $width = '960px'; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* debugger panel max height |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
private $height = '720px'; |
58
|
|
|
|
59
|
|
|
|
60
|
|
|
public function __construct(string $appDir) |
61
|
|
|
{ |
62
|
|
|
$this->appDir = $appDir; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
|
66
|
|
|
public function getHeight(): string |
67
|
|
|
{ |
68
|
|
|
return $this->height; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
|
72
|
|
|
public function setHeight(string $height) |
73
|
|
|
{ |
74
|
|
|
$this->height = $height; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
public function getPanel() |
79
|
|
|
{ |
80
|
|
|
$totalQueries = count($this->queries); |
81
|
|
|
$color = $totalQueries ? 'green' : '#555555'; |
82
|
|
|
$timeCaption = $this->formatTime($this->getTotalTime()); |
83
|
|
|
$panel = $this->getTemplate('panel'); |
84
|
|
|
$row = $this->getTemplate('query'); |
85
|
|
|
$buffer = ''; |
86
|
|
|
$colorizer = new SimpleQueryColorizer(); |
87
|
|
|
foreach ($this->queries as $query) { |
88
|
|
|
|
89
|
|
|
$link = Helpers::editorUri($query['file'], $query['line']); |
90
|
|
|
$linkText = '…/' . substr($query['file'], strlen(dirname($this->appDir)) + 1) . ':' . $query['line']; |
91
|
|
|
$a = Html::el('a')->setAttribute('href', $link)->setText($linkText); |
92
|
|
|
$buffer .= sprintf($row, |
93
|
|
|
$this->formatTime($query['dur']), |
94
|
|
|
$colorizer->colorize($query['sql'], true), |
95
|
|
|
$this->dump($query['params']), |
96
|
|
|
(string) $a |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return sprintf($panel, $color, $timeCaption, $totalQueries, $this->width, $this->height, $buffer); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
|
104
|
|
|
public function getTab() |
105
|
|
|
{ |
106
|
|
|
$count = count($this->queries); |
107
|
|
|
$color = $count ? 'green' : '#555555'; |
108
|
|
|
$time = $this->formatTime($this->getTotalTime()); |
109
|
|
|
$template = $this->getTemplate('tab'); |
110
|
|
|
|
111
|
|
|
return sprintf($template, $color, $time, $count); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
private function formatTime($microseconds) |
116
|
|
|
{ |
117
|
|
|
return number_format($microseconds * 1000, 1, '.', ' ') . ' ms'; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
|
121
|
|
|
private function getTotalTime() |
122
|
|
|
{ |
123
|
|
|
if ($this->totalTime === NULL) { |
124
|
|
|
foreach ($this->queries as $query) { |
125
|
|
|
$this->totalTime += $query['dur']; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->totalTime; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
|
133
|
|
|
private function getTemplate($name) |
134
|
|
|
{ |
135
|
|
|
return file_get_contents(__DIR__ . "/templates/$name.html"); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
|
139
|
|
|
private function dump($params) |
140
|
|
|
{ |
141
|
|
|
if ($params) { |
142
|
|
|
|
143
|
|
|
return Dumper::toHtml($params, [Dumper::COLLAPSE => 1]); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
return ''; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
|
150
|
|
|
public function getWidth(): string |
151
|
|
|
{ |
152
|
|
|
return $this->width; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
|
156
|
|
|
public function setWidth(string $width) |
157
|
|
|
{ |
158
|
|
|
$this->width = $width; |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
|
162
|
|
|
public function startQuery($sql, array $params = NULL, array $types = NULL) |
163
|
|
|
{ |
164
|
|
|
$this->start = (float) microtime(true); |
165
|
|
|
|
166
|
|
|
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS); |
167
|
|
|
|
168
|
|
|
foreach ($trace as $i => $item) { |
169
|
|
|
if (substr($trace[$i]['file'], 0, strlen($this->appDir)) === $this->appDir) { |
170
|
|
|
break; |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
$this->queries[++$this->current] = [ |
175
|
|
|
'sql' => trim($sql, " \t\n\r\0\x0B\""), |
176
|
|
|
'params' => $params, |
177
|
|
|
'types' => $types, |
178
|
|
|
'dur' => 0, |
179
|
|
|
'file' => $trace[$i]['file'], |
|
|
|
|
180
|
|
|
'line' => $trace[$i]['line'], |
181
|
|
|
]; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
|
185
|
|
|
public function stopQuery() |
186
|
|
|
{ |
187
|
|
|
$this->queries[$this->current]['dur'] = (float) microtime(true) - $this->start; |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
|
191
|
|
|
} |
192
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths