Commit 57a04ad8 authored by Tomáš Pospíšil's avatar Tomáš Pospíšil
Browse files

optimized dumping

parent 073b57a5
......@@ -4,19 +4,17 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\Dumper;
use Praguebest\Tools\OnOffSwitcher;
use Praguebest\Tools\SmartyTrace\InternalAssign;
use Praguebest\Tools\TraceableSmarty;
use Smarty;
use SmartyException;
use Tracy;
use Tracy\Dumper;
use Tracy\Helpers;
final class InternalSmartyBarPanel implements Tracy\IBarPanel
{
private static $maxDepth = 8;
private static $maxLength = 512;
public $data;
public $id = 'database';
......@@ -63,26 +61,18 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
private function assignSmarty(): void
{
$variables = $this->currentSmarty->getAssigns();
foreach ($variables as $name => &$assigns) {
foreach ($assigns as &$assign) {
$assign['backtrace'] = array_map(
foreach ($variables as $name => $assigns) {
foreach ($assigns as $assign) {
assert($assign instanceof InternalAssign);
$assign->backtraces = array_map(
static function ($backtrace) {
return Helpers::editorLink($backtrace);
return urldecode(Helpers::editorLink($backtrace));
},
$assign['backtrace']
$assign->backtraces
);
$assign['first'] = array_shift($assign['backtrace']);
$assign->first = array_shift($assign->backtraces);
$assign['value'] = Dumper::toHtml(
$assign['value'],
[
Dumper::DEPTH => self::$maxDepth,
Dumper::TRUNCATE => self::$maxLength,
Dumper::LOCATION => Dumper::LOCATION_CLASS,
Dumper::LAZY => true,
Dumper::DEBUGINFO => true,
]
);
$assign->value = Dumper::toHtml($assign->value);
}
}
$this->smarty->assign('internalVariables', $variables);
......
......@@ -4,18 +4,15 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\Dumper;
use Praguebest\Tools\OnOffSwitcher;
use Praguebest\Tools\TraceableSmarty;
use Smarty;
use SmartyException;
use Tracy;
use Tracy\Dumper;
final class SmartyBarPanel implements Tracy\IBarPanel
{
private static $maxDepth = 8;
private static $maxLength = 512;
public $data;
public $id = 'database';
......@@ -65,23 +62,14 @@ final class SmartyBarPanel implements Tracy\IBarPanel
$smartyVars = [];
if (OnOffSwitcher::shouldProfile(TraceableSmarty::SMARTY_VARIABLES)) {
foreach ($this->currentSmarty->getBacktraces() as $key => $backtrace) {
$smartyBacktrace[$key] = Helper::getBacktrace($backtrace);
$smartyBacktrace[$key] = Dumper::getBacktrace($backtrace);
}
$this->smarty->assign('smartyBacktrace', $smartyBacktrace);
$smartyVars =
array_map(
static function ($value) {
return Dumper::toHtml(
$value,
[
Dumper::DEPTH => self::$maxDepth,
Dumper::TRUNCATE => self::$maxLength,
Dumper::LOCATION => Dumper::LOCATION_CLASS,
Dumper::LAZY => true,
Dumper::DEBUGINFO => true,
]
);
return Dumper::toHtml($value);
},
array_combine(array_keys($this->currentSmarty->tpl_vars), array_column($this->currentSmarty->tpl_vars, 'value'))
);
......
<?php /** @noinspection GlobalVariableUsageInspection */
<?php
/** @noinspection GlobalVariableUsageInspection */
namespace Praguebest\Tools;
use PDOStatement;
use Praguebest\BarPanel\Helper;
final class DBProfiler
{
......@@ -43,6 +43,7 @@ final class DBProfiler
return DBProfiler::$instance;
}
/** @noinspection PhpUnused */
public function profilePublisher(PDOStatement $statement, ?array $placeholders, ?array $extraParams): bool
{
$start = $this->timer();
......@@ -90,7 +91,7 @@ final class DBProfiler
if (isset($this->queriesProfiler[$hash])) {
$this->queriesProfiler[$hash]['time'] += $time;
$this->queriesProfiler[$hash]['count']++;
$this->queriesProfiler[$hash]['calls'][] = Helper::getBacktrace($backtrace);
$this->queriesProfiler[$hash]['calls'][] = Dumper::getBacktrace($backtrace);
} else {
$sql = trim($sql);
$sql = $this->highlight($sql);
......@@ -99,7 +100,7 @@ final class DBProfiler
'query' => $sql,
'time' => $time,
'count' => 1,
'calls' => [Helper::getBacktrace($backtrace)],
'calls' => [Dumper::getBacktrace($backtrace)],
];
}
}
......
......@@ -2,12 +2,12 @@
declare(strict_types=1);
namespace Praguebest\BarPanel;
namespace Praguebest\Tools;
use Nette\Utils\Strings;
use Smarty;
use Tracy\Helpers;
final class Helper
final class Dumper
{
private static $genericClasses = [
'Db',
......@@ -18,21 +18,10 @@ final class Helper
'EPModel',
'EPBase',
self::class,
\Praguebest\Tools\DBProfiler::class,
DBProfiler::class,
];
public static function getInternalSmartyBacktrace(array $backtraces = null): array
{
$filtered = [];
foreach ($backtraces as $backtrace) {
$match = Strings::match($backtrace['file'], '#.*file\.(.*\.tpl).*#');
if ($match) {
$filtered[] = $match[1];
}
}
return $filtered;
}
private static $maxDepth;
private static $maxLength;
public static function getBacktrace(array $backtraces = null): array
{
......@@ -41,9 +30,8 @@ final class Helper
}
$lines = [];
$first = null;
foreach ($backtraces as $i => $iValue) {
foreach ($backtraces as $i => $backtrace) {
$isGeneric = false;
$backtrace = $iValue;
$nextBacktrace = $backtrace;
if (array_key_exists($i + 1, $backtraces)) {
$nextBacktrace = $backtraces[$i + 1];
......@@ -82,4 +70,35 @@ final class Helper
return compact('lines', 'first');
}
/**
* @param mixed $value
* @return string
* @noinspection MissingParameterTypeDeclarationInspection
*/
public static function toHtml($value): string
{
return \Tracy\Dumper::toHtml(
$value,
[
\Tracy\Dumper::DEPTH => self::$maxDepth,
\Tracy\Dumper::TRUNCATE => self::$maxLength,
\Tracy\Dumper::LOCATION => \Tracy\Dumper::LOCATION_CLASS,
\Tracy\Dumper::LAZY => true,
\Tracy\Dumper::DEBUGINFO => true,
\Tracy\Dumper::OBJECT_EXPORTERS => [
'Context' => [self::class, 'skipDump'],
'Database' => [self::class, 'skipDump'],
'Db' => [self::class, 'skipDump'],
'StorageReflection' => [self::class, 'skipDump'],
Smarty::class => [self::class, 'skipDump'],
],
]
);
}
public static function skipDump(): array
{
return ['Skipped'];
}
}
......@@ -30,9 +30,7 @@ final class OnOffSwitcher
private static function getFromSession(string $profileName): bool
{
$profileValue = $_SESSION[self::PREFIX . $profileName] ?? false;
return (bool)$profileValue;
return (bool)($_SESSION[self::PREFIX . $profileName] ?? null);
}
private static function setToSession(string $profileName, bool $value): void
......
......@@ -50,12 +50,14 @@ final class PublisherQueryFormatter
$sql = str_replace($key, $param, $sql);
}
return $sql . ";";
return $sql . ';';
}
/**
* @param mixed $param
* @return mixed
* @noinspection MissingReturnTypeInspection
* @noinspection MissingParameterTypeDeclarationInspection
*/
private function debugGetParam($param)
{
......
<?php
declare(strict_types=1);
namespace Praguebest\Tools;
use Nette\Utils\Strings;
......
......@@ -18,17 +18,17 @@
{if $assign@first}
<td rowspan="{count($assigns)}">{$name}</td>
{/if}
<td>{$assign['first'] nofilter}
{if count($assign['backtrace']) > 0}
<td>{$assign->first|unescape:'html'}
{if count($assign->backtraces) > 0}
<a class="tracy-toggle tracy-collapsed" data-tracy-ref="^tr .nette-DbConnectionPanel-trace"></a>
<div class="tracy-collapsed nette-DbConnectionPanel-trace">
{foreach $assign['backtrace'] as $backtrace}
<div>{$backtrace nofilter}</div>
{foreach $assign->backtraces as $backtrace}
<div>{$backtrace|unescape:'html'}</div>
{/foreach}
</div>
{/if}
</td>
<td>{$assign['value'] nofilter}</td>
<td>{$assign->value nofilter}</td>
</tr>
{/foreach}
{/foreach}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment