From e448fadf98c288edc3cb990413539d0aa505dab3 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Mon, 15 Jun 2015 14:04:42 +0200 Subject: [PATCH] [VarDumper] Fix dump output for better readability --- .../Component/VarDumper/Dumper/CliDumper.php | 87 ++++++++++++++----- .../Component/VarDumper/Dumper/HtmlDumper.php | 40 ++++++--- .../VarDumper/Tests/CliDumperTest.php | 30 +++---- .../VarDumper/Tests/Fixtures/dumb-var.php | 12 +-- .../VarDumper/Tests/HtmlDumperTest.php | 24 ++--- 5 files changed, 126 insertions(+), 67 deletions(-) diff --git a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php index ece7b38abd..1fcad90c87 100644 --- a/src/Symfony/Component/VarDumper/Dumper/CliDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/CliDumper.php @@ -31,7 +31,6 @@ class CliDumper extends AbstractDumper 'num' => '1;38;5;38', 'const' => '1;38;5;208', 'str' => '1;38;5;113', - 'cchr' => '7', 'note' => '38;5;38', 'ref' => '38;5;247', 'public' => '', @@ -42,7 +41,15 @@ class CliDumper extends AbstractDumper 'index' => '38;5;38', ); - protected static $controlCharsRx = '/[\x00-\x1F\x7F]/'; + protected static $controlCharsRx = '/[\x00-\x1F\x7F]+/'; + protected static $controlCharsMap = array( + "\t" => '\t', + "\n" => '\n', + "\v" => '\v', + "\f" => '\f', + "\r" => '\r', + "\033" => '\e', + ); /** * {@inheritdoc} @@ -146,7 +153,7 @@ class CliDumper extends AbstractDumper $this->line .= $this->style($style, $value, $attr); - $this->dumpLine($cursor->depth); + $this->dumpLine($cursor->depth, true); } /** @@ -161,13 +168,17 @@ class CliDumper extends AbstractDumper } if ('' === $str) { $this->line .= '""'; - $this->dumpLine($cursor->depth); + $this->dumpLine($cursor->depth, true); } else { $attr = array( - 'length' => function_exists('iconv_strlen') && 0 <= $cut ? iconv_strlen($str, 'UTF-8') + $cut : 0, + 'length' => 0 <= $cut && function_exists('iconv_strlen') ? iconv_strlen($str, 'UTF-8') + $cut : 0, 'binary' => $bin, ); $str = explode("\n", $str); + if (isset($str[1]) && !isset($str[2]) && !isset($str[1][0])) { + unset($str[1]); + $str[0] .= "\n"; + } $m = count($str) - 1; $i = $lineCut = 0; @@ -183,20 +194,30 @@ class CliDumper extends AbstractDumper } foreach ($str as $str) { + if ($i < $m) { + $str .= "\n"; + } if (0 < $this->maxStringWidth && $this->maxStringWidth < $len = iconv_strlen($str, 'UTF-8')) { $str = iconv_substr($str, 0, $this->maxStringWidth, 'UTF-8'); $lineCut = $len - $this->maxStringWidth; } - - if ($m) { + if ($m && 0 < $cursor->depth) { $this->line .= $this->indentPad; } - $this->line .= $this->style('str', $str, $attr); - + if ('' !== $str) { + $this->line .= $this->style('str', $str, $attr); + } if ($i++ == $m) { - $this->line .= '"'; if ($m) { - $this->line .= '""'; + if ('' !== $str) { + $this->dumpLine($cursor->depth); + if (0 < $cursor->depth) { + $this->line .= $this->indentPad; + } + } + $this->line .= '"""'; + } else { + $this->line .= '"'; } if ($cut < 0) { $this->line .= '…'; @@ -210,7 +231,7 @@ class CliDumper extends AbstractDumper $lineCut = 0; } - $this->dumpLine($cursor->depth); + $this->dumpLine($cursor->depth, $i > $m); } } } @@ -228,7 +249,7 @@ class CliDumper extends AbstractDumper if (Cursor::HASH_OBJECT === $type) { $prefix = 'stdClass' !== $class ? $this->style('note', $class).' {' : '{'; } elseif (Cursor::HASH_RESOURCE === $type) { - $prefix = $this->style('note', ':'.$class).' {'; + $prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' '); } else { $prefix = $class ? $this->style('note', 'array:'.$class).' [' : '['; } @@ -237,6 +258,8 @@ class CliDumper extends AbstractDumper $prefix .= $this->style('ref', (Cursor::HASH_RESOURCE === $type ? '@' : '#').(0 < $cursor->softRefHandle ? $cursor->softRefHandle : $cursor->softRefTo), array('count' => $cursor->softRefCount)); } elseif ($cursor->hardRefTo && !$cursor->refIndex && $class) { $prefix .= $this->style('ref', '&'.$cursor->hardRefTo, array('count' => $cursor->hardRefCount)); + } elseif (!$hasChild && Cursor::HASH_RESOURCE === $type) { + $prefix = substr($prefix, 0, -1); } $this->line .= $prefix; @@ -252,8 +275,8 @@ class CliDumper extends AbstractDumper public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut) { $this->dumpEllipsis($cursor, $hasChild, $cut); - $this->line .= Cursor::HASH_OBJECT === $type || Cursor::HASH_RESOURCE === $type ? '}' : ']'; - $this->dumpLine($cursor->depth); + $this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : '')); + $this->dumpLine($cursor->depth, true); } /** @@ -360,12 +383,34 @@ class CliDumper extends AbstractDumper } $style = $this->styles[$style]; - $cchr = $this->colors ? "\033[m\033[{$style};{$this->styles['cchr']}m%s\033[m\033[{$style}m" : '%s'; - $value = preg_replace_callback(self::$controlCharsRx, function ($r) use ($cchr) { - return sprintf($cchr, "\x7F" === $r[0] ? '?' : chr(64 + ord($r[0]))); - }, $value); - return $this->colors ? sprintf("\033[%sm%s\033[m\033[%sm", $style, $value, $this->styles['default']) : $value; + $map = static::$controlCharsMap; + $startCchr = $this->colors ? "\033[m\033[{$this->styles['default']}m" : ''; + $endCchr = $this->colors ? "\033[m\033[{$style}m" : ''; + $value = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $startCchr, $endCchr) { + $s = $startCchr; + $c = $c[$i = 0]; + do { + $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i])); + } while (isset($c[++$i])); + + return $s.$endCchr; + }, $value, -1, $cchrCount); + + if ($this->colors) { + if ($cchrCount && "\033" === $value[0]) { + $value = substr($value, strlen($startCchr)); + } else { + $value = "\033[{$style}m".$value; + } + if ($cchrCount && $endCchr === substr($value, -strlen($endCchr))) { + $value = substr($value, 0, -strlen($endCchr)); + } else { + $value .= "\033[{$this->styles['default']}m"; + } + } + + return $value; } /** @@ -418,7 +463,7 @@ class CliDumper extends AbstractDumper /** * {@inheritdoc} */ - protected function dumpLine($depth) + protected function dumpLine($depth, $endOfValue = false) { if ($this->colors) { $this->line = sprintf("\033[%sm%s\033[m", $this->styles['default'], $this->line); diff --git a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php index 496a1520e0..2f312c0807 100644 --- a/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php +++ b/src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php @@ -35,7 +35,6 @@ class HtmlDumper extends CliDumper 'num' => 'font-weight:bold; color:#1299DA', 'const' => 'font-weight:bold', 'str' => 'font-weight:bold; color:#56DB3A', - 'cchr' => 'color:#FF8400', 'note' => 'color:#1299DA', 'ref' => 'color:#A0A0A0', 'public' => 'color:#FFFFFF', @@ -329,10 +328,6 @@ EOHTML; } $v = htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); - $v = preg_replace_callback(self::$controlCharsRx, function ($r) { - // Use Unicode Control Pictures - see http://www.unicode.org/charts/PDF/U2400.pdf - return sprintf('&#%d;', ord($r[0]), "\x7F" !== $r[0] ? 0x2400 + ord($r[0]) : 0x2421); - }, $v); if ('ref' === $style) { if (empty($attr['count'])) { @@ -349,25 +344,44 @@ EOHTML; $style .= sprintf(' title="%s"', empty($attr['dynamic']) ? 'Public property' : 'Runtime added dynamic property'); } elseif ('str' === $style && 1 < $attr['length']) { $style .= sprintf(' title="%s%s characters"', $attr['length'], $attr['binary'] ? ' binary or non-UTF-8' : ''); - } elseif ('note' === $style) { - if (false !== $c = strrpos($v, '\\')) { - return sprintf('%s', $v, $style, substr($v, $c + 1)); - } elseif (':' === $v[0]) { - return sprintf('%s', substr($v, 1), $style, $v); - } + } elseif ('note' === $style && false !== $c = strrpos($v, '\\')) { + return sprintf('%s', $v, $style, substr($v, $c + 1)); } elseif ('protected' === $style) { $style .= ' title="Protected property"'; } elseif ('private' === $style) { $style .= sprintf(' title="Private property defined in class: `%s`"', $attr['class']); } - return "$v"; + $map = static::$controlCharsMap; + $style = ""; + $v = preg_replace_callback(static::$controlCharsRx, function ($c) use ($map, $style) { + $s = ''; + $c = $c[$i = 0]; + do { + $s .= isset($map[$c[$i]]) ? $map[$c[$i]] : sprintf('\x%02X', ord($c[$i])); + } while (isset($c[++$i])); + + return $s.$style; + }, $v, -1, $cchrCount); + + if ($cchrCount && '<' === $v[0]) { + $v = substr($v, 7); + } else { + $v = $style.$v; + } + if ($cchrCount && '>' === substr($v, -1)) { + $v = substr($v, 0, -strlen($style)); + } else { + $v .= ''; + } + + return $v; } /** * {@inheritdoc} */ - protected function dumpLine($depth) + protected function dumpLine($depth, $endOfValue = false) { if (-1 === $this->lastDepth) { $this->line = sprintf($this->dumpPrefix, $this->dumpId, $this->indentPad).$this->line; diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index 62d2a99612..2b860fdc97 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -55,10 +55,10 @@ array:25 [ 4 => INF 5 => -INF 6 => {$intMax} - "str" => "déjà" - 7 => b"é@" + "str" => "déjà\\n" + 7 => b"é\\x00" "[]" => [] - "res" => :stream {@{$res1} + "res" => stream resource {@{$res1} wrapper_type: "plainfile" stream_type: "STDIO" mode: "r" @@ -69,21 +69,21 @@ array:25 [ eof: false options: [] } - 8 => :Unknown {@{$res2}} + 8 => Unknown resource @{$res2} "obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d +foo: "foo" +"bar": "bar" } "closure" => Closure {#%d reflection: """ - Closure [ %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] { - @@ {$var['file']} {$var['line']} - {$var['line']} - - - Parameters [2] { - Parameter #0 [ \$a ] - Parameter #1 [ PDO or NULL &\$b = NULL ] - } - } + Closure [ %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {\\n + @@ {$var['file']} {$var['line']} - {$var['line']}\\n + \\n + - Parameters [2] {\\n + Parameter #0 [ \$a ]\\n + Parameter #1 [ PDO or NULL &\$b = NULL ]\\n + }\\n + }\\n """ } "line" => {$var['line']} @@ -130,7 +130,7 @@ EOTXT $this->assertStringMatchesFormat( << 'foo'); - $var->bar =& $var->foo; + $var->bar = &$var->foo; $dumper = new CliDumper(); $dumper->setColors(false); @@ -325,7 +325,7 @@ EOTXT $var = function &() { $var = array(); - $var[] =& $var; + $var[] = &$var; return $var; }; diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php index 380369a3c8..2cd707d507 100644 --- a/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/dumb-var.php @@ -19,7 +19,7 @@ fclose($h); $var = array( 'number' => 1, null, 'const' => 1.1, true, false, NAN, INF, -INF, PHP_INT_MAX, - 'str' => "déjà", "\xE9\x00", + 'str' => "déjà\n", "\xE9\x00", '[]' => array(), 'res' => $g, $h, @@ -30,14 +30,14 @@ $var = array( ); $r = array(); -$r[] =& $r; +$r[] = &$r; -$var['recurs'] =& $r; -$var[] =& $var[0]; +$var['recurs'] = &$r; +$var[] = &$var[0]; $var['sobj'] = $var['obj']; -$var['snobj'] =& $var['nobj'][0]; +$var['snobj'] = &$var['nobj'][0]; $var['snobj2'] = $var['nobj'][0]; $var['file'] = __FILE__; -$var["bin-key-\xE9"] = ""; +$var["bin-key-\xE9"] = ''; unset($g, $h, $r); diff --git a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php index 0cc9d106b8..b0e21ffe31 100644 --- a/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php @@ -59,10 +59,10 @@ class HtmlDumperTest extends \PHPUnit_Framework_TestCase 4 => INF 5 => -INF 6 => {$intMax} - "str" => "déjà" - 7 => b"é" + "str" => "déjà\\n" + 7 => b"é\\x00" "[]" => [] - "res" => :stream {@{$res1} + "res" => stream resource @{$res1} wrapper_type: "plainfile" stream_type: "STDIO" mode: "r" @@ -73,21 +73,21 @@ class HtmlDumperTest extends \PHPUnit_Framework_TestCase eof: false options: [] } - 8 => :Unknown {@{$res2}} + 8 => Unknown resource @{$res2} "obj" => DumbFoo {#%d +foo: "foo" +"bar": "bar" } "closure" => Closure {#%d reflection: """ - Closure [ <user%S> %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] { - @@ {$var['file']} {$var['line']} - {$var['line']} - - - Parameters [2] { - Parameter #0 [ <required> \$a ] - Parameter #1 [ <optional> PDO or NULL &\$b = NULL ] - } - } + Closure [ <user%S> %s Symfony\Component\VarDumper\Tests\Fixture\{closure} ] {\\n + @@ {$var['file']} {$var['line']} - {$var['line']}\\n + \\n + - Parameters [2] {\\n + Parameter #0 [ <required> \$a ]\\n + Parameter #1 [ <optional> PDO or NULL &\$b = NULL ]\\n + }\\n + }\\n """ } "line" => {$var['line']}