Skip to content

Commit ecc5843

Browse files
[VarDumper] polymorphic and stateless output selection
1 parent d7aa4aa commit ecc5843

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

Dumper/AbstractDumper.php

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
2323
{
24-
public static $defaultOutputStream = 'php://output';
24+
public static $defaultOutput = 'php://output';
2525

2626
protected $line = '';
2727
protected $lineDumper;
@@ -30,37 +30,39 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
3030
protected $indentPad = ' ';
3131

3232
/**
33-
* @param callable|resource|string|null $outputStream A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutputStream.
33+
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput.
3434
*/
35-
public function __construct($outputStream = null)
35+
public function __construct($output = null)
3636
{
3737
$this->decimalPoint = (string) 0.5;
3838
$this->decimalPoint = $this->decimalPoint[1];
39-
if (is_callable($outputStream)) {
40-
$this->setLineDumper($outputStream);
41-
} else {
42-
if (null === $outputStream) {
43-
$outputStream =& static::$defaultOutputStream;
44-
}
45-
if (is_string($outputStream)) {
46-
$outputStream = fopen($outputStream, 'wb');
47-
}
48-
$this->outputStream = $outputStream;
49-
$this->setLineDumper(array($this, 'echoLine'));
39+
$this->setOutput($output ?: static::$defaultOutput);
40+
if (!$output && is_string(static::$defaultOutput)) {
41+
static::$defaultOutput = $this->outputStream;
5042
}
5143
}
5244

5345
/**
54-
* Sets a line dumper callback.
46+
* Sets the output destination of the dumps.
5547
*
56-
* @param callable $callback A callback responsible for writing the dump, one line at a time.
48+
* @param callable|resource|string $output A line dumper callable, an opened stream or an output path.
5749
*
58-
* @return callable|null The previous line dumper.
50+
* @return callable|resource|string The previous output destination.
5951
*/
60-
public function setLineDumper($callback)
52+
public function setOutput($output)
6153
{
62-
$prev = $this->lineDumper;
63-
$this->lineDumper = $callback;
54+
$prev = null !== $this->outputStream ? $this->outputStream : $this->lineDumper;
55+
56+
if (is_callable($output)) {
57+
$this->outputStream = null;
58+
$this->lineDumper = $output;
59+
} else {
60+
if (is_string($output)) {
61+
$output = fopen($output, 'wb');
62+
}
63+
$this->outputStream = $output;
64+
$this->lineDumper = array($this, 'echoLine');
65+
}
6466

6567
return $prev;
6668
}
@@ -83,23 +85,23 @@ public function setIndentPad($pad)
8385
/**
8486
* Dumps a Data object.
8587
*
86-
* @param Data $data A Data object.
87-
* @param callable|null $lineDumper A callback for writing dump's lines.
88+
* @param Data $data A Data object.
89+
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path.
8890
*/
89-
public function dump(Data $data, $lineDumper = null)
91+
public function dump(Data $data, $output = null)
9092
{
9193
$exception = null;
92-
if ($lineDumper) {
93-
$prevLineDumper = $this->setLineDumper($lineDumper);
94+
if ($output) {
95+
$prevOutput = $this->setOutput($output);
9496
}
9597
try {
9698
$data->dump($this);
9799
$this->dumpLine(-1);
98100
} catch (\Exception $exception) {
99101
// Re-thrown below
100102
}
101-
if ($lineDumper) {
102-
$this->setLineDumper($prevLineDumper);
103+
if ($output) {
104+
$this->setOutput($prevOutput);
103105
}
104106
if (null !== $exception) {
105107
throw $exception;

Dumper/CliDumper.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
class CliDumper extends AbstractDumper
2323
{
2424
public static $defaultColors;
25-
public static $defaultOutputStream = 'php://stdout';
25+
public static $defaultOutput = 'php://stdout';
2626

2727
protected $colors;
2828
protected $maxStringWidth = 0;
@@ -335,7 +335,7 @@ protected function style($style, $value, $attr = array())
335335
*/
336336
protected function supportsColors()
337337
{
338-
if ($this->outputStream !== static::$defaultOutputStream) {
338+
if ($this->outputStream !== static::$defaultOutput) {
339339
return @(is_resource($this->outputStream) && function_exists('posix_isatty') && posix_isatty($this->outputStream));
340340
}
341341
if (null !== static::$defaultColors) {

Dumper/HtmlDumper.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
*/
2222
class HtmlDumper extends CliDumper
2323
{
24-
public static $defaultOutputStream = 'php://output';
24+
public static $defaultOutput = 'php://output';
2525

2626
protected $dumpHeader;
2727
protected $dumpPrefix = '<pre class=sf-dump id=%s data-indent-pad="%s">';
@@ -47,11 +47,11 @@ class HtmlDumper extends CliDumper
4747
/**
4848
* {@inheritdoc}
4949
*/
50-
public function setLineDumper($callback)
50+
public function setOutput($output)
5151
{
5252
$this->headerIsDumped = false;
5353

54-
return parent::setLineDumper($callback);
54+
return parent::setOutput($output);
5555
}
5656

5757
/**
@@ -88,10 +88,10 @@ public function setDumpBoundaries($prefix, $suffix)
8888
/**
8989
* {@inheritdoc}
9090
*/
91-
public function dump(Data $data, $lineDumper = null)
91+
public function dump(Data $data, $output = null)
9292
{
9393
$this->dumpId = 'sf-dump-'.mt_rand();
94-
parent::dump($data, $lineDumper);
94+
parent::dump($data, $output);
9595
}
9696

9797
/**

0 commit comments

Comments
 (0)