21
21
*/
22
22
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
23
23
{
24
- public static $ defaultOutputStream = 'php://output ' ;
24
+ public static $ defaultOutput = 'php://output ' ;
25
25
26
26
protected $ line = '' ;
27
27
protected $ lineDumper ;
@@ -30,37 +30,39 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
30
30
protected $ indentPad = ' ' ;
31
31
32
32
/**
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 .
34
34
*/
35
- public function __construct ($ outputStream = null )
35
+ public function __construct ($ output = null )
36
36
{
37
37
$ this ->decimalPoint = (string ) 0.5 ;
38
38
$ 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 ;
50
42
}
51
43
}
52
44
53
45
/**
54
- * Sets a line dumper callback .
46
+ * Sets the output destination of the dumps .
55
47
*
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 .
57
49
*
58
- * @return callable|null The previous line dumper .
50
+ * @return callable|resource|string The previous output destination .
59
51
*/
60
- public function setLineDumper ( $ callback )
52
+ public function setOutput ( $ output )
61
53
{
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
+ }
64
66
65
67
return $ prev ;
66
68
}
@@ -83,23 +85,23 @@ public function setIndentPad($pad)
83
85
/**
84
86
* Dumps a Data object.
85
87
*
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 .
88
90
*/
89
- public function dump (Data $ data , $ lineDumper = null )
91
+ public function dump (Data $ data , $ output = null )
90
92
{
91
93
$ exception = null ;
92
- if ($ lineDumper ) {
93
- $ prevLineDumper = $ this ->setLineDumper ( $ lineDumper );
94
+ if ($ output ) {
95
+ $ prevOutput = $ this ->setOutput ( $ output );
94
96
}
95
97
try {
96
98
$ data ->dump ($ this );
97
99
$ this ->dumpLine (-1 );
98
100
} catch (\Exception $ exception ) {
99
101
// Re-thrown below
100
102
}
101
- if ($ lineDumper ) {
102
- $ this ->setLineDumper ( $ prevLineDumper );
103
+ if ($ output ) {
104
+ $ this ->setOutput ( $ prevOutput );
103
105
}
104
106
if (null !== $ exception ) {
105
107
throw $ exception ;
0 commit comments