Skip to content
Snippets Groups Projects
Commit 387222b6 authored by Jan-Hendrik Willms's avatar Jan-Hendrik Willms
Browse files

refine debugbar integration, re #4220

Merge request studip/studip!3050
parent 789a3147
No related branches found
No related tags found
No related merge requests found
<?php
final class DebugbarController extends Trails_Controller
{
public function __construct(
Trails\Dispatcher $dispatcher,
private readonly DebugBar\DebugBar $debugbar
) {
parent::__construct($dispatcher);
}
public function css_action(): void
{
$this->set_content_type('text/css;charset=utf-8');
$this->render_nothing();
app()->get(DebugBar\DebugBar::class)->getJavascriptRenderer()->dumpCssAssets();
$this->debugbar->getJavascriptRenderer()->dumpCssAssets();
}
public function js_action(): void
{
$this->set_content_type('text/javascript;charset=utf-8');
$this->render_nothing();
app()->get(DebugBar\DebugBar::class)->getJavascriptRenderer()->setIncludeVendors(false)->dumpJsAssets();
$this->debugbar->getJavascriptRenderer()->setIncludeVendors(false)->dumpJsAssets();
}
}
......@@ -14,7 +14,7 @@ $DB_STUDIP_HOST = $_ENV['MYSQL_HOST'] ?? 'localhost';
$DB_STUDIP_USER = $_ENV['MYSQL_USER'] ?? '';
$DB_STUDIP_PASSWORD = $_ENV['MYSQL_PASSWORD'] ?? '';
$DB_STUDIP_DATABASE = $_ENV['MYSQL_DATABASE'] ?? 'studip';
$DEBUG_ALL_DB_QUERIES_WITH_TRACE = false;
/*
// optional Stud.IP slave database
$DB_STUDIP_SLAVE_HOST = "localhost";
......
......@@ -40,14 +40,14 @@ return [
// Future Improvements, not used/activated right now
# $debugBar->addCollector(new MessagesCollector());
# $debugBar->addCollector(new TimeDataCollector());
$debugBar->addCollector(new TimeDataCollector());
$config = iterator_to_array(Config::getInstance()->getIterator());
ksort($config);
$debugBar->addCollector(new DebugBar\DataCollector\ConfigCollector($config));
$pdo = $container->get(PDO::class);
if ($pdo instanceof DebugBar\DataCollector\PDO\TraceablePDO) {
if ($pdo instanceof Studip\Debug\TraceableStudipPDO) {
$collector = new DebugBar\DataCollector\PDO\PDOCollector($pdo);
$debugBar->addCollector($collector);
}
......@@ -62,7 +62,7 @@ return [
);
if (Studip\Debug\DebugBar::isActivated()) {
$pdo = new DebugBar\DataCollector\PDO\TraceablePDO($pdo);
$pdo = new Studip\Debug\TraceableStudipPDO($pdo);
}
return $pdo;
......
<?php
namespace Studip\Debug;
use DebugBar\DataCollector\PDO\TraceablePDO;
final class TraceableStudipPDO extends TraceablePDO
{
/**
* Quotes a string for use in a query.
*
* @link http://php.net/manual/en/pdo.quote.php
* @param string $string The string to be quoted.
* @param int $parameter_type [optional] Provides a data type hint for drivers that have
* alternate quoting styles.
* @return string|bool A quoted string that is theoretically safe to pass into an SQL statement.
* Returns FALSE if the driver does not support quoting in this way.
*/
#[\ReturnTypeWillChange]
public function quote($string, $parameter_type = null)
{
return $this->pdo->quote($string, $parameter_type);
}
}
......@@ -23,7 +23,6 @@ class StudipPDO extends PDO
// Counter for the queries sent to the database
public $query_count = 0;
public $queries = [];
/**
* Verifies that the given SQL query only contains a single statement.
......@@ -42,26 +41,6 @@ class StudipPDO extends PDO
// Count executed queries (this is placed here since this is the only
// method that is executed on every call to the database)
$this->query_count += 1;
if (!empty($GLOBALS['DEBUG_ALL_DB_QUERIES'])) {
$trace = debug_backtrace();
$classes = [];
if (isset($trace[2]['class']) && $trace[2]['class'] === 'SimpleORMap') {
$classes[] = 'sorm';
}
if (isset($trace[1]) && $trace[1]['function'] === 'prepare') {
$classes[] = 'prepared';
}
$this->queries[] = [
'query' => implode("\n", array_filter(array_map('trim', explode("\n", $statement)))),
'classes' => implode(' ', $classes),
'trace' => $GLOBALS['DEBUG_ALL_DB_QUERIES_WITH_TRACE']
? array_slice($trace, 2)
: null,
];
}
}
/**
......
<? if (!empty($GLOBALS['DEBUG_ALL_DB_QUERIES'])) : ?>
<style>
#all_db_queries td:first-child {
border-left: 4px solid transparent;
border-right: 4px solid red;
}
#all_db_queries .sorm td:first-child {
border-left-color: blue;
}
#all_db_queries .prepared td:first-child {
border-right-color: green;
}
#all_db_queries .query {
white-space: pre-wrap;
}
#all_db_queries ul {
counter-reset: queries -1;
}
#all_db_queries li:hover {
text-decoration: underline;
}
#all_db_queries li::before {
content: "#" counter(queries);
counter-increment: queries;
}
#all_db_queries li::before,
#all_db_queries span {
font-weight: lighter;
}
</style>
<div style="display: none;" id="all_db_queries">
<table class="default">
<tbody>
<? foreach ((array) DBManager::get()->queries as $query) : ?>
<tr class="<?= $query['classes'] ?>">
<td>
<code class="query"><?= htmlReady($query['query']) ?></code>
</td>
<? if ($GLOBALS['DEBUG_ALL_DB_QUERIES_WITH_TRACE']) : ?>
<td>
<ul class="list-unstyled">
<? foreach ($query['trace'] as $i => $row): ?>
<li>
<?= $this->render_partial('debug/trace-row.php', $row) ?>
</li>
<? endforeach; ?>
</ul>
</td>
<? endif ?>
</tr>
<? endforeach ?>
</tbody>
</table>
</div>
<? endif ?>
<?php
$file = ltrim(str_replace($GLOBALS['STUDIP_BASE_PATH'], '', $file), '/');
$trac = htmlReady("https://develop.studip.de/trac/browser/trunk/{$file}#L{$line}");
?>
<code>
<?= htmlReady(sprintf(
'%s(%s)',
isset($class) ? "{$class}{$type}{$function}" : $function,
implode(', ', array_map(function ($arg) { return is_object($arg) ? get_class($arg) : (string) $arg; }, $args))
)) ?>
</code>
<span>called at</span>
<a href="<?= $trac ?>" target="_blank"><?= htmlReady("{$file}:{$line}") ?></a>
......@@ -9,29 +9,6 @@
htmlReady($GLOBALS['user']->perms)) ?>
|
<?= strftime('%x, %X') ?>
<? if (Studip\ENV === 'development'): ?>
[
<? if (DBManager::get('studip') === DBManager::get('studip-slave')): ?>
<?= sprintf('%u db queries', DBManager::get('studip')->query_count) ?>
<? else: ?>
<?= sprintf(
'M%u/S%u = %u db queries',
DBManager::get('studip')->query_count,
DBManager::get('studip-slave')->query_count,
DBManager::get('studip')->query_count + DBManager::get('studip-slave')->query_count
) ?>
<? endif; ?>
/
<?= relsize(memory_get_peak_usage(true), false) ?> mem
/
<?= sprintf('%.5f sec', microtime(true) - $GLOBALS['STUDIP_STARTUP_TIME']) ?>
]
<? if (!empty($GLOBALS['DEBUG_ALL_DB_QUERIES'])) : ?>
<a href="" onClick="jQuery('#all_db_queries').toggle(); return false;">
<?= Icon::create("code", "info_alt")->asImg(16, ['class' => "text-bottom"]) ?>
</a>
<? endif ?>
<? endif; ?>
</div>
<? endif; ?>
......@@ -57,5 +34,4 @@
<? endif; ?>
</footer>
<? endif; ?>
<?= $this->render_partial('debug/db-log.php') ?>
<!-- Ende Footer -->
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment