Commit 7465fc41 authored by Tomáš Pospíšil's avatar Tomáš Pospíšil
Browse files

errors

parent 327ea350
{
"name": "praguebest/tracybars",
"description": "Collection of bars for Tracy",
"type": "library",
"description": "Collection of bars for Tracy",
"license": "proprietary",
"authors": [
{
......@@ -9,19 +9,29 @@
"email": "pospisil@praguebest.cz"
}
],
"require": {
"php": "^7.1",
"ext-dom": "*",
"ext-pdo": "*",
"nette/utils": "^3.0",
"noximo/dbgr": "^0.1.5",
"smarty/smarty": "^3.1",
"tracy/tracy": "^2.7",
"voku/html-min": "^4.3"
},
"require-dev": {
"ergebnis/composer-normalize": "^1.3.1",
"roave/security-advisories": "dev-master"
},
"autoload": {
"psr-4": {
"Praguebest\\": "src/"
}
},
"require": {
"php": "^7.1",
"noximo/dbgr": "^0.1.5",
"tracy/tracy": "^2.7",
"nette/utils": "^3.0",
"smarty/smarty": "^3.1",
"ext-pdo": "*",
"ext-dom": "*",
"voku/arrayy": "^7.4"
"scripts": {
"post-update-cmd": [
"composer composer-normalize"
],
"composer-normalize": "composer normalize --indent-size=2 --indent-style=space -q -n"
}
}
This diff is collapsed.
<?php
declare(strict_types=1);
namespace Praguebest\BarPanel;
use Nette\Utils\Strings;
use Throwable;
use Tracy\IBarPanel;
use voku\helper\HtmlMin;
abstract class BaseBarPanel implements IBarPanel
{
public $id = __CLASS__;
/**
* @var HtmlMin|null
*/
private $minifier;
final public function getPanel(): string
{
return $this->minify($this->generatePanel());
}
private function minify(string $generatePanel): string
{
try {
return $this->getHtmlMinifier()->minify($generatePanel);
} catch (Throwable $e) {
return $generatePanel;
}
}
private function getHtmlMinifier(): HtmlMin
{
if ($this->minifier !== null) {
return $this->minifier;
}
$htmlMin = new HtmlMin();
$htmlMin->doOptimizeViaHtmlDomParser(); // optimize html via "HtmlDomParser()"
$htmlMin->doRemoveComments(); // remove default HTML comments (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doSumUpWhitespace(); // sum-up extra whitespace from the Dom (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doRemoveWhitespaceAroundTags(); // remove whitespace around tags (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doOptimizeAttributes(); // optimize html attributes (depends on "doOptimizeViaHtmlDomParser(true)")
$htmlMin->doRemoveHttpPrefixFromAttributes(); // remove optional "http:"-prefix from attributes (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveHttpsPrefixFromAttributes(); // remove optional "https:"-prefix from attributes (depends on "doOptimizeAttributes(true)")
$htmlMin->doKeepHttpAndHttpsPrefixOnExternalAttributes(); // keep "http:"- and "https:"-prefix for all external links
$htmlMin->doRemoveDefaultAttributes(); // remove defaults (depends on "doOptimizeAttributes(true)" | disabled by default)
$htmlMin->doRemoveDeprecatedAnchorName(); // remove deprecated anchor-jump (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedScriptCharsetAttribute(); // remove deprecated charset-attribute - the browser will use the charset from the HTTP-Header, anyway (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedTypeFromScriptTag(); // remove deprecated script-mime-types (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveDeprecatedTypeFromStylesheetLink(); // remove "type=text/css" for css links (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveEmptyAttributes(); // remove some empty attributes (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveValueFromEmptyInput(); // remove 'value=""' from empty <input> (depends on "doOptimizeAttributes(true)")
$htmlMin->doSortCssClassNames(); // sort css-class-names, for better gzip results (depends on "doOptimizeAttributes(true)")
$htmlMin->doSortHtmlAttributes(); // sort html-attributes, for better gzip results (depends on "doOptimizeAttributes(true)")
$htmlMin->doRemoveSpacesBetweenTags(); // remove more (aggressive) spaces in the dom (disabled by default)
$htmlMin->doRemoveOmittedQuotes(); // remove quotes e.g. class="lall" => class=lall
$htmlMin->doRemoveOmittedHtmlTags(); // remove ommitted html tags e.g. <p>lall</p> => <p>lall
$this->minifier = $htmlMin;
return $this->minifier;
}
abstract public function generatePanel(): string;
final public function getTab(): string
{
return $this->minify($this->generateTab());
}
abstract public function generateTab(): string;
final public function getId(): string
{
return Strings::webalize(static::class);
}
}
......@@ -7,14 +7,9 @@ namespace Praguebest\BarPanel;
use Praguebest\Tools;
use Smarty;
use SmartyException;
use Tracy;
final class DBProfileBar implements Tracy\IBarPanel
final class DBProfileBar extends BaseBarPanel
{
public $data;
public $id = 'database';
/**
* @var SmartyBarPanel
*/
......@@ -35,7 +30,7 @@ final class DBProfileBar implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getPanel(): string
public function generatePanel(): string
{
$this->assignSmarty();
......@@ -54,7 +49,7 @@ final class DBProfileBar implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getTab(): string
public function generateTab(): string
{
$this->assignSmarty();
......
......@@ -7,10 +7,8 @@ namespace Praguebest\BarPanel;
use Praguebest\Tools\ErrorFilesParser;
use Smarty;
use SmartyException;
use Tracy\IBarPanel;
use Tracy\ILogger;
final class ErrorCountBar implements IBarPanel
final class ErrorCountBar extends BaseBarPanel
{
/**
* @var Smarty
......@@ -31,19 +29,9 @@ final class ErrorCountBar implements IBarPanel
$errors = $parser->parse($errorDir);
$this->smarty->assign('errors', $errors);
$this->smarty->assign(
'criticals',
$errors->filterBy('type', [ILogger::CRITICAL, ILogger::EXCEPTION, ILogger::ERROR, ErrorFilesParser::UNKNOWN])->count()
);
$this->smarty->assign(
'warnings',
$errors->filterBy('type', ILogger::WARNING)->count()
);
$this->smarty->assign(
'infos',
$errors->filterBy('type', [ILogger::DEBUG, ILogger::INFO])->count()
);
$this->smarty->assign('criticals', $parser->countCriticals($errors));
$this->smarty->assign('warnings', $parser->countWarnings($errors));
$this->smarty->assign('infos', $parser->countInfos($errors));
}
/**
......@@ -51,7 +39,7 @@ final class ErrorCountBar implements IBarPanel
* @return string
* @throws SmartyException
*/
public function getPanel(): string
public function generatePanel(): string
{
return $this->smarty->fetch(dirname(__DIR__) . '/templates/errorCount/errors.tpl');
}
......@@ -61,7 +49,7 @@ final class ErrorCountBar implements IBarPanel
* @return string
* @throws SmartyException
*/
public function getTab(): string
public function generateTab(): string
{
return $this->smarty->fetch(dirname(__DIR__) . '/templates/errorCount/tab.tpl');
}
......
......@@ -5,20 +5,13 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\Dumper;
use Praguebest\Tools\OnOffChecker;
use Praguebest\Tools\SmartyTrace\InternalTrace;
use Praguebest\Tools\TraceableSmarty;
use Smarty;
use SmartyException;
use Tracy;
use Tracy\Helpers;
final class InternalSmartyBarPanel implements Tracy\IBarPanel
final class InternalSmartyBarPanel extends BaseBarPanel
{
public $data;
public $id = 'database';
/**
* @var Smarty
*/
......@@ -39,7 +32,7 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getPanel(): string
public function generatePanel(): string
{
$this->assignSmarty();
......@@ -57,7 +50,7 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
}
$assign->backtraces = array_map(
static function ($backtrace) {
return urldecode(Helpers::editorLink($backtrace));
return urldecode(Dumper::editorLink($backtrace));
},
$assign->backtraces
);
......@@ -69,7 +62,6 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
}
$this->smarty->assign('internalVariables', $variables);
$this->smarty->assign('countInternalVariables', count($variables));
$this->smarty->assign('profileInternalVariables', OnOffChecker::getCurrentSetting(TraceableSmarty::INTERNAL_VARIABLES));
}
/**
......@@ -77,7 +69,7 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getTab(): string
public function generateTab(): string
{
$this->assignSmarty();
......
......@@ -9,6 +9,8 @@ use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Praguebest\Tools;
use Praguebest\Tools\DBProfiler;
use Praguebest\Tools\Settings\OnOff;
use Praguebest\Tools\Settings\StringSetting;
use Praguebest\Tools\WebSwitcherUrls;
use Smarty;
use Tracy\Debugger;
......@@ -18,28 +20,62 @@ final class Registrator
{
/** @var self */
private static $instance;
/** @var BaseBarPanel[] */
private static $registeredPanels = [];
/** @var Smarty */
private $smarty;
/** @var IBarPanel[] */
private $registeredPanels = [];
/**
* @var ArrayObject
*/
private $settings;
private function __construct(string $compileDir, string $cacheDir)
{
$this->initSmarty($cacheDir, $compileDir);
$this->settings = new ArrayObject();
$this->addSettingsBar();
register_shutdown_function([__CLASS__, 'shutdownHandler']);
}
/**
* @param string $cacheDir
* @param string $compileDir
*/
private function initSmarty(string $cacheDir, string $compileDir): void
{
$this->smarty = new Smarty();
$this->smarty->setCacheDir($cacheDir);
$this->smarty->setCompileDir($compileDir);
$this->smarty->setCaching(Smarty::CACHING_OFF);
$this->smarty->setCompileCheck(Smarty::COMPILECHECK_ON);
$this->smarty->setForceCompile(false);
}
$this->settings = new ArrayObject();
private function addSettingsBar(): void
{
$settingsBarPanel = function (): IBarPanel {
return new SettingsBarPanel($this->smarty, $this->settings);
};
$this->addPanel(
'settings', $settingsBarPanel
);
$settingsBarPanel = new SettingsBarPanel($this->smarty, $this->settings);
$this->settings->append(new StringSetting(Tools\Dumper::PROJECT_PATH, 'Local project path'));
}
$this->registeredPanels['settings'] = $settingsBarPanel;
private function addPanel(string $method, callable $panelCreator): void
{
if (PHP_SAPI !== 'cli' && !array_key_exists($method, self::$registeredPanels) && Debugger::isEnabled()) {
$panel = $panelCreator();
assert($panel instanceof IBarPanel);
self::$registeredPanels[$method] = $panel;
Debugger::getBar()->addPanel($settingsBarPanel);
Debugger::getBar()->addPanel($panel);
}
}
public static function getInstance(string $compileDir, string $cacheDir): Registrator
......@@ -51,6 +87,22 @@ final class Registrator
return self::$instance;
}
public static function shutdownHandler(): void
{
// if (PHP_SAPI !== 'cli' && Debugger::isEnabled() && !Helpers::isAjax() && Helpers::isHtmlMode()) {
// foreach (self::$registeredPanels as $name => $panel) {
// echo $panel->getId();
// echo "<div style='hidden'>";
// echo $panel->minifiedPanel();
// echo "</div>";
//
// echo "<div style='hidden' id='{$panel->id}'>";
// echo $panel->minifiedTab();
// echo "</div>";
// }
// }
}
/**
* @param string $errorDir
* @param string $emailDeveloper
......@@ -60,11 +112,11 @@ final class Registrator
{
FileSystem::createDir($errorDir);
$currentSetting = new Tools\Setting('Debugger::$strictMode', 'Tracy strict mode', true);
$currentSetting = new OnOff('Debugger::$strictMode', 'Tracy strict mode', true);
$this->settings->append($currentSetting);
Debugger::$strictMode = $currentSetting->isOn();
Debugger::$maxLength = 1024;
Debugger::$maxLength = 512;
Debugger::$maxDepth = 5;
Debugger::enable('42@84.42.168.182,42@185.156.123.46', realpath($errorDir), Strings::replace($emailDeveloper, '/,;/', ','));
......@@ -78,17 +130,6 @@ final class Registrator
return $this;
}
private function addPanel(string $method, callable $panelCreator): void
{
if (PHP_SAPI !== 'cli' && !array_key_exists($method, $this->registeredPanels) && Debugger::isEnabled()) {
$panel = $panelCreator();
assert($panel instanceof IBarPanel);
$this->registeredPanels[$method] = $panel;
Debugger::getBar()->addPanel($panel);
}
}
public function registerAll(Tools\TraceableSmarty $smarty, WebSwitcherUrls $urls): Registrator
{
if (Debugger::isEnabled()) {
......@@ -109,7 +150,7 @@ final class Registrator
}
);
$currentSetting = new Tools\Setting(DBProfiler::PROFILE_DB, 'Profile database');
$currentSetting = new OnOff(DBProfiler::PROFILE_DB, 'Profile database');
$this->settings->append($currentSetting);
......@@ -137,9 +178,7 @@ final class Registrator
}
);
$currentSetting = new Tools\Setting(Tools\TraceableSmarty::SMARTY_VARIABLES, 'List Smarty variables');
$this->settings->append($currentSetting);
$this->settings->append(new OnOff(Tools\TraceableSmarty::SMARTY_VARIABLES, 'List Smarty variables'));
return $this;
}
......@@ -153,11 +192,8 @@ final class Registrator
}
);
$currentSetting = new Tools\Setting(Tools\TraceableSmarty::INTERNAL_VARIABLES, 'List internal Smarty variables');
$this->settings->append($currentSetting);
$this->settings->append(new OnOff(Tools\TraceableSmarty::INTERNAL_VARIABLES, 'List internal Smarty variables'));
return $this;
}
}
......@@ -5,23 +5,18 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use ArrayObject;
use Praguebest\Tools\OnOffChecker;
use Praguebest\Tools\Settings\Setting;
use Smarty;
use SmartyException;
use Tracy;
final class SettingsBarPanel implements Tracy\IBarPanel
final class SettingsBarPanel extends BaseBarPanel
{
public $data;
public $id = 'settings';
/**
* @var Smarty
*/
private $smarty;
/**
* @var OnOffChecker[]
* @var Setting[]&ArrayObject
*/
private $switchers;
......@@ -41,9 +36,14 @@ final class SettingsBarPanel implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getPanel(): string
public function generatePanel(): string
{
$this->smarty->assign('switchers', $this->switchers);
$switchers = $this->switchers->uasort(
static function (Setting $a, Setting $b) {
return $a->getName() <=> $b->getName();
}
);
$this->smarty->assign('switchers', $switchers);
return $this->smarty->fetch(dirname(__DIR__) . '/templates/settings/settings.tpl');
}
......@@ -53,7 +53,7 @@ final class SettingsBarPanel implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getTab(): string
public function generateTab(): string
{
return $this->smarty->fetch(dirname(__DIR__) . '/templates/settings/tab.tpl');
}
......
......@@ -5,18 +5,13 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\Dumper;
use Praguebest\Tools\OnOffChecker;
use Praguebest\Tools\Settings\PersistentValue;
use Praguebest\Tools\TraceableSmarty;
use Smarty;
use SmartyException;
use Tracy;
final class SmartyBarPanel implements Tracy\IBarPanel
final class SmartyBarPanel extends BaseBarPanel
{
public $data;
public $id = 'database';
/**
* @var Smarty
*/
......@@ -37,7 +32,7 @@ final class SmartyBarPanel implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getPanel(): string
public function generatePanel(): string
{
$this->assignSmarty();
......@@ -48,7 +43,7 @@ final class SmartyBarPanel implements Tracy\IBarPanel
{
$smartyBacktrace = [];
$smartyVars = [];
if (OnOffChecker::getCurrentSetting(TraceableSmarty::SMARTY_VARIABLES)) {
if (PersistentValue::isOn(TraceableSmarty::SMARTY_VARIABLES)) {
foreach ($this->currentSmarty->getBacktraces() as $key => $backtrace) {
$smartyBacktrace[$key] = Dumper::getBacktrace($backtrace);
}
......@@ -66,7 +61,6 @@ final class SmartyBarPanel implements Tracy\IBarPanel
}
$this->smarty->assign('smartyVariables', $smartyVars);
$this->smarty->assign('countSmartyVariables', count($smartyVars));
$this->smarty->assign('profileSmartyVariables', OnOffChecker::getCurrentSetting(TraceableSmarty::SMARTY_VARIABLES));
}
/**
......@@ -74,7 +68,7 @@ final class SmartyBarPanel implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getTab(): string
public function generateTab(): string
{
$this->assignSmarty();
......
......@@ -4,15 +4,14 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\GitParser;
use Praguebest\Tools\PBSParser;
use Praguebest\Tools\WebSwitcherUrls;
use Smarty;
use SmartyException;
use Tracy;
final class WebSwitcher implements Tracy\IBarPanel
final class WebSwitcher extends BaseBarPanel
{
public $id = 'webswitcher';
/**
* @var Smarty
*/
......@@ -23,6 +22,18 @@ final class WebSwitcher implements Tracy\IBarPanel
$this->smarty = $smarty;
$this->smarty->assign('urls', $urls);
if ($urls->isLocal()) {
$gitParser = new GitParser();
$branchName = $gitParser->getBranchName();
$branches = $gitParser->getBranches();
}
$this->smarty->assign('branch', $branchName ?? null);
$this->smarty->assign('branches', $branches ?? []);
$this->smarty->assign('pbsParser', new PBSParser());
}
/**
......@@ -30,7 +41,7 @@ final class WebSwitcher implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getPanel(): string
public function generatePanel(): string
{
return $this->smarty->fetch(dirname(__DIR__) . '/templates/webswitcher/contexts.tpl');
}
......@@ -40,7 +51,7 @@ final class WebSwitcher implements Tracy\IBarPanel
* @return string
* @throws SmartyException
*/
public function getTab(): string
public function generateTab(): string
{
return $this->smarty->fetch(dirname(__DIR__) . '/templates/webswitcher/tab.tpl');
}
......
......@@ -6,6 +6,7 @@ declare(strict_types=1);
namespace Praguebest\Tools;
use PDOStatement;
use Praguebest\Tools\Settings\PersistentValue;
final class DBProfiler
{
......@@ -35,7 +36,7 @@ final class DBProfiler
private function initProfiling(): void
{
$this->profile = OnOffChecker::getCurrentSetting(self::PROFILE_DB);
$this->profile = PersistentValue::isOn(self::PROFILE_DB);
}
public static function getInstance(): self
......@@ -96,10 +97,12 @@ final class DBProfiler
$this->queriesProfiler[$hash]['calls'][] = Dumper::getBacktrace($backtrace);
} else {
$sql = trim($sql);
$sql = $this->highlight($sql);
$highlightedSql = function () use ($sql): string {
return $this->highlight($sql);
};