-
-
Notifications
You must be signed in to change notification settings - Fork 113
Expand file tree
/
Copy pathConnectionPanel.php
More file actions
165 lines (138 loc) · 3.96 KB
/
ConnectionPanel.php
File metadata and controls
165 lines (138 loc) · 3.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/**
* This file is part of the Nette Framework (https://nette.org)
* Copyright (c) 2004 David Grudl (https://davidgrudl.com)
*/
declare(strict_types=1);
namespace Nette\Bridges\DatabaseTracy;
use Nette;
use Nette\Database\DriverException;
use Nette\Database\Explorer;
use Nette\Database\Helpers;
use Nette\Database\Result;
use Tracy;
/**
* Debug panel for Nette\Database.
*/
class ConnectionPanel implements Tracy\IBarPanel
{
public int $maxQueries = 100;
public string $name;
public bool|string $explain = true;
public bool $disabled = false;
public float $performanceScale = 0.25;
private float $totalTime = 0;
private int $count = 0;
private array $events = [];
private Tracy\BlueScreen $blueScreen;
public static function initialize(
Explorer $explorer,
bool $addBarPanel = true,
string $name = '',
bool $explain = true,
?Tracy\Bar $bar = null,
?Tracy\BlueScreen $blueScreen = null,
): ?self
{
$blueScreen ??= Tracy\Debugger::getBlueScreen();
$blueScreen->addPanel(self::renderException(...));
if ($addBarPanel) {
$panel = new self($explorer, $blueScreen);
$panel->explain = $explain;
$panel->name = $name;
$bar ??= Tracy\Debugger::getBar();
$bar->addPanel($panel);
}
return $panel ?? null;
}
public function __construct(Explorer $explorer, Tracy\BlueScreen $blueScreen)
{
$explorer->onQuery[] = $this->logQuery(...);
$this->blueScreen = $blueScreen;
}
private function logQuery(Explorer $connection, $result): void
{
if ($this->disabled) {
return;
}
$this->count++;
$source = null;
$trace = $result instanceof DriverException
? $result->getTrace()
: debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS);
foreach ($trace as $row) {
if (
(isset($row['file'])
&& preg_match('~\.(php.?|phtml)$~', $row['file'])
&& !$this->blueScreen->isCollapsed($row['file']))
&& ($row['class'] ?? '') !== self::class
&& !is_a($row['class'] ?? '', Explorer::class, allow_string: true)
) {
$source = [$row['file'], (int) $row['line']];
break;
}
}
if ($result instanceof Result) {
$this->totalTime += $result->getTime();
if ($this->count < $this->maxQueries) {
$this->events[] = [$connection, $result->getQuery(), $source, $result->getTime(), $result->getRowCount(), null];
}
} elseif ($result instanceof DriverException && $this->count < $this->maxQueries) {
$this->events[] = [$connection, $result->getQuery(), $source, null, null, $result->getMessage()];
}
}
public static function renderException(?\Throwable $e): ?array
{
if (!$e instanceof DriverException) {
return null;
}
return $e->getQuery() ? [
'tab' => 'SQL',
'panel' => Helpers::dumpSql($e->getQuery()),
] : null;
}
public function getTab(): string
{
return Nette\Utils\Helpers::capture(function () {
$name = $this->name;
$count = $this->count;
$totalTime = $this->totalTime;
require __DIR__ . '/templates/ConnectionPanel.tab.phtml';
});
}
public function getPanel(): ?string
{
if (!$this->count) {
return null;
}
$events = [];
foreach ($this->events as $event) {
[$connection, $query, , , , $error] = $event;
$explain = null;
$sql = $query->getSql();
$command = preg_match('#\s*\(?\s*(SELECT|INSERT|UPDATE|DELETE)\s#iA', $sql, $m)
? strtolower($m[1])
: null;
if (!$error && $this->explain && $command === 'select') {
try {
$cmd = is_string($this->explain)
? $this->explain
: 'EXPLAIN';
$rows = $connection->getConnection()->query("$cmd $sql", $query->getParameters());
for ($explain = []; $row = $rows->fetch(); $explain[] = $row);
} catch (DriverException) {
}
}
$event[] = $command;
$event[] = $explain;
$events[] = $event;
}
return Nette\Utils\Helpers::capture(function () use ($events, $connection) {
$name = $this->name;
$count = $this->count;
$totalTime = $this->totalTime;
$performanceScale = $this->performanceScale;
require __DIR__ . '/templates/ConnectionPanel.panel.phtml';
});
}
}