Skip to content

Commit

Permalink
[DebugBundle] enhance dump excerpts
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Sep 23, 2014
1 parent 49f13c6 commit de05cd9
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public function dump(Data $data)

for ($i = 1; $i < 7; ++$i) {
if (isset($trace[$i]['class'], $trace[$i]['function'])
&& ('dump' === $trace[$i]['function'] || 'debug' === $trace[$i]['function'])
&& 'dump' === $trace[$i]['function']
&& 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
) {
$file = $trace[$i]['file'];
Expand Down Expand Up @@ -152,66 +152,29 @@ public function getDumpsCount()
return $this->dataCount;
}

public function getDumpsExcerpts()
public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
{
$dumps = array();

foreach ($this->data as $dump) {
$data = $dump['data']->getRawData();
unset($dump['data']);

$data = $data[0][0];

if (isset($data->val)) {
$data = $data->val;
}

if (isset($data->bin)) {
$data = 'b"'.$data->bin.'"';
} elseif (isset($data->str)) {
$data = '"'.$data->str.'"';
} elseif (isset($data->count)) {
$data = 'array('.$data->count.')';
} elseif (isset($data->class)) {
$data = $data->class.'{...}';
} elseif (isset($data->res)) {
$data = 'resource:'.$data->res.'{...}';
} elseif (is_array($data)) {
$data = 'array()';
} elseif (null === $data) {
$data = 'null';
} elseif (false === $data) {
$data = 'false';
} elseif (INF === $data) {
$data = 'INF';
} elseif (-INF === $data) {
$data = '-INF';
} elseif (NAN === $data) {
$data = 'NAN';
} elseif (true === $data) {
$data = 'true';
}

$dump['dataExcerpt'] = $data;
$dumps[] = $dump;
}

return $dumps;
}

public function getDumps($getData = false)
{
if ($getData) {
if ('html' === $format) {
$dumper = new HtmlDumper();
} elseif ('json' === $format) {
$dumper = new JsonDumper();
} else {
throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));

}
$dumps = array();

foreach ($this->data as $dump) {
$json = '';
if ($getData) {
$dumper->dump($dump['data'], function ($line) use (&$json) {$json .= $line;});
}
$dump['data'] = $json;
$data = '';
$dumper->dump(
$dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth),
function ($line, $depth) use (&$data) {
if (false !==$depth) {
$data .= str_repeat(' ', $depth).$line."\n";
}
}
);
$dump['data'] = $data;
$dumps[] = $dump;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,17 @@ public function testDump()

$xDump = array(
array(
'data' => '',
'data' => "<!DOCTYPE html><style> pre.sf-dump { background-color: #300a24; white-space: pre; line-height: 1.2em; color: #eee8d5; font-family: monospace, sans-serif; padding: 5px; } .sf-dump span { display: inline; }a.sf-dump-ref {color:#444444}span.sf-dump-num {font-weight:bold;color:#0087FF}span.sf-dump-const {font-weight:bold;color:#0087FF}span.sf-dump-str {font-weight:bold;color:#00D7FF}span.sf-dump-cchr {font-style: italic}span.sf-dump-note {color:#D7AF00}span.sf-dump-ref {color:#444444}span.sf-dump-public {color:#008700}span.sf-dump-protected {color:#D75F00}span.sf-dump-private {color:#D70000}span.sf-dump-meta {color:#005FFF}</style><pre class=sf-dump><span class=sf-dump-0><span class=sf-dump-num>123</span>\n</pre>\n",
'name' => 'DumpDataCollectorTest.php',
'file' => __FILE__,
'line' => $line,
'fileExcerpt' => false,
),
);

$this->assertSame($xDump, $collector->getDumps());

$xDump[0]['data'] = '123';
$this->assertSame($xDump, $collector->getDumps(true));
$this->assertSame($xDump, $collector->getDumps('html'));

$this->assertStringStartsWith(
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":1:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}}s:4:"name";s:25:"DumpDataCollectorTest.php";',
'a:1:{i:0;a:5:{s:4:"data";O:39:"Symfony\Component\VarDumper\Cloner\Data":3:{s:45:"Symfony\Component\VarDumper\Cloner\Datadata";a:1:{i:0;a:1:{i:0;i:123;}}s:49:"Symfony\Component\VarDumper\Cloner\DatamaxDepth";i:-1;s:57:"Symfony\Component\VarDumper\Cloner\DatamaxItemsPerDepth";i:-1;}s:4:"name";s:25:"DumpDataCollectorTest.php";s:4:"file";s:',
str_replace("\0", '', $collector->serialize())
);

Expand Down
51 changes: 38 additions & 13 deletions src/Symfony/Component/VarDumper/Cloner/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
class Data
{
private $data;
private $maxDepth = -1;
private $maxItemsPerDepth = -1;

/**
* @param array $data A array as returned by ClonerInterface::cloneVar().
Expand All @@ -29,11 +31,31 @@ public function __construct(array $data)
$this->data = $data;
}

/**
* @return array The raw data structure.
*/
public function getRawData()
{
return $this->data;
}

/**
* Returns a depth limited clone of $this.
*
* @param int $maxDepth The max dumped depth level.
* @param int $maxItemsPerDepth The max number of items dumped per depth level.
*
* @return self A depth limited clone of $this.
*/
public function getLimitedClone($maxDepth, $maxItemsPerDepth)
{
$data = clone $this;
$data->maxDepth = (int) $maxDepth;
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;

return $data;
}

/**
* Dumps data with a DumperInternalsInterface dumper.
*/
Expand Down Expand Up @@ -147,20 +169,23 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
{
if ($children) {
$cursor = clone $parentCursor;
++$cursor->depth;
$cursor->hashType = $hashType;
$cursor->hashIndex = 0;
$cursor->hashLength = count($children);
$cursor->hashCut = $hashCut;
foreach ($children as $cursor->hashKey => $child) {
$this->dumpItem($dumper, $cursor, $refs, $child);
++$cursor->hashIndex;
if ($cursor->stop) {
$parentCursor->stop = true;

return $hashCut >= 0 ? $hashCut + $children - $cursor->hashIndex : $hashCut;
if ($parentCursor->depth !== $this->maxDepth && $this->maxItemsPerDepth) {
$cursor = clone $parentCursor;
++$cursor->depth;
$cursor->hashType = $hashType;
$cursor->hashIndex = 0;
$cursor->hashLength = count($children);
$cursor->hashCut = $hashCut;
foreach ($children as $cursor->hashKey => $child) {
$this->dumpItem($dumper, $cursor, $refs, $child);
if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
$parentCursor->stop = true;

return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
}
}
} elseif ($hashCut >= 0) {
$hashCut += count($children);
}
}

Expand Down
42 changes: 23 additions & 19 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class HtmlDumper extends CliDumper
public static $defaultOutputStream = 'php://output';

protected $dumpHeader;
protected $dumpPrefix = '<pre class=sf-dump style=white-space:pre>';
protected $dumpPrefix = '<pre class=sf-dump>';
protected $dumpSuffix = '</pre>';
protected $colors = true;
protected $headerIsDumped = false;
Expand Down Expand Up @@ -59,7 +59,7 @@ public function setStyles(array $styles)
}

/**
* Sets an HTML header the will be dumped once in the output stream.
* Sets an HTML header that will be dumped once in the output stream.
*
* @param string $header An HTML string.
*/
Expand All @@ -83,28 +83,32 @@ public function setDumpBoudaries($prefix, $suffix)
/**
* Dumps the HTML header.
*/
protected function dumpHeader()
protected function getDumpHeader()
{
$this->headerIsDumped = true;
$line = $this->line;

$p = 'sf-dump';
$this->line = '<!DOCTYPE html><style>';
parent::dumpLine(0);
$this->line .= "a.$p-ref {{$this->styles['ref']}}";
parent::dumpLine(0);
$line = <<<EOHTML
<!DOCTYPE html><style>
pre.sf-dump {
background-color: #300a24;
white-space: pre;
line-height: 1.2em;
color: #eee8d5;
font-family: monospace, sans-serif;
padding: 5px;
}
.sf-dump span {
display: inline;
}
EOHTML;
$line .= "a.$p-ref {{$this->styles['ref']}}";

foreach ($this->styles as $class => $style) {
$this->line .= "span.$p-$class {{$style}}";
parent::dumpLine(0);
$line .= "span.$p-$class {{$style}}";
}

$this->line .= '</style>';
parent::dumpLine(0);
$this->line .= $this->dumpHeader;
parent::dumpLine(0);

$this->line = $line;
return preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
}

/**
Expand Down Expand Up @@ -144,9 +148,6 @@ protected function style($style, $val)
*/
protected function dumpLine($depth)
{
if (!$this->headerIsDumped) {
$this->dumpHeader();
}

switch ($this->lastDepth - $depth) {
case +1: $this->line = '</span>'.$this->line; break;
Expand All @@ -156,6 +157,9 @@ protected function dumpLine($depth)
if (-1 === $this->lastDepth) {
$this->line = $this->dumpPrefix.$this->line;
}
if (!$this->headerIsDumped) {
$this->line = $this->getDumpHeader().$this->line;
}

if (false === $depth) {
$this->lastDepth = -1;
Expand Down
16 changes: 1 addition & 15 deletions src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,7 @@ public function testGet()

$this->assertSame(
<<<EOTXT
<!DOCTYPE html><style>
a.sf-dump-ref {color:#444444}
span.sf-dump-num {font-weight:bold;color:#0087FF}
span.sf-dump-const {font-weight:bold;color:#0087FF}
span.sf-dump-str {font-weight:bold;color:#00D7FF}
span.sf-dump-cchr {font-style: italic}
span.sf-dump-note {color:#D7AF00}
span.sf-dump-ref {color:#444444}
span.sf-dump-public {color:#008700}
span.sf-dump-protected {color:#D75F00}
span.sf-dump-private {color:#D70000}
span.sf-dump-meta {color:#005FFF}
</style>
<pre class=sf-dump style=white-space:pre><span class=sf-dump-0><span class=sf-dump-note>array:25</span> [
<!DOCTYPE html><style> pre.sf-dump { background-color: #300a24; white-space: pre; line-height: 1.2em; color: #eee8d5; font-family: monospace, sans-serif; padding: 5px; } .sf-dump span { display: inline; }a.sf-dump-ref {color:#444444}span.sf-dump-num {font-weight:bold;color:#0087FF}span.sf-dump-const {font-weight:bold;color:#0087FF}span.sf-dump-str {font-weight:bold;color:#00D7FF}span.sf-dump-cchr {font-style: italic}span.sf-dump-note {color:#D7AF00}span.sf-dump-ref {color:#444444}span.sf-dump-public {color:#008700}span.sf-dump-protected {color:#D75F00}span.sf-dump-private {color:#D70000}span.sf-dump-meta {color:#005FFF}</style><pre class=sf-dump><span class=sf-dump-0><span class=sf-dump-note>array:25</span> [
<span class=sf-dump-1>"<span class=sf-dump-meta>number</span>" => <span class=sf-dump-num>1</span>
<span class=sf-dump-meta>0</span> => <span class=sf-dump-const>null</span> <a class=sf-dump-ref name="sf-dump-ref1">#1</a>
"<span class=sf-dump-meta>const</span>" => <span class=sf-dump-num>1.1</span>
Expand Down

0 comments on commit de05cd9

Please sign in to comment.