Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit fa81544

Browse filesBrowse files
[VarDumper] CLI dedicated dumper and related abstract
1 parent 1d5e3f4 commit fa81544
Copy full SHA for fa81544

File tree

Expand file treeCollapse file tree

2 files changed

+557
-0
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+557
-0
lines changed
+123Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Dumper;
13+
14+
use Symfony\Component\VarDumper\Cloner\Data;
15+
16+
/**
17+
* Abstract mechanism for dumping a Data object.
18+
*
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
abstract class AbstractDumper implements DataDumperInterface, DumperInternalsInterface
22+
{
23+
public static $defaultOutputStream = 'php://output';
24+
25+
protected $line = '';
26+
protected $lineDumper;
27+
protected $outputStream;
28+
protected $decimalPoint; // This is locale dependent
29+
protected $indentPad = ' ';
30+
31+
/**
32+
* @param callable|resource|string|null $outputStream A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutputStream.
33+
*/
34+
public function __construct($outputStream = null)
35+
{
36+
$this->decimalPoint = (string) 0.5;
37+
$this->decimalPoint = $this->decimalPoint[1];
38+
if (is_callable($outputStream)) {
39+
$this->setLineDumper($outputStream);
40+
} else {
41+
if (null === $outputStream) {
42+
$outputStream =& static::$defaultOutputStream;
43+
}
44+
if (is_string($outputStream)) {
45+
$outputStream = fopen($outputStream, 'wb');
46+
}
47+
$this->outputStream = $outputStream;
48+
$this->setLineDumper(array($this, 'echoLine'));
49+
}
50+
}
51+
52+
/**
53+
* Sets a line dumper callback.
54+
*
55+
* @param callable $callback A callback responsible for writing the dump, one line at a time.
56+
*
57+
* @return callable|null The previous line dumper.
58+
*/
59+
public function setLineDumper($callback)
60+
{
61+
$prev = $this->lineDumper;
62+
$this->lineDumper = $callback;
63+
64+
return $prev;
65+
}
66+
67+
/**
68+
* Sets the indentation pad string.
69+
*
70+
* @param string $pad A string the will be prepended to dumped lines, repeated by nesting level.
71+
*
72+
* @return string The indent pad.
73+
*/
74+
public function setIndentPad($pad)
75+
{
76+
$prev = $this->indentPad;
77+
$this->indentPad = $pad;
78+
79+
return $prev;
80+
}
81+
82+
/**
83+
* Dumps a Data object.
84+
*
85+
* @param Data $data A Data object.
86+
* @param callable|null $lineDumper A callback for writing dump's lines.
87+
*/
88+
public function dump(Data $data, $lineDumper = null)
89+
{
90+
$this->decimalPoint = (string) 0.5;
91+
$this->decimalPoint = $this->decimalPoint[1];
92+
$dumper = clone $this;
93+
if ($lineDumper) {
94+
$dumper->setLineDumper($lineDumper);
95+
}
96+
$data->dump($dumper);
97+
$dumper->dumpLine(false);
98+
}
99+
100+
/**
101+
* Dumps the current line.
102+
*
103+
* @param int $depth The recursive depth in the dumped structure for the line being dumped.
104+
*/
105+
protected function dumpLine($depth)
106+
{
107+
call_user_func($this->lineDumper, $this->line, $depth);
108+
$this->line = '';
109+
}
110+
111+
/**
112+
* Generic line dumper callback.
113+
*
114+
* @param string $line The line to write.
115+
* @param int $depth The recursive depth in the dumped structure.
116+
*/
117+
protected function echoLine($line, $depth)
118+
{
119+
if (false !== $depth) {
120+
fwrite($this->outputStream, str_repeat($this->indentPad, $depth).$line."\n");
121+
}
122+
}
123+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.