8000 [DebugBundle] enhance dump excerpts · symfony/symfony@de05cd9 · GitHub
[go: up one dir, main page]

Skip to content

Commit de05cd9

Browse files
[DebugBundle] enhance dump excerpts
1 parent 49f13c6 commit de05cd9

File tree

5 files changed

+83
-109
lines changed

5 files changed

+83
-109
lines changed

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 18 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ public function dump(Data $data)
7373

7474
for ($i = 1; $i < 7; ++$i) {
7575
if (isset($trace[$i]['class'], $trace[$i]['function'])
76-
&& ('dump' === $trace[$i]['function'] || 'debug' === $trace[$i]['function'])
76+
&& 'dump' === $trace[$i]['function']
7777
&& 'Symfony\Component\VarDumper\VarDumper' === $trace[$i]['class']
7878
) {
7979
$file = $trace[$i]['file'];
@@ -152,66 +152,29 @@ public function getDumpsCount()
152152
return $this->dataCount;
153153
}
154154

155-
public function getDumpsExcerpts()
155+
public function getDumps($format, $maxDepthLimit = -1, $maxItemsPerDepth = -1)
156156
{
157-
$dumps = array();
158-
159-
foreach ($this->data as $dump) {
160-
$data = $dump['data']->getRawData();
161-
unset($dump['data']);
162-
163-
$data = $data[0][0];
164-
165-
if (isset($data->val)) {
166-
$data = $data->val;
167-
}
168-
169-
if (isset($data->bin)) {
170-
$data = 'b"'.$data->bin.'"';
171-
} elseif (isset($data->str)) {
172-
$data = '"'.$data->str.'"';
173-
} elseif (isset($data->count)) {
174-
$data = 'array('.$data->count.')';
175-
} elseif (isset($data->class)) {
176-
$data = $data->class.'{...}';
177-
} elseif (isset($data->res)) {
178-
$data = 'resource:'.$data->res.'{...}';
179-
} elseif (is_array($data)) {
180-
$data = 'array()';
181-
} elseif (null === $data) {
182-
$data = 'null';
183-
} elseif (false === $data) {
184-
$data = 'false';
185-
} elseif (INF === $data) {
186-
$data = 'INF';
187-
} elseif (-INF === $data) {
188-
$data = '-INF';
189-
} elseif (NAN === $data) {
190-
$data = 'NAN';
191-
} elseif (true === $data) {
192-
$data = 'true';
193-
}
194-
195-
$dump['dataExcerpt'] = $data;
196-
$dumps[] = $dump;
197-
}
198-
199-
return $dumps;
200-
}
201-
202-
public function getDumps($getData = false)
203-
{
204-
if ($getData) {
157+
if ('html' === $format) {
158+
$dumper = new HtmlDumper();
159+
} elseif ('json' === $format) {
205160
$dumper = new JsonDumper();
161+
} else {
162+
throw new \InvalidArgumentException(sprintf('Invalid dump format: %s', $format));
163+
206164
}
207165
$dumps = array();
208166

209167
foreach ($this->data as $dump) {
210-
$json = '';
211-
if ($getData) {
212-
$dumper->dump($dump['data'], function ($line) use (&$json) {$json .= $line;});
213-
}
214-
$dump['data'] = $json;
168+
$data = '';
169+
$dumper->dump(
170+
$dump['data']->getLimitedClone($maxDepthLimit, $maxItemsPerDepth),
171+
function ($line, $depth) use (&$data) {
172+
if (false !==$depth) {
173+
$data .= str_repeat(' ', $depth).$line."\n";
174+
}
175+
}
176+
);
177+
$dump['data'] = $data;
215178
$dumps[] = $dump;
216179
}
217180

src/Symfony/Component/HttpKernel/Tests/DataCollector/DumpDataCollectorTest.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,21 +34,17 @@ public function testDump()
3434

3535
$xDump = array(
3636
array(
37-
'data' => '',
37+
'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",
3838
'name' => 'DumpDataCollectorTest.php',
3939
'file' => __FILE__,
4040
'line' => $line,
4141
'fileExcerpt' => false,
4242
),
4343
);
44-
45-
$this->assertSame($xDump, $collector->getDumps());
46-
47-
$xDump[0]['data'] = '123';
48-
$this->assertSame($xDump, $collector->getDumps(true));
44+
$this->assertSame($xDump, $collector->getDumps('html'));
4945

5046
$this->assertStringStartsWith(
51-
'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";',
47+
'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:',
5248
str_replace("\0", '', $collector->serialize())
5349
);
5450

src/Symfony/Component/VarDumper/Cloner/Data.php

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
class Data
2121
{
2222
private $data;
23+
private $maxDepth = -1;
24+
private $maxItemsPerDepth = -1;
2325

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

34+
/**
35+
* @return array The raw data structure.
36+
*/
3237
public function getRawData()
3338
{
3439
return $this->data;
3540
}
3641

42+
/**
43+
* Returns a depth limited clone of $this.
44+
*
45+
* @param int $maxDepth The max dumped depth level.
46+
* @param int $maxItemsPerDepth The max number of items dumped per depth level.
47+
*
48+
* @return self A depth limited clone of $this.
49+
*/
50+
public function getLimitedClone($maxDepth, $maxItemsPerDepth)
51+
{
52+
$data = clone $this;
53+
$data->maxDepth = (int) $maxDepth;
54+
$data->maxItemsPerDepth = (int) $maxItemsPerDepth;
55+
56+
return $data;
57+
}
58+
3759
/**
3860
* Dumps data with a DumperInternalsInterface dumper.
3961
*/
@@ -147,20 +169,23 @@ private function dumpItem($dumper, $cursor, &$refs, $item)
147169
private function dumpChildren($dumper, $parentCursor, &$refs, $children, $hashCut, $hashType)
148170
{
149171
if ($children) {
150-
$cursor = clone $parentCursor;
151-
++$cursor->depth;
152-
$cursor->hashType = $hashType;
153-
$cursor->hashIndex = 0;
154-
$cursor->hashLength = count($children);
155-
$cursor->hashCut = $hashCut;
156-
foreach ($children as $cursor->hashKey => $child) {
157-
$this->dumpItem($dumper, $cursor, $refs, $child);
158-
++$cursor->hashIndex;
159-
if ($cursor->stop) {
160-
$parentCursor->stop = true;
161-
162-
return $hashCut >= 0 ? $hashCut + $children - $cursor->hashIndex : $hashCut;
172+
if ($parentCursor->depth !== $this->maxDepth && $this->maxItemsPerDepth) {
173+
$cursor = clone $parentCursor;
174+
++$cursor->depth;
175+
$cursor->hashType = $hashType;
176+
$cursor->hashIndex = 0;
177+
$cursor->hashLength = count($children);
178+
$cursor->hashCut = $hashCut;
179+
foreach ($children as $cursor->hashKey => $child) {
180+
$this->dumpItem($dumper, $cursor, $refs, $child);
181+
if (++$cursor->hashIndex === $this->maxItemsPerDepth || $cursor->stop) {
182+
$parentCursor->stop = true;
183+
184+
return $hashCut >= 0 ? $hashCut + $cursor->hashLength - $cursor->hashIndex : $hashCut;
185+
}
163186
}
187+
} elseif ($hashCut >= 0) {
188+
$hashCut += count($children);
164189
}
165190
}
166191

src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php

Lines changed: 23 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class HtmlDumper extends CliDumper
2121
public static $defaultOutputStream = 'php://output';
2222

2323
protected $dumpHeader;
24-
protected $dumpPrefix = '<pre class=sf-dump style=white-space:pre>';
24+
protected $dumpPrefix = '<pre class=sf-dump>';
2525
protected $dumpSuffix = '</pre>';
2626
protected $colors = true;
2727
protected $headerIsDumped = false;
@@ -59,7 +59,7 @@ public function setStyles(array $styles)
5959
}
6060

6161
/**
62-
* Sets an HTML header the will be dumped once in the output stream.
62+
* Sets an HTML header that will be dumped once in the output stream.
6363
*
6464
* @param string $header An HTML string.
6565
*/
@@ -83,28 +83,32 @@ public function setDumpBoudaries($prefix, $suffix)
8383
/**
8484
* Dumps the HTML header.
8585
*/
86-
protected function dumpHeader()
86+
protected function getDumpHeader()
8787
{
8888
$this->headerIsDumped = true;
89-
$line = $this->line;
9089

9190
$p = 'sf-dump';
92-
$this->line = '<!DOCTYPE html><style>';
93-
parent::dumpLine(0);
94-
$this->line .= "a.$p-ref {{$this->styles['ref']}}";
95-
parent::dumpLine(0);
91+
$line = <<<EOHTML
92+
<!DOCTYPE html><style>
93+
pre.sf-dump {
94+
background-color: #300a24;
95+
white-space: pre;
96+
line-height: 1.2em;
97+
color: #eee8d5;
98+
font-family: monospace, sans-serif;
99+
padding: 5px;
100+
}
101+
.sf-dump span {
102+
display: inline;
103+
}
104+
EOHTML;
105+
$line .= "a.$p-ref {{$this->styles['ref']}}";
96106

97107
foreach ($this->styles as $class => $style) {
98-
$this->line .= "span.$p-$class {{$style}}";
99-
parent::dumpLine(0);
108+
$line .= "span.$p-$class {{$style}}";
100109
}
101110

102-
$this->line .= '</style>';
103-
parent::dumpLine(0);
104-
$this->line .= $this->dumpHeader;
105-
parent::dumpLine(0);
106-
107-
$this->line = $line;
111+
return preg_replace('/\s+/', ' ', $line).'</style>'.$this->dumpHeader;
108112
}
109113

110114
/**
@@ -144,9 +148,6 @@ protected function style($style, $val)
144148
*/
145149
protected function dumpLine($depth)
146150
{
147-
if (!$this->headerIsDumped) {
148-
$this->dumpHeader();
149-
}
150151

151152
switch ($this->lastDepth - $depth) {
152153
case +1: $this->line = '</span>'.$this->line; break;
@@ -156,6 +157,9 @@ protected function dumpLine($depth)
156157
if (-1 === $this->lastDepth) {
157158
$this->line = $this->dumpPrefix.$this->line;
158159
}
160+
if (!$this->headerIsDumped) {
161+
$this->line = $this->getDumpHeader().$this->line;
162+
}
159163

160164
if (false === $depth) {
161165
$this->lastDepth = -1;

src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -37,21 +37,7 @@ public function testGet()
3737

3838
$this->assertSame(
3939
<<<EOTXT
40-
<!DOCTYPE html><style>
41-
a.sf-dump-ref {color:#444444}
42-
span.sf-dump-num {font-weight:bold;color:#0087FF}
43-
span.sf-dump-const {font-weight:bold;color:#0087FF}
44-
span.sf-dump-str {font-weight:bold;color:#00D7FF}
45-
span.sf-dump-cchr {font-style: italic}
46-
span.sf-dump-note {color:#D7AF00}
47-
span.sf-dump-ref {color:#444444}
48-
span.sf-dump-public {color:#008700}
49-
span.sf-dump-protected {color:#D75F00}
50-
span.sf-dump-private {color:#D70000}
51-
span.sf-dump-meta {color:#005FFF}
52-
</style>
53-
54-
<pre class=sf-dump style=white-space:pre><span class=sf-dump-0><span class=sf-dump-note>array:25</span> [
40+
<!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> [
5541
<span class=sf-dump-1>"<span class=sf-dump-meta>number</span>" => <span class=sf-dump-num>1</span>
5642
<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>
5743
"<span class=sf-dump-meta>const</span>" => <span class=sf-dump-num>1.1</span>

0 commit comments

Comments
 (0)
0