Skip to content

Commit

Permalink
BlueScreen: keysToHide respected in title [Closes #381]
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Sep 13, 2019
1 parent 9f3b88f commit 76e3f5d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 20 deletions.
16 changes: 9 additions & 7 deletions src/Tracy/BlueScreen/BlueScreen.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,11 +240,11 @@ private function renderActions(\Throwable $ex): array
/**
* Returns syntax highlighted source code.
*/
public static function highlightFile(string $file, int $line, int $lines = 15, array $vars = []): ?string
public static function highlightFile(string $file, int $line, int $lines = 15, array $vars = [], array $keysToHide = []): ?string
{
$source = @file_get_contents($file); // @ file may not exist
if ($source) {
$source = static::highlightPhp($source, $line, $lines, $vars);
$source = static::highlightPhp($source, $line, $lines, $vars, $keysToHide);
if ($editor = Helpers::editorUri($file, $line)) {
$source = substr_replace($source, ' data-tracy-href="' . Helpers::escapeHtml($editor) . '"', 4, 0);
}
Expand All @@ -256,7 +256,7 @@ public static function highlightFile(string $file, int $line, int $lines = 15, a
/**
* Returns syntax highlighted source code.
*/
public static function highlightPhp(string $source, int $line, int $lines = 15, array $vars = []): string
public static function highlightPhp(string $source, int $line, int $lines = 15, array $vars = [], array $keysToHide = []): string
{
if (function_exists('ini_set')) {
ini_set('highlight.comment', '#998; font-style: italic');
Expand All @@ -273,11 +273,13 @@ public static function highlightPhp(string $source, int $line, int $lines = 15,
$out .= static::highlightLine($source, $line, $lines);

if ($vars) {
$out = preg_replace_callback('#">\$(\w+)(&nbsp;)?</span>#', function (array $m) use ($vars): string {
$out = preg_replace_callback('#">\$(\w+)(&nbsp;)?</span>#', function (array $m) use ($vars, $keysToHide): string {
$dump = Dumper::toHtml($vars[$m[1]], [
Dumper::DEPTH => 1,
Dumper::KEYS_TO_HIDE => $keysToHide,
]);
return array_key_exists($m[1], $vars)
? '" title="'
. str_replace('"', '&quot;', trim(strip_tags(Dumper::toHtml($vars[$m[1]], [Dumper::DEPTH => 1]))))
. $m[0]
? '" title="' . str_replace('"', '&quot;', trim(strip_tags($dump))) . $m[0]
: $m[0];
}, $out);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Tracy/BlueScreen/assets/content.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ $code = $exception->getCode() ? ' #' . $exception->getCode() : '';

<div class="<?= $collapsed ? 'tracy-collapsed ' : '' ?>inner">
<p><b>File:</b> <?= Helpers::editorLink($ex->getFile(), $ex->getLine()) ?></p>
<?php if (is_file($ex->getFile())): ?><?= self::highlightFile($ex->getFile(), $ex->getLine(), 15, $ex instanceof \ErrorException && isset($ex->context) ? $ex->context : []) ?><?php endif ?>
<?php if (is_file($ex->getFile())): ?><?= self::highlightFile($ex->getFile(), $ex->getLine(), 15, $ex instanceof \ErrorException && isset($ex->context) ? $ex->context : [], $this->keysToHide) ?><?php endif ?>
</div></div>


Expand Down
24 changes: 12 additions & 12 deletions tests/Tracy/Dumper.keysToHide.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@ $obj = (object) [

Assert::match('stdClass #%a%
a => 456
password => "*****" (5)
PASSWORD => "*****" (5)
Pin => "*****" (5)
password => ***** (string)
PASSWORD => ***** (string)
Pin => ***** (string)
inner => array (4)
| a => 123
| password => "*****" (5)
| PASSWORD => "*****" (5)
| Pin => "*****" (5)
| password => ***** (string)
| PASSWORD => ***** (string)
| Pin => ***** (string)
', Dumper::toText($obj, [Dumper::KEYS_TO_HIDE => ['password', 'PIN']]));


Expand All @@ -49,16 +49,16 @@ Assert::equal([
'hash' => Expect::match('%h%'),
'items' => [
['a', 456, 0],
['password', '*****', 0],
['PASSWORD', '*****', 0],
['Pin', '*****', 0],
['password', ['type' => '***** (string)'], 0],
['PASSWORD', ['type' => '***** (string)'], 0],
['Pin', ['type' => '***** (string)'], 0],
[
'inner',
[
['a', 123],
['password', '*****'],
['PASSWORD', '*****'],
['Pin', '*****'],
['password', ['type' => '***** (string)']],
['PASSWORD', ['type' => '***** (string)']],
['Pin', ['type' => '***** (string)']],
],
0,
],
Expand Down

0 comments on commit 76e3f5d

Please sign in to comment.