Skip to content

Commit 5368483

Browse files
[DebugBundle] Use output mechanism of dumpers instead of echoing
1 parent 8cb2abb commit 5368483

File tree

2 files changed

+47
-15
lines changed

2 files changed

+47
-15
lines changed

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

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -246,20 +246,33 @@ public function __destruct()
246246

247247
private function doDump($data, $name, $file, $line)
248248
{
249-
if ($this->dumper instanceof HtmlDumper) {
250-
$name = $this->htmlEncode($name);
251-
$file = $this->htmlEncode($file);
252-
if ('' !== $file) {
253-
if ($this->fileLinkFormat) {
254-
$link = strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line));
255-
$name = sprintf('<a href="%s" title="%s">%s</a>', $link, $file, $name);
249+
if (PHP_VERSION_ID >= 50400 && $this->dumper instanceof CliDumper) {
250+
$contextDumper = function ($name, $file, $line, $fileLinkFormat) {
251+
if ($this instanceof HtmlDumper) {
252+
if ('' !== $file) {
253+
$s = $this->style('meta', '%s');
254+
$name = strip_tags($this->style('', $name));
255+
$file = strip_tags($this->style('', $file));
256+
if ($fileLinkFormat) {
257+
$link = strtr($fileLinkFormat, array('%f' => $file, '%l' => (int) $line));
258+
$name = sprintf('<a href="%s" title="%s">'.$s.'</a>', $link, $file, $name);
259+
} else {
260+
$name = sprintf('<abbr title="%s">'.$s.'</abbr>', $file, $name);
261+
}
262+
} else {
263+
$name = $this->style('meta', $name);
264+
}
265+
$this->line = $name.' on line '.$this->style('meta', $line).':';
256266
} else {
257-
$name = sprintf('<abbr title="%s">%s</abbr>', $file, $name);
267+
$this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
258268
}
259-
}
260-
echo "\n<span class=\"sf-dump-meta\">{$name} on line {$line}:</span>";
269+
$this->dumpLine(0);
270+
};
271+
$contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
272+
$contextDumper($name, $file, $line, $this->fileLinkFormat);
261273
} else {
262-
echo "{$name} on line {$line}:\n";
274+
$cloner = new VarCloner();
275+
$this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
263276
}
264277
$this->dumper->dump($data);
265278
}

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ public function testCollectDefault()
7171
$collector->collect(new Request(), new Response());
7272
$output = ob_get_clean();
7373

74-
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n123\n", $output);
74+
if (PHP_VERSION_ID >= 50400) {
75+
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n123\n", $output);
76+
} else {
77+
$this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n123\n", $output);
78+
}
7579
$this->assertSame(1, $collector->getDumpsCount());
7680
$collector->serialize();
7781
}
@@ -85,12 +89,23 @@ public function testCollectHtml()
8589
$collector->dump($data);
8690
$line = __LINE__ - 1;
8791
$file = __FILE__;
88-
$xOutput = <<<EOTXT
92+
if (PHP_VERSION_ID >= 50400) {
93+
$xOutput = <<<EOTXT
94+
<pre class=sf-dump id=sf-dump data-indent-pad=" "><a href="test://{$file}:{$line}" title="{$file}"><span class=sf-dump-meta>DumpDataCollectorTest.php</span></a> on line <span class=sf-dump-meta>{$line}</span>:
95+
<span class=sf-dump-num>123</span>
96+
</pre>
8997
90-
<span class="sf-dump-meta"><a href="test://{$file}:{$line}" title="{$file}">DumpDataCollectorTest.php</a> on line {$line}:</span> <pre class=sf-dump id=sf-dump data-indent-pad=" "><span class=sf-dump-num>123</span>
98+
EOTXT;
99+
} else {
100+
$len = strlen("DumpDataCollectorTest.php on line {$line}:");
101+
$xOutput = <<<EOTXT
102+
<pre class=sf-dump id=sf-dump data-indent-pad=" ">"<span class=sf-dump-str title="{$len} characters">DumpDataCollectorTest.php on line {$line}:</span>"
103+
</pre>
104+
<pre class=sf-dump id=sf-dump data-indent-pad=" "><span class=sf-dump-num>123</span>
91105
</pre>
92106
93107
EOTXT;
108+
}
94109

95110
ob_start();
96111
$response = new Response();
@@ -114,6 +129,10 @@ public function testFlush()
114129

115130
ob_start();
116131
$collector = null;
117-
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
132+
if (PHP_VERSION_ID >= 50400) {
133+
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
134+
} else {
135+
$this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n456\n", ob_get_clean());
136+
}
118137
}
119138
}

0 commit comments

Comments
 (0)