[DebugBundle] Use output mechanism of dumpers instead of echoing

This commit is contained in:
Nicolas Grekas 2015-05-14 09:57:59 +02:00
parent 8cb2abbd26
commit 5368483d9a
2 changed files with 48 additions and 16 deletions

View File

@ -246,20 +246,33 @@ class DumpDataCollector extends DataCollector implements DataDumperInterface
private function doDump($data, $name, $file, $line) private function doDump($data, $name, $file, $line)
{ {
if ($this->dumper instanceof HtmlDumper) { if (PHP_VERSION_ID >= 50400 && $this->dumper instanceof CliDumper) {
$name = $this->htmlEncode($name); $contextDumper = function ($name, $file, $line, $fileLinkFormat) {
$file = $this->htmlEncode($file); if ($this instanceof HtmlDumper) {
if ('' !== $file) { if ('' !== $file) {
if ($this->fileLinkFormat) { $s = $this->style('meta', '%s');
$link = strtr($this->fileLinkFormat, array('%f' => $file, '%l' => $line)); $name = strip_tags($this->style('', $name));
$name = sprintf('<a href="%s" title="%s">%s</a>', $link, $file, $name); $file = strip_tags($this->style('', $file));
if ($fileLinkFormat) {
$link = strtr($fileLinkFormat, array('%f' => $file, '%l' => (int) $line));
$name = sprintf('<a href="%s" title="%s">'.$s.'</a>', $link, $file, $name);
} else {
$name = sprintf('<abbr title="%s">'.$s.'</abbr>', $file, $name);
}
} else {
$name = $this->style('meta', $name);
}
$this->line = $name.' on line '.$this->style('meta', $line).':';
} else { } else {
$name = sprintf('<abbr title="%s">%s</abbr>', $file, $name); $this->line = $this->style('meta', $name).' on line '.$this->style('meta', $line).':';
} }
} $this->dumpLine(0);
echo "\n<span class=\"sf-dump-meta\">{$name} on line {$line}:</span>"; };
$contextDumper = $contextDumper->bindTo($this->dumper, $this->dumper);
$contextDumper($name, $file, $line, $this->fileLinkFormat);
} else { } else {
echo "{$name} on line {$line}:\n"; $cloner = new VarCloner();
$this->dumper->dump($cloner->cloneVar($name.' on line '.$line.':'));
} }
$this->dumper->dump($data); $this->dumper->dump($data);
} }

View File

@ -71,7 +71,11 @@ class DumpDataCollectorTest extends \PHPUnit_Framework_TestCase
$collector->collect(new Request(), new Response()); $collector->collect(new Request(), new Response());
$output = ob_get_clean(); $output = ob_get_clean();
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n123\n", $output); if (PHP_VERSION_ID >= 50400) {
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n123\n", $output);
} else {
$this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n123\n", $output);
}
$this->assertSame(1, $collector->getDumpsCount()); $this->assertSame(1, $collector->getDumpsCount());
$collector->serialize(); $collector->serialize();
} }
@ -85,12 +89,23 @@ class DumpDataCollectorTest extends \PHPUnit_Framework_TestCase
$collector->dump($data); $collector->dump($data);
$line = __LINE__ - 1; $line = __LINE__ - 1;
$file = __FILE__; $file = __FILE__;
$xOutput = <<<EOTXT if (PHP_VERSION_ID >= 50400) {
$xOutput = <<<EOTXT
<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> <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>:
<span class=sf-dump-num>123</span>
</pre> </pre>
EOTXT; EOTXT;
} else {
$len = strlen("DumpDataCollectorTest.php on line {$line}:");
$xOutput = <<<EOTXT
<pre class=sf-dump id=sf-dump data-indent-pad=" ">"<span class=sf-dump-str title="{$len} characters">DumpDataCollectorTest.php on line {$line}:</span>"
</pre>
<pre class=sf-dump id=sf-dump data-indent-pad=" "><span class=sf-dump-num>123</span>
</pre>
EOTXT;
}
ob_start(); ob_start();
$response = new Response(); $response = new Response();
@ -114,6 +129,10 @@ EOTXT;
ob_start(); ob_start();
$collector = null; $collector = null;
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean()); if (PHP_VERSION_ID >= 50400) {
$this->assertSame("DumpDataCollectorTest.php on line {$line}:\n456\n", ob_get_clean());
} else {
$this->assertSame("\"DumpDataCollectorTest.php on line {$line}:\"\n456\n", ob_get_clean());
}
} }
} }