Skip to content

Commit 0d552cf

Browse files
committed
Delegate HTML rendering of query metadata to LaravelSQLQueriesWidget
1 parent 506c1eb commit 0d552cf

File tree

4 files changed

+255
-143
lines changed

4 files changed

+255
-143
lines changed

src/DataCollector/QueryCollector.php

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function addQuery($query, $bindings, $time, $connection)
124124

125125
$this->queries[] = [
126126
'query' => $query,
127+
'type' => 'query',
127128
'bindings' => $this->getDataFormatter()->escapeBindings($bindings),
128129
'time' => $time,
129130
'source' => $source,
@@ -208,7 +209,6 @@ protected function parseTrace($index, array $trace)
208209
'index' => $index,
209210
'namespace' => null,
210211
'name' => null,
211-
'file' => null,
212212
'line' => isset($trace['line']) ? $trace['line'] : '?',
213213
];
214214

@@ -223,12 +223,12 @@ protected function parseTrace($index, array $trace)
223223
DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR
224224
) === false
225225
) {
226-
$frame->file = $trace['file'];
226+
$file = $trace['file'];
227227

228228
if (isset($trace['object']) && is_a($trace['object'], 'Twig_Template')) {
229-
list($frame->file, $frame->line) = $this->getTwigInfo($trace);
230-
} elseif (strpos($frame->file, storage_path()) !== false) {
231-
$hash = pathinfo($frame->file, PATHINFO_FILENAME);
229+
list($file, $frame->line) = $this->getTwigInfo($trace);
230+
} elseif (strpos($file, storage_path()) !== false) {
231+
$hash = pathinfo($file, PATHINFO_FILENAME);
232232

233233
if (! $frame->name = $this->findViewFromHash($hash)) {
234234
$frame->name = $hash;
@@ -237,19 +237,19 @@ protected function parseTrace($index, array $trace)
237237
$frame->namespace = 'view';
238238

239239
return $frame;
240-
} elseif (strpos($frame->file, 'Middleware') !== false) {
241-
$frame->name = $this->findMiddlewareFromFile($frame->file);
240+
} elseif (strpos($file, 'Middleware') !== false) {
241+
$frame->name = $this->findMiddlewareFromFile($file);
242242

243243
if ($frame->name) {
244244
$frame->namespace = 'middleware';
245245
} else {
246-
$frame->name = $this->normalizeFilename($frame->file);
246+
$frame->name = $this->normalizeFilename($file);
247247
}
248248

249249
return $frame;
250250
}
251251

252-
$frame->name = $this->normalizeFilename($frame->file);
252+
$frame->name = $this->normalizeFilename($file);
253253

254254
return $frame;
255255
}
@@ -355,6 +355,7 @@ public function collectTransactionEvent($event, $connection)
355355

356356
$this->queries[] = [
357357
'query' => $event,
358+
'type' => 'transaction',
358359
'bindings' => [],
359360
'time' => 0,
360361
'source' => $source,
@@ -384,13 +385,15 @@ public function collect()
384385
foreach ($queries as $query) {
385386
$totalTime += $query['time'];
386387

387-
$metadata = $this->getDataFormatter()->formatMetadata($query);
388-
389388
$statements[] = [
390389
'sql' => $this->getDataFormatter()->formatSql($query['query']),
391-
'params' => $metadata,
390+
'type' => $query['type'],
391+
'params' => [],
392+
'bindings' => $query['bindings'],
393+
'hints' => $query['hints'],
394+
'backtrace' => array_values($query['source']),
392395
'duration' => $query['time'],
393-
'duration_str' => $this->formatDuration($query['time']),
396+
'duration_str' => ($query['type'] == 'transaction') ? '' : $this->formatDuration($query['time']),
394397
'stmt_id' => $this->getDataFormatter()->formatSource(reset($query['source'])),
395398
'connection' => $query['connection'],
396399
];
@@ -399,15 +402,20 @@ public function collect()
399402
foreach($query['explain'] as $explain){
400403
$statements[] = [
401404
'sql' => ' - EXPLAIN #' . $explain->id . ': `' . $explain->table . '` (' . $explain->select_type . ')',
405+
'type' => 'explain',
402406
'params' => $explain,
403407
'row_count' => $explain->rows,
404408
'stmt_id' => $explain->id,
405409
];
406410
}
407411
}
408412

413+
$nb_statements = array_filter($queries, function ($query) {
414+
return $query['type'] == 'query';
415+
});
416+
409417
$data = [
410-
'nb_statements' => count($queries),
418+
'nb_statements' => count($nb_statements),
411419
'nb_failed_statements' => 0,
412420
'accumulated_duration' => $totalTime,
413421
'accumulated_duration_str' => $this->formatDuration($totalTime),
@@ -432,7 +440,7 @@ public function getWidgets()
432440
return [
433441
"queries" => [
434442
"icon" => "database",
435-
"widget" => "PhpDebugBar.Widgets.SQLQueriesWidget",
443+
"widget" => "PhpDebugBar.Widgets.LaravelSQLQueriesWidget",
436444
"map" => "queries",
437445
"default" => "[]"
438446
],

src/DataFormatter/QueryFormatter.php

Lines changed: 3 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class QueryFormatter extends DataFormatter
1010
/**
1111
* Removes extra spaces at the beginning and end of the SQL query and its lines.
1212
*
13-
* @param string $sql
13+
* @param string $sql
1414
* @return string
1515
*/
1616
public function formatSql($sql)
@@ -31,6 +31,7 @@ public function checkBindings($bindings)
3131
$binding = '[BINARY DATA]';
3232
}
3333
}
34+
3435
return $bindings;
3536
}
3637

@@ -45,61 +46,8 @@ public function escapeBindings($bindings)
4546
foreach ($bindings as &$binding) {
4647
$binding = htmlentities($binding, ENT_QUOTES, 'UTF-8', false);
4748
}
48-
return $bindings;
49-
}
50-
51-
/**
52-
* Format query bindings into an ordered list.
53-
*
54-
* @param array $bindings
55-
* @return string
56-
*/
57-
public function formatBindings(array $bindings)
58-
{
59-
$bindings = array_map(function ($binding, $index) {
60-
return '<span class="phpdebugbar-text-muted">' . $index . '.</span> ' . $binding;
61-
}, $bindings, array_keys($bindings));
62-
63-
return $this->formatList($bindings);
64-
}
65-
66-
/**
67-
* Format the hints into an list.
68-
*
69-
* @param array $hints
70-
* @return string
71-
*/
72-
public function formatHints(array $hints)
73-
{
74-
return $this->formatList($hints);
75-
}
76-
77-
/**
78-
* Format the backtrace sources into an ordered list.
79-
*
80-
* @param array $sources
81-
* @return string
82-
*/
83-
public function formatSources(array $sources)
84-
{
85-
$items = [];
86-
87-
foreach ($sources as $source) {
88-
$parts = [
89-
'index' => '<span class="phpdebugbar-text-muted">' . $source->index . '.</span>&nbsp;',
90-
];
91-
92-
if ($source->namespace) {
93-
$parts['namespace'] = $source->namespace . '::';
94-
}
95-
96-
$parts['name'] = $source->name;
97-
$parts['line'] = '<span class="phpdebugbar-text-muted">:' . $source->line . '</span>';
98-
99-
$items[] = implode($parts);
100-
}
10149

102-
return $this->formatList($items);
50+
return $bindings;
10351
}
10452

10553
/**
@@ -125,77 +73,4 @@ public function formatSource($source)
12573

12674
return implode($parts);
12775
}
128-
129-
/**
130-
* Generate an array with metadata about the query.
131-
*
132-
* @param array $query
133-
* @return object
134-
*/
135-
public function formatMetadata(array $query)
136-
{
137-
$metadata = (object) [];
138-
139-
if ($query['bindings']) {
140-
$metadata = $this->addMetadata(
141-
$metadata, 'bindings', $this->formatBindings($query['bindings'])
142-
);
143-
}
144-
145-
if ($query['hints']) {
146-
$metadata = $this->addMetadata(
147-
$metadata, 'hints', $this->formatHints($query['hints'])
148-
);
149-
}
150-
151-
if ($query['source']) {
152-
$metadata = $this->addMetadata(
153-
$metadata, 'backtrace', $this->formatSources($query['source'])
154-
);
155-
}
156-
157-
return $metadata;
158-
}
159-
160-
/**
161-
* Append an item to the metadata.
162-
*
163-
* @param object $metadata
164-
* @param string $name
165-
* @param string $parameter
166-
* @return object
167-
*/
168-
public function addMetadata($metadata, $name, $parameter)
169-
{
170-
$icons = [
171-
'bindings' => 'thumb-tack',
172-
'hints' => 'question-circle',
173-
'backtrace' => 'list-ul',
174-
];
175-
176-
$icon = isset($icons[$name]) ? $icons[$name] : 'circle';
177-
178-
$key = ucfirst($name) . ' <i class="phpdebugbar-fa phpdebugbar-fa-' . $icon . ' phpdebugbar-text-muted"></i>';
179-
180-
$metadata->$key = $parameter;
181-
182-
return $metadata;
183-
}
184-
185-
/**
186-
* Format an array into a list.
187-
*
188-
* @param array $items
189-
* @return string
190-
*/
191-
protected function formatList(array $items)
192-
{
193-
$list = [];
194-
195-
foreach ($items as $item) {
196-
$list[] = '<li class="phpdebugbar-widgets-table-list-item">' . $item . '</li>';
197-
}
198-
199-
return '<ul class="phpdebugbar-widgets-table-list">' . implode($list) . '</ul>';
200-
}
20176
}

src/Resources/laravel-debugbar.css

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,10 @@ ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-list-item {
203203
flex-wrap: wrap;
204204
}
205205

206+
.phpdebugbar-widgets-sql.phpdebugbar-widgets-name {
207+
font-weight: bold;
208+
}
209+
206210
ul.phpdebugbar-widgets-list li.phpdebugbar-widgets-list-item .phpdebugbar-widgets-sql {
207211
flex: 1;
208212
margin-right: 5px;

0 commit comments

Comments
 (0)