Skip to content

[VarDumper] Get dump as string with $dumper->dump(..., true); #19755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,29 @@ public function setIndentPad($pad)
/**
* Dumps a Data object.
*
* @param Data $data A Data object
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path
* @param Data $data A Data object
* @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
*
* @return string|null The dump as string when $output is true
*/
public function dump(Data $data, $output = null)
{
if ($returnDump = true === $output) {
$output = fopen('php://memory', 'r+b');
}
if ($output) {
$prevOutput = $this->setOutput($output);
}
try {
$data->dump($this);
$this->dumpLine(-1);

if ($returnDump) {
$result = stream_get_contents($output, -1, 0);
fclose($output);

return $result;
}
} finally {
if ($output) {
$this->setOutput($prevOutput);
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,10 @@ public function setDumpBoundaries($prefix, $suffix)
public function dump(Data $data, $output = null, array $extraDisplayOptions = array())
{
$this->extraDisplayOptions = $extraDisplayOptions;
parent::dump($data, $output);
$result = parent::dump($data, $output);
$this->dumpId = 'sf-dump-'.mt_rand();

return $result;
}

/**
Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,15 @@ protected function getDump($data, $key = null)
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;

$h = fopen('php://memory', 'r+b');
$cloner = new VarCloner();
$cloner->setMaxItems(-1);
$dumper = new CliDumper($h, null, $flags);
$dumper = new CliDumper(null, null, $flags);
$dumper->setColors(false);
$data = $cloner->cloneVar($data)->withRefHandles(false);
if (null !== $key && null === $data = $data->seek($key)) {
return;
}
$dumper->dump($data);
$data = stream_get_contents($h, -1, 0);
fclose($h);

return rtrim($data);
return rtrim($dumper->dump($data, true));
}
}
8 changes: 2 additions & 6 deletions src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,7 @@ public function testThrowingCaster()

$data = $cloner->cloneVar($out);
$dumper->dump($data, $out);
rewind($out);
$out = stream_get_contents($out);
$out = stream_get_contents($out, -1, 0);

if (method_exists($twig, 'getSource')) {
$twig = <<<EOTXT
Expand Down Expand Up @@ -328,11 +327,8 @@ public function testRefsInProperties()
$dumper->setColors(false);
$cloner = new VarCloner();

$out = fopen('php://memory', 'r+b');
$data = $cloner->cloneVar($var);
$dumper->dump($data, $out);
rewind($out);
$out = stream_get_contents($out);
$out = $dumper->dump($data, true);

$r = defined('HHVM_VERSION') ? '' : '#%d';
$this->assertStringMatchesFormat(
Expand Down
4 changes: 1 addition & 3 deletions src/Symfony/Component/VarDumper/Tests/HtmlDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,7 @@ public function testCharset()
$cloner = new VarCloner();

$data = $cloner->cloneVar($var);
$out = fopen('php://memory', 'r+b');
$dumper->dump($data, $out);
$out = stream_get_contents($out, -1, 0);
$out = $dumper->dump($data, true);

$this->assertStringMatchesFormat(
<<<EOTXT
Expand Down