sqlLogger = new DebugStack();
$doctrineConfiguration->setSQLLogger($this->sqlLogger);
}
public function getTab() {
$queries = $this->sqlLogger->queries;
$count = count($queries);
$countSuffix = $count === 1 ? 'query' : 'queries';
$time = $this->formatTime(array_sum(array_column($queries, 'executionMS')));
$img = '
';
return $img . '' . "$count $count_suffix / $time ms" . '';
}
public function getPanel() {
ob_start();
require __DIR__ . '/doctrine-panel.phtml';
return ob_get_clean();
}
public static function init(EntityManagerInterface $entityManager) {
Debugger::getBar()->addPanel(new DoctrinePanel($entityManager->getConnection()->getConfiguration()));
}
protected function formatSql($sql) {
// format SELECT queries a bit (wrap long select clauses, wrap lines on some keywords)
preg_match('/^(SELECT\s+)(.*?)(\s+FROM\s+)(.*?)$/iu', trim($sql), $matches);
if (count($matches) >= 5) {
// if SELECT clause over 50 chars, make it wrappable
$selectHtml = mb_strlen($matches[2]) > 50 ? ($matches[1] . '
...
' . $matches[2] . '
') : ($matches[1] . $matches[2]);
$fromKeyword = $matches[3];
$rest = $matches[4];
// try to match & indent WHERE clause
$whereHtml = '';
preg_match('/^(.*)(\s+WHERE\s+)(.*?)$/iu', $rest, $matches);
if (count($matches) >= 4) {
$whereHtml = $matches[1] . '
' . $matches[2];
$rest = $matches[3];
}
// try to match & indent ORDER BY/GROUP BY/LIMIT/OFFSET
$endHtml = '';
preg_match('/^(.*?)(\s+(?:ORDER\s+BY|GROUP\s+BY|LIMIT|OFFSET)\s+)(.*)$/iu', $rest, $matches);
if (count($matches) >= 4) {
$endHtml = $matches[1] . '
' . $matches[2];
$rest = $matches[3];
}
$sql = $selectHtml . '' . $fromKeyword . $whereHtml . $endHtml . $rest;
}
// highlight keywords
$keywords = new MySQLKeywords();
$tokens = preg_split('/(\s+)/', $sql, -1, PREG_SPLIT_DELIM_CAPTURE);
$output = '';
foreach ($tokens as $token) {
$output .= $keywords->isKeyword($token) ? ('' . $token . '') : $token;
}
return $output;
}
protected function formatArrayData($data) {
return preg_replace(
'#^\s{4}#m', '', // remove 1rst "tab" of the JSON result
substr(
json_encode($data, JSON_PRETTY_PRINT | JSON_NUMERIC_CHECK),
2, // remove "[\n"
-2 // remove "\n]"
)
);
}
protected function transformNumericType($data) {
$search = [
'#\b101\b#', // array of int
'#\b102\b#', // array of string
];
$replace = [
'integer[]', // array of int
'string[]', // array of string
];
return preg_replace($search, $replace, $data);
}
protected function formatTime($doctrineTime) {
return number_format($doctrineTime * 1000, 1, '.', ' ');
}
}