Skip to content

Commit

Permalink
TracyExtension: allow to specify error level for scream and strictMod…
Browse files Browse the repository at this point in the history
…e, improve supported error level expressions
  • Loading branch information
xificurk authored and dg committed Dec 18, 2021
1 parent 5ea68b7 commit da6a350
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 11 deletions.
36 changes: 27 additions & 9 deletions src/Bridges/Nette/TracyExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
*/
class TracyExtension extends Nette\DI\CompilerExtension
{
private const ERROR_SEVERITY_PATTERN = 'E_(?:ALL|PARSE|STRICT|RECOVERABLE_ERROR|(?:CORE|COMPILE)_(?:ERROR|WARNING)|(?:USER_)?(?:ERROR|WARNING|NOTICE|DEPRECATED))';

/** @var bool */
private $debugMode;

Expand All @@ -35,22 +37,25 @@ public function __construct(bool $debugMode = false, bool $cliMode = false)

public function getConfigSchema(): Nette\Schema\Schema
{
$errorSeverity = Expect::string()->pattern(self::ERROR_SEVERITY_PATTERN);
$errorSeverityExpr = Expect::string()->pattern('(' . self::ERROR_SEVERITY_PATTERN . '|[ &|~()])+');

return Expect::structure([
'email' => Expect::anyOf(Expect::email(), Expect::listOf('email'))->dynamic(),
'fromEmail' => Expect::email()->dynamic(),
'emailSnooze' => Expect::string()->dynamic(),
'logSeverity' => Expect::anyOf(Expect::scalar(), Expect::listOf('scalar')),
'logSeverity' => Expect::anyOf(Expect::int(), $errorSeverityExpr, Expect::listOf($errorSeverity)),
'editor' => Expect::string()->dynamic(),
'browser' => Expect::string()->dynamic(),
'errorTemplate' => Expect::string()->dynamic(),
'strictMode' => Expect::bool()->dynamic(),
'strictMode' => Expect::anyOf(Expect::bool(), Expect::int(), $errorSeverityExpr, Expect::listOf($errorSeverity)),
'showBar' => Expect::bool()->dynamic(),
'maxLength' => Expect::int()->dynamic(),
'maxDepth' => Expect::int()->dynamic(),
'keysToHide' => Expect::array(null)->dynamic(),
'dumpTheme' => Expect::string()->dynamic(),
'showLocation' => Expect::bool()->dynamic(),
'scream' => Expect::bool()->dynamic(),
'scream' => Expect::anyOf(Expect::bool(), Expect::int(), $errorSeverityExpr, Expect::listOf($errorSeverity)),
'bar' => Expect::listOf('string|Nette\DI\Definitions\Statement'),
'blueScreen' => Expect::listOf('callable'),
'editorMapping' => Expect::arrayOf('string')->dynamic()->default(null),
Expand Down Expand Up @@ -84,13 +89,11 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)

$options = (array) $this->config;
unset($options['bar'], $options['blueScreen'], $options['netteMailer']);
if (isset($options['logSeverity'])) {
$res = 0;
foreach ((array) $options['logSeverity'] as $level) {
$res |= is_int($level) ? $level : constant($level);
}

$options['logSeverity'] = $res;
foreach (['logSeverity', 'strictMode', 'scream'] as $key) {
if (is_string($options[$key]) || is_array($options[$key])) {
$options[$key] = $this->parseErrorSeverity($options[$key]);
}
}

foreach ($options as $key => $value) {
Expand Down Expand Up @@ -160,4 +163,19 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
throw new Nette\InvalidStateException("Make directory '$dir' writable.");
}
}


/**
* @param string|string[] $value
*/
private function parseErrorSeverity($value): int
{
$value = implode('|', (array) $value);
$res = (int) @parse_ini_string('e = ' . $value)['e']; // @ may fail
if (!$res) {
throw new Nette\InvalidStateException("Syntax error in expression '$value'");
}

return $res;
}
}
8 changes: 6 additions & 2 deletions tests/Tracy.Bridges/TracyExtension.services.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ $compiler->setClassName('Container');
$compiler->addExtension('tracy', new TracyExtension);
$compiler->addConfig([
'tracy' => [
'logSeverity' => 'E_USER_WARNING',
'logSeverity' => E_USER_NOTICE,
'strictMode' => 'E_ALL & ~(E_STRICT|E_NOTICE)',
'scream' => ['E_DEPRECATED', 'E_USER_DEPRECATED'],
'keysToHide' => ['abc'],
],
'services' => [
Expand All @@ -46,6 +48,8 @@ Assert::same(Tracy\Debugger::getLogger(), $container->getService('tracy.logger')
Assert::same(Tracy\Debugger::getBlueScreen(), $container->getService('tracy.blueScreen'));
Assert::same(Tracy\Debugger::getBar(), $container->getService('tracy.bar'));

Assert::same(E_USER_WARNING, Tracy\Debugger::$logSeverity);
Assert::same(E_USER_NOTICE, Tracy\Debugger::$logSeverity);
Assert::same(E_ALL & ~(E_STRICT | E_NOTICE), Tracy\Debugger::$strictMode);
Assert::same(E_DEPRECATED | E_USER_DEPRECATED, Tracy\Debugger::$scream);
Assert::contains('password', Tracy\Debugger::getBlueScreen()->keysToHide);
Assert::contains('abc', Tracy\Debugger::getBlueScreen()->keysToHide);

0 comments on commit da6a350

Please sign in to comment.