8000 feature #19755 [VarDumper] Get dump as string with `$dumper->dump(...… · symfony/symfony@3030c79 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3030c79

Browse files
feature #19755 [VarDumper] Get dump as string with $dumper->dump(..., true); (nicolas-grekas)
This PR was merged into the 3.2-dev branch. Discussion ---------- [VarDumper] Get dump as string with `$dumper->dump(..., true);` | Q | A | ------------- | --- | Branch? | master | New feature? | yes | Tests pass? | yes | License | MIT ping @lyrixx @wouterj since you expressed interest in boilerplate reduction around this in the past :) Commits ------- 4be9776 [VarDumper] Get dump as string with `$dumper->dump(..., true);`
2 parents feb5413 + 4be9776 commit 3030c79

File tree

5 files changed

+22
-18
lines changed

5 files changed

+22
-18
lines changed

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

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,29 @@ public function setIndentPad($pad)
114114
/**
115115
* Dumps a Data object.
116116
*
117-
* @param Data $data A Data object
118-
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path
117+
* @param Data $data A Data object
118+
* @param callable|resource|string|true|null $output A line dumper callable, an opened stream, an output path or true to return the dump
119+
*
120+
* @return string|null The dump as string when $output is true
119121
*/
120122
public function dump(Data $data, $output = null)
121123
{
124+
if ($returnDump = true === $output) {
125+
$output = fopen('php://memory', 'r+b');
126+
}
122127
if ($output) {
123128
$prevOutput = $this->setOutput($output);
124129
}
125130
try {
126131
$data->dump($this);
127132
$this->dumpLine(-1);
133+
134+
if ($returnDump) {
135+
$result = stream_get_contents($output, -1, 0);
136+
fclose($output);
137+
138+
return $result;
139+
}
128140
} finally {
129141
if ($output) {
130142
$this->setOutput($prevOutput);

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,10 @@ public function setDumpBoundaries($prefix, $suffix)
108108
public function dump(Data $data, $output = null, array $extraDisplayOptions = array())
109109
{
110110
$this->extraDisplayOptions = $extraDisplayOptions;
111-
parent::dump($data, $output);
111+
$result = parent::dump($data, $output);
112112
$this->dumpId = 'sf-dump-'.mt_rand();
113+
114+
return $result;
113115
}
114116

115117
/**

src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,15 @@ protected function getDump($data, $key = null)
3434
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
3535
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
3636

37-
$h = fopen('php://memory', 'r+b');
3837
$cloner = new VarCloner();
3938
$cloner->setMaxItems(-1);
40-
$dumper = new CliDumper($h, null, $flags);
39+
$dumper = new CliDumper(null, null, $flags);
4140
$dumper->setColors(false);
4241
$data = $cloner->cloneVar($data)->withRefHandles(false);
4342
if (null !== $key && null === $data = $data->seek($key)) {
4443
return;
4544
}
46-
$dumper->dump($data);
47-
$data = stream_get_contents($h, -1, 0);
48-
fclose($h);
4945

50-
return rtrim($data);
46+
return rtrim($dumper->dump($data, true));
5147
}
5248
}

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

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,7 @@ public function testThrowingCaster()
259259

260260
$data = $cloner->cloneVar($out);
261261
$dumper->dump($data, $out);
262-
rewind($out);
263-
$out = stream_get_contents($out);
262+
$out = stream_get_contents($out, -1, 0);
264263

265264
if (method_exists($twig, 'getSource')) {
266265
$twig = <<<EOTXT
@@ -328,11 +327,8 @@ public function testRefsInProperties()
328327
$dumper->setColors(false);
329328
$cloner = new VarCloner();
330329

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

337333
$r = defined('HHVM_VERSION') ? '' : '#%d';
338334
$this->assertStringMatchesFormat(

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,7 @@ public function testCharset()
120120
$cloner = new VarCloner();
121121

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

127125
$this->assertStringMatchesFormat(
128126
<<<EOTXT

0 commit comments

Comments
 (0)
0