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

Settings in their own tab

parent aca2f9d6
......@@ -19,6 +19,7 @@
"noximo/dbgr": "^0.1.5",
"tracy/tracy": "^2.7",
"nette/utils": "^3.0",
"smarty/smarty": "^3.1"
"smarty/smarty": "^3.1",
"ext-pdo": "*"
}
}
......@@ -4,20 +4,20 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "81b69c965ce3eb2238488bb19d81133c",
"content-hash": "4025776c3ad17438001b07401afee348",
"packages": [
{
"name": "nette/utils",
"version": "v3.0.2",
"version": "v3.1.1",
"source": {
"type": "git",
"url": "https://github.com/nette/utils.git",
"reference": "c133e18c922dcf3ad07673077d92d92cef25a148"
"reference": "2c17d16d8887579ae1c0898ff94a3668997fd3eb"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nette/utils/zipball/c133e18c922dcf3ad07673077d92d92cef25a148",
"reference": "c133e18c922dcf3ad07673077d92d92cef25a148",
"url": "https://api.github.com/repos/nette/utils/zipball/2c17d16d8887579ae1c0898ff94a3668997fd3eb",
"reference": "2c17d16d8887579ae1c0898ff94a3668997fd3eb",
"shasum": ""
},
"require": {
......@@ -25,6 +25,7 @@
},
"require-dev": {
"nette/tester": "~2.0",
"phpstan/phpstan": "^0.12",
"tracy/tracy": "^2.3"
},
"suggest": {
......@@ -39,7 +40,7 @@
"type": "library",
"extra": {
"branch-alias": {
"dev-master": "3.0-dev"
"dev-master": "3.1-dev"
}
},
"autoload": {
......@@ -50,8 +51,8 @@
"notification-url": "https://packagist.org/downloads/",
"license": [
"BSD-3-Clause",
"GPL-2.0",
"GPL-3.0"
"GPL-2.0-only",
"GPL-3.0-only"
],
"authors": [
{
......@@ -81,7 +82,55 @@
"utility",
"validation"
],
"time": "2019-10-21T20:40:16+00:00"
"time": "2020-02-09T14:10:55+00:00"
},
{
"name": "noximo/dbgr",
"version": "0.1.5",
"source": {
"type": "git",
"url": "https://github.com/noximo/dbgr.git",
"reference": "1035dbcbc705a3bac364e483576e3beafa1e31b6"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/noximo/dbgr/zipball/1035dbcbc705a3bac364e483576e3beafa1e31b6",
"reference": "1035dbcbc705a3bac364e483576e3beafa1e31b6",
"shasum": ""
},
"require": {
"ext-json": "*",
"nette/utils": "^3.0",
"php": ">= 7.2",
"tracy/tracy": "^2.6"
},
"require-dev": {
"phpstan/phpstan": "^0.11",
"symplify/easy-coding-standard": "^6.0"
},
"type": "library",
"autoload": {
"psr-4": {
"noximo\\": "src/"
}
},
"notification-url": "https://packagist.org/downloads/",
"license": [
"MIT"
],
"authors": [
{
"name": "Tomas Pospisil",
"email": "pospisilt@gmail.com"
}
],
"description": "Versatile alternative for var_dump",
"keywords": [
"debug",
"dump",
"var_dump"
],
"time": "2019-09-30T09:25:56+00:00"
},
{
"name": "smarty/smarty",
......@@ -141,16 +190,16 @@
},
{
"name": "tracy/tracy",
"version": "v2.7.1",
"version": "v2.7.3",
"source": {
"type": "git",
"url": "https://github.com/nette/tracy.git",
"reference": "16691cf8b609b238a9a3596ee2878183a91b6446"
"reference": "344c77293038173f49678d9a95c27da6cbdbccd0"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/nette/tracy/zipball/16691cf8b609b238a9a3596ee2878183a91b6446",
"reference": "16691cf8b609b238a9a3596ee2878183a91b6446",
"url": "https://api.github.com/repos/nette/tracy/zipball/344c77293038173f49678d9a95c27da6cbdbccd0",
"reference": "344c77293038173f49678d9a95c27da6cbdbccd0",
"shasum": ""
},
"require": {
......@@ -162,9 +211,12 @@
"nette/di": "<3.0"
},
"require-dev": {
"latte/latte": "^2.5",
"nette/di": "^3.0",
"nette/mail": "^3.0",
"nette/tester": "^2.2",
"nette/utils": "^3.0",
"phpstan/phpstan": "^0.12",
"psr/log": "^1.0"
},
"suggest": {
......@@ -207,7 +259,7 @@
"nette",
"profiler"
],
"time": "2019-11-02T20:12:14+00:00"
"time": "2020-02-23T18:40:15+00:00"
}
],
"packages-dev": [],
......@@ -217,7 +269,9 @@
"prefer-stable": false,
"prefer-lowest": false,
"platform": {
"php": "^7.1"
"php": "^7.1",
"ext-pdo": "*"
},
"platform-dev": []
"platform-dev": [],
"plugin-api-version": "1.1.0"
}
......@@ -9,9 +9,8 @@ use Smarty;
use SmartyException;
use Tracy;
final class DBProfiler implements Tracy\IBarPanel
final class DBProfileBar implements Tracy\IBarPanel
{
public $data;
public $id = 'database';
......@@ -21,7 +20,7 @@ final class DBProfiler implements Tracy\IBarPanel
*/
private $smarty;
/**
* @var DBProfiler
* @var DBProfileBar
*/
private $sqlProfile;
......@@ -43,6 +42,13 @@ final class DBProfiler implements Tracy\IBarPanel
return $this->smarty->fetch(dirname(__DIR__) . '/templates/database/dbProfiler.tpl');
}
private function assignSmarty(): void
{
$this->smarty->assign('count', $this->sqlProfile->getTotalCount());
$this->smarty->assign('time', $this->sqlProfile->getTotalTime());
$this->smarty->assign('data', $this->sqlProfile->returnProfiling());
}
/**
* Renders HTML code for custom tab.
* @return string
......@@ -54,12 +60,4 @@ final class DBProfiler implements Tracy\IBarPanel
return $this->smarty->fetch(dirname(__DIR__) . '/templates/database/tab.tpl');
}
private function assignSmarty(): void
{
$this->smarty->assign('count', $this->sqlProfile->getTotalCount());
$this->smarty->assign('time', $this->sqlProfile->getTotalTime());
$this->smarty->assign('data', $this->sqlProfile->returnProfiling());
$this->smarty->assign('profile', $this->sqlProfile->shouldProfile());
}
}
......@@ -5,8 +5,8 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\Dumper;
use Praguebest\Tools\OnOffSwitcher;
use Praguebest\Tools\SmartyTrace\InternalAssign;
use Praguebest\Tools\OnOffChecker;
use Praguebest\Tools\SmartyTrace\InternalTrace;
use Praguebest\Tools\TraceableSmarty;
use Smarty;
use SmartyException;
......@@ -46,24 +46,15 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
return $this->smarty->fetch(dirname(__DIR__) . '/templates/internalSmarty/smartyVars.tpl');
}
/**
* Renders HTML code for custom tab.
* @return string
* @throws SmartyException
*/
public function getTab(): string
{
$this->assignSmarty();
return $this->smarty->fetch(dirname(__DIR__) . '/templates/internalSmarty/tab.tpl');
}
private function assignSmarty(): void
{
$variables = $this->currentSmarty->getAssigns();
foreach ($variables as $name => $assigns) {
foreach ($assigns as $assign) {
assert($assign instanceof InternalAssign);
assert($assign instanceof InternalTrace);
if ($assign->processed) {
continue;
}
$assign->backtraces = array_map(
static function ($backtrace) {
return urldecode(Helpers::editorLink($backtrace));
......@@ -73,10 +64,23 @@ final class InternalSmartyBarPanel implements Tracy\IBarPanel
$assign->first = array_shift($assign->backtraces);
$assign->value = Dumper::toHtml($assign->value);
$assign->processed = true;
}
}
$this->smarty->assign('internalVariables', $variables);
$this->smarty->assign('countInternalVariables', count($variables));
$this->smarty->assign('profileInternalVariables', OnOffSwitcher::shouldProfile(TraceableSmarty::INTERNAL_VARIABLES));
$this->smarty->assign('profileInternalVariables', OnOffChecker::getCurrentSetting(TraceableSmarty::INTERNAL_VARIABLES));
}
/**
* Renders HTML code for custom tab.
* @return string
* @throws SmartyException
*/
public function getTab(): string
{
$this->assignSmarty();
return $this->smarty->fetch(dirname(__DIR__) . '/templates/internalSmarty/tab.tpl');
}
}
......@@ -4,9 +4,11 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use ArrayObject;
use Nette\Utils\FileSystem;
use Nette\Utils\Strings;
use Praguebest\Tools;
use Praguebest\Tools\DBProfiler;
use Praguebest\Tools\WebSwitcherUrls;
use Smarty;
use Tracy\Debugger;
......@@ -20,12 +22,24 @@ final class Registrator
private $smarty;
/** @var IBarPanel[] */
private $registeredPanels = [];
/**
* @var ArrayObject
*/
private $settings;
private function __construct(string $compileDir, string $cacheDir)
{
$this->smarty = new Smarty();
$this->smarty->setCacheDir($cacheDir);
$this->smarty->setCompileDir($compileDir);
$this->settings = new ArrayObject();
$settingsBarPanel = new SettingsBarPanel($this->smarty, $this->settings);
$this->registeredPanels['settings'] = $settingsBarPanel;
Debugger::getBar()->addPanel($settingsBarPanel);
}
public static function getInstance(string $compileDir, string $cacheDir): Registrator
......@@ -37,11 +51,19 @@ final class Registrator
return self::$instance;
}
/**
* @param string $errorDir
* @param string $emailDeveloper
* @return $this
*/
public function registerDebugger(string $errorDir, string $emailDeveloper): Registrator
{
FileSystem::createDir($errorDir);
Debugger::$strictMode = true;
$currentSetting = new Tools\Setting('Debugger::$strictMode', 'Tracy strict mode', true);
$this->settings->append($currentSetting);
Debugger::$strictMode = $currentSetting->isOn();
Debugger::$maxLength = 1024;
Debugger::$maxDepth = 5;
Debugger::enable('42@84.42.168.182,42@185.156.123.46', realpath($errorDir), Strings::replace($emailDeveloper, '/,;/', ','));
......@@ -65,13 +87,28 @@ final class Registrator
$this->addPanel(
__METHOD__,
function () {
return new DBProfiler(Tools\DBProfiler::getInstance(), $this->smarty);
return new DBProfileBar(Tools\DBProfiler::getInstance(), $this->smarty);
}
);
$currentSetting = new Tools\Setting(DBProfiler::PROFILE_DB, 'Profile database');
$this->settings->append($currentSetting);
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 registerWebswitcher(WebSwitcherUrls $urls): Registrator
{
$this->addPanel(
......@@ -93,6 +130,10 @@ final class Registrator
}
);
$currentSetting = new Tools\Setting(Tools\TraceableSmarty::SMARTY_VARIABLES, 'List Smarty variables');
$this->settings->append($currentSetting);
return $this;
}
......@@ -105,18 +146,11 @@ final class Registrator
}
);
return $this;
}
$currentSetting = new Tools\Setting(Tools\TraceableSmarty::INTERNAL_VARIABLES, 'List internal Smarty variables');
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;
$this->settings->append($currentSetting);
Debugger::getBar()->addPanel($panel);
}
return $this;
}
}
<?php
declare(strict_types=1);
namespace Praguebest\BarPanel;
use ArrayObject;
use Praguebest\Tools\OnOffChecker;
use Smarty;
use SmartyException;
use Tracy;
final class SettingsBarPanel implements Tracy\IBarPanel
{
public $data;
public $id = 'settings';
/**
* @var Smarty
*/
private $smarty;
/**
* @var OnOffChecker[]
*/
private $switchers;
/**
* SettingsBarPanel constructor.
* @param Smarty $smarty
* @param ArrayObject $switchers
*/
public function __construct(Smarty $smarty, ArrayObject $switchers)
{
$this->smarty = $smarty;
$this->switchers = $switchers;
}
/**
* Renders HTML code for custom panel.
* @return string
* @throws SmartyException
*/
public function getPanel(): string
{
$this->smarty->assign('switchers', $this->switchers);
return $this->smarty->fetch(dirname(__DIR__) . '/templates/settings/settings.tpl');
}
/**
* Renders HTML code for custom tab.
* @return string
* @throws SmartyException
*/
public function getTab(): string
{
return $this->smarty->fetch(dirname(__DIR__) . '/templates/settings/tab.tpl');
}
}
......@@ -5,7 +5,7 @@ declare(strict_types=1);
namespace Praguebest\BarPanel;
use Praguebest\Tools\Dumper;
use Praguebest\Tools\OnOffSwitcher;
use Praguebest\Tools\OnOffChecker;
use Praguebest\Tools\TraceableSmarty;
use Smarty;
use SmartyException;
......@@ -44,23 +44,11 @@ final class SmartyBarPanel implements Tracy\IBarPanel
return $this->smarty->fetch(dirname(__DIR__) . '/templates/smarty/smartyVars.tpl');
}
/**
* Renders HTML code for custom tab.
* @return string
* @throws SmartyException
*/
public function getTab(): string
{
$this->assignSmarty();
return $this->smarty->fetch(dirname(__DIR__) . '/templates/smarty/tab.tpl');
}
private function assignSmarty(): void
{
$smartyBacktrace = [];
$smartyVars = [];
if (OnOffSwitcher::shouldProfile(TraceableSmarty::SMARTY_VARIABLES)) {
if (OnOffChecker::getCurrentSetting(TraceableSmarty::SMARTY_VARIABLES)) {
foreach ($this->currentSmarty->getBacktraces() as $key => $backtrace) {
$smartyBacktrace[$key] = Dumper::getBacktrace($backtrace);
}
......@@ -78,6 +66,18 @@ final class SmartyBarPanel implements Tracy\IBarPanel
}
$this->smarty->assign('smartyVariables', $smartyVars);
$this->smarty->assign('countSmartyVariables', count($smartyVars));
$this->smarty->assign('profileSmartyVariables', OnOffSwitcher::shouldProfile(TraceableSmarty::SMARTY_VARIABLES));
$this->smarty->assign('profileSmartyVariables', OnOffChecker::getCurrentSetting(TraceableSmarty::SMARTY_VARIABLES));
}
/**
* Renders HTML code for custom tab.
* @return string
* @throws SmartyException
*/
public function getTab(): string
{
$this->assignSmarty();
return $this->smarty->fetch(dirname(__DIR__) . '/templates/smarty/tab.tpl');
}
}
<?php
/** @noinspection GlobalVariableUsageInspection */
declare(strict_types=1);
namespace Praguebest\Tools;
......@@ -7,7 +9,7 @@ use PDOStatement;
final class DBProfiler
{
private const PROFILE_DB = 'profileDB';
public const PROFILE_DB = 'profileDB';
/** @var DBProfiler */
private static $instance;
/** @var PublisherQueryFormatter */
......@@ -33,7 +35,7 @@ final class DBProfiler
private function initProfiling(): void
{
$this->profile = OnOffSwitcher::shouldProfile(DBProfiler::PROFILE_DB);
$this->profile = OnOffChecker::getCurrentSetting(self::PROFILE_DB);
}
public static function getInstance(): self
......
......@@ -20,8 +20,8 @@ final class Dumper
self::class,
DBProfiler::class,
];
private static $maxDepth;
private static $maxLength;
private static $maxDepth = 5;
private static $maxLength = 250;
public static function getBacktrace(array $backtraces = null): array
{
......
<?php
declare(strict_types=1);
namespace Praguebest\Tools;
use Nette\Utils\Strings;
final class OnOffChecker
{
public const PREFIX = 'OnOffSetting-';
public static function getCurrentSetting(string $settingCode, bool $default = false): bool
{
$settingCode = self::sanitizeCode($settingCode);
if (session_status() === PHP_SESSION_NONE && !headers_sent()) {
session_start();
}
$get = filter_input(INPUT_POST, $settingCode, FILTER_VALIDATE_BOOLEAN);
$fromSession = $_SESSION[self::PREFIX . $settingCode] ?? null;
if ($get === null && $fromSession === null) {
return $default;
}
return $get === true || ($get === null && $fromSession === true);
}
/**
* @param string $settingCode
* @return string
*/
public static function sanitizeCode(string $settingCode): string
{
return Strings::webalize($settingCode);
}
}
<?php
declare(strict_types=1);
namespace Praguebest\Tools;
final class OnOffSwitcher
{
private const PREFIX = 'OnOffSwitcher-';
public static function shouldProfile(string $profileName): bool
{
if (session_status() === PHP_SESSION_NONE && !headers_sent()) {
session_start();
}
$profile = false;
$get = filter_input(INPUT_GET, $profileName, FILTER_VALIDATE_BOOLEAN);
if ($get === true || ($get === null && static::getFromSession($profileName) === true)) {
static::setToSession($profileName, true);
$profile = true;
}
if ($get === false || ($get === null && static::getFromSession($profileName) === false)) {
static::setToSession($profileName, false);
$profile = false;
}
return $profile;
}
private static function getFromSession(string $profileName): bool
{
return (bool)($_SESSION[self::PREFIX . $profileName] ?? null);
}
private static function setToSession(string $profileName, bool $value): void
{