-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDoctrineCollector.php
More file actions
182 lines (166 loc) · 5.18 KB
/
DoctrineCollector.php
File metadata and controls
182 lines (166 loc) · 5.18 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar\Bridge\Doctrine;
use DebugBar\DataCollector\AssetProvider;
use DebugBar\DataCollector\DataCollector;
use DebugBar\DataCollector\Renderable;
use DebugBar\DebugBarException;
/**
* Collects Doctrine queries
*
* http://doctrine-project.org
*
* Uses a middleware to collects data about queries
*
* <code>
* $debugbar->addCollector(new DoctrineCollector($debugBarSQLMiddleware));
* $config->setMiddlewares([$debugBarSQLMiddleware]);
* $conn = Doctrine\DBAL\DriverManager::getConnection($dbParameters, $config);
* $entityManager = new Doctrine\ORM\EntityManager($conn, $config);
* </code>
*/
class DoctrineCollector extends DataCollector implements Renderable, AssetProvider
{
protected $debugStack;
protected $durationBackground = false;
protected $slowThreshold;
/**
* DoctrineCollector constructor.
*
* @throws DebugBarException
*/
public function __construct(?DebugBarSQLMiddleware $debugStack)
{
$this->debugStack = $debugStack;
}
public function setDebugStack(DebugBarSQLMiddleware $debugStack): void
{
$this->debugStack = $debugStack;
}
/**
* Enable/disable the shaded duration background on queries
*
* @param bool $enabled
*/
public function setDurationBackground(bool $enabled): void
{
$this->durationBackground = $enabled;
}
/**
* Highlights queries that exceed the threshold
*
* @param int|float $threshold miliseconds value
*/
public function setSlowThreshold(int|float $threshold): void
{
$this->slowThreshold = $threshold / 1000;
}
/**
* @return array
*/
public function collect(): array
{
$queries = array();
$nb_statements = 0;
$totalExecTime = 0;
foreach ($this->debugStack?->queries ?? [] as $q) {
$queries[] = array(
'sql' => $q['sql'],
'params' => (object) $this->getParameters($q['params'] ?? []),
'duration' => $q['executionMS'],
'duration_str' => $this->getDataFormatter()->formatDuration($q['executionMS']),
'type' => $q['type'] ?? null,
'slow' => $this->slowThreshold && $this->slowThreshold <= $q['executionMS'],
);
$totalExecTime += $q['executionMS'];
if (($q['type'] ?? null) === 'transaction') continue;
$nb_statements++;
}
if ($this->durationBackground && $totalExecTime > 0) {
// For showing background measure on Queries tab
$start_percent = 0;
foreach ($queries as $i => $stmt) {
if (!isset($stmt['duration'])) {
continue;
}
$width_percent = $stmt['duration'] / $totalExecTime * 100;
$queries[$i] = array_merge($stmt, [
'start_percent' => round($start_percent, 3),
'width_percent' => round($width_percent, 3),
]);
$start_percent += $width_percent;
}
}
return array(
'count' => count($queries),
'nb_statements' => $nb_statements,
'accumulated_duration' => $totalExecTime,
'accumulated_duration_str' => $this->getDataFormatter()->formatDuration($totalExecTime),
'statements' => $queries
);
}
/**
* Returns an array of parameters used with the query
*
* @return array
*/
public function getParameters($params) : array
{
return array_map(function ($param) {
if (is_string($param)) {
return htmlentities($param, ENT_QUOTES, 'UTF-8', false);
} elseif (is_array($param)) {
return '[' . implode(', ', $this->getParameters($param)) . ']';
} elseif (is_numeric($param)) {
return strval($param);
} elseif ($param instanceof \DateTimeInterface) {
return $param->format('Y-m-d H:i:s');
} elseif (is_object($param)) {
return json_encode($param);
}
return $param ?: '';
}, $params);
}
/**
* @return string
*/
public function getName(): string
{
return 'doctrine';
}
/**
* @return array
*/
public function getWidgets(): array
{
return array(
"database" => array(
"icon" => "arrow-right",
"widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
"map" => "doctrine",
"default" => "[]"
),
"database:badge" => array(
"map" => "doctrine.nb_statements",
"default" => 0
)
);
}
/**
* @return array
*/
public function getAssets(): array
{
return array(
'css' => 'widgets/sqlqueries/widget.css',
'js' => 'widgets/sqlqueries/widget.js'
);
}
}