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 61dd58a

Browse filesBrowse files
[VarDumper] Add Dump object to configure dumping options
1 parent d46ac31 commit 61dd58a
Copy full SHA for 61dd58a

File tree

1 file changed

+344
-0
lines changed
Filter options

1 file changed

+344
-0
lines changed
+344Lines changed: 344 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,344 @@
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;
13+
14+
use Symfony\Component\VarDumper\Cloner\VarCloner;
15+
use Symfony\Component\VarDumper\Dumper\CliDumper;
16+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
17+
18+
/**
19+
* @author Nicolas Grekas <p@tchwork.com>
20+
*/
21+
class Dump
22+
{
23+
const AS_HTML = 'html';
24+
const AS_CLI = 'cli';
25+
const AS_TEXT = 'text';
26+
const AS_ENV = 'env';
27+
28+
private $vars;
29+
private $dumpOnDestruct;
30+
private $to = null;
31+
private $format = 'env';
32+
private $die = false;
33+
private $title = '';
34+
private $trace = false;
35+
private $maxItems = 2500;
36+
private $maxDepth = -1;
37+
private $maxItemsPerDepth = -1;
38+
private $maxStringLength = -1;
39+
private $maxStringWidth = -1;
40+
private $useRefHandles = true;
41+
private $casters = array();
42+
private $replaceCasters = false;
43+
private $filter = 0;
44+
private $lightArray = false;;
45+
private $stringLength = false;;
46+
private $charset = false;;
47+
48+
public function __construct(array $vars, $dumpOnDestruct = true)
49+
{
50+
$this->vars = $vars;
51+
$this->dumpOnDestruct = $dumpOnDestruct;
52+
}
53+
54+
public function __destruct()
55+
{
56+
if ($this->dumpOnDestruct) {
57+
$this->dumpOnDestruct = false;
58+
$this->dump();
59+
}
60+
}
61+
62+
public function dump()
63+
{
64+
if ($this->replaceCasters) {
65+
$cloner = new VarCloner($this->casters);
66+
} else {
67+
$cloner = new VarCloner();
68+
$cloner->addCasters($this->casters);
69+
}
70+
$cloner->setMaxItems($this->maxItems);
71+
$cloner->setMaxString($this->maxStringLength);
72+
73+
$flags = $this->lightArray ? CliDumper::DUMP_LIGHT_ARRAY : 0;
74+
$flags |= $this->stringLength ? CliDumper::DUMP_STRING_LENGTH : 0;
75+
$dumper = $this->getDumper($this->getFormat($this->format), $this->output, $this->charset, $flags);
76+
$dumper->setMaxStringWidth($this->maxStringWidth);
77+
78+
foreach ($this->vars as $v) {
79+
$dumper->dump($cloner->cloneVar($v, $this->filters)
80+
->withMaxDepth($this->maxDepth)
81+
->withMaxItemsPerDepth($this->maxItemsPerDepth)
82+
->withRefHandles($this->useRefHandles)
83+
);
84+
}
85+
86+
if ($this->die) {
87+
exit(1);
88+
}
89+
}
90+
91+
protected function getFormat($format)
92+
{
93+
foreach (array($format, getenv('DUMP_FORMAT')) as $format) {
94+
$format = strtolower($format);
95+
if (self::AS_HTML === $format || self::AS_CLI === $format || self::AS_TEXT === $format) {
96+
return $format;
97+
}
98+
}
99+
100+
return 'cli' === PHP_SAPI ? self::AS_CLI : self::AS_HTML;
101+
}
102+
103+
protected function getDumper($format, $output, $charset, $flags)
104+
{
105+
if (self::AS_HTML === $format) {
106+
return new HtmlDumper($output, $charset, $flags);
107+
}
108+
109+
$dumper = new CliDumper($output, $charset, $flags);
110+
111+
if (self::AS_TEXT === $format) {
112+
$dumper->setColors(false);
113+
}
114+
115+
return $dumper;
116+
}
117+
118+
/**
119+
* Sets the output destination of the dump.
120+
*
121+
* @param callable|resource|string $output A line callable, an opened stream or an output path.
122+
*
123+
* @return self
124+
*/
125+
public function to($output)
126+
{
127+
$dump = clone $this;
128+
$this->dumpOnDestruct = false;
129+
$dump->output = $output;
130+
131+
return $dump;
132+
}
133+
134+
/**
135+
* Sets the output format of the dump as one of the self::AS_* constants, the default being to check the `DUMP_FORMAT` env var then the PHP SAPI.
136+
*
137+
* @return self
138+
*/
139+
public function format($format)
140+
{
141+
$dump = clone $this;
142+
$this->dumpOnDestruct = false;
143+
$dump->format = (string) $format;
144+
145+
return $dump;
146+
}
147+
148+
/**
149+
* Dies after dumping.
150+
*
151+
* @return self
152+
*/
153+
public function thenDie($die = true)
154+
{
155+
$dump = clone $this;
156+
$this->dumpOnDestruct = false;
157+
$dump->die = (bool) $die;
158+
159+
return $dump;
160+
}
161+
162+
/**
163+
* Sets the title of the dump.
164+
*
165+
* @return self
166+
*/
167+
public function withTitle($title)
168+
{
169+
$dump = clone $this;
170+
$this->dumpOnDestruct = false;
171+
$dump->title = (string) $title;
172+
173+
return $dump;
174+
}
175+
176+
/**
177+
* Enables/disables dumping the stack trace after dumping.
178+
*
179+
* @return self
180+
*/
181+
public function withTrace($trace)
182+
{
183+
$dump = clone $this;
184+
$this->dumpOnDestruct = false;
185+
$dump->trace = (bool) $trace;
186+
187+
return $dump;
188+
}
189+
190+
/**
191+
* Limits the number of items past the first level.
192+
*
193+
* @return self
194+
*/
195+
public function withMaxItems($items)
196+
{
197+
$dump = clone $this;
198+
$this->dumpOnDestruct = false;
199+
$dump->maxItems = (int) $items;
200+
201+
return $dump;
202+
}
203+
204+
/**
205+
* Limits the depth of the dump.
206+
*
207+
* @return self
208+
*/
209+
public function withMaxDepth($depth)
210+
{
211+
$dump = clone $this;
212+
$this->dumpOnDestruct = false;
213+
$dump->maxDepth = (int) $depth;
214+
215+
return $dump;
216+
}
217+
218+
/**
219+
* Limits the number of items per depth level.
220+
*
221+
* @return self
222+
*/
223+
public function withMaxItemsPerDepth($max)
224+
{
225+
$dump = clone $this;
226+
$this->dumpOnDestruct = false;
227+
$dump->maxItemsPerDepth = (int) $max;
228+
229+
return $dump;
230+
}
231+
232+
/**
233+
* Limits the number of characters for dumped strings.
234+
*
235+
* @return self
236+
*/
237+
public function withMaxStringLength($length)
238+
{
239+
$dump = clone $this;
240+
$this->dumpOnDestruct = false;
241+
$dump->maxStringLength = (int) $length;
242+
243+
return $dump;
244+
}
245+
246+
/**
247+
* Limits the number of characters per line for dumped strings.
248+
*
249+
* @return self
250+
*/
251+
public function withMaxStringWidth($width)
252+
{
253+
$dump = clone $this;
254+
$this->dumpOnDestruct = false;
255+
$dump->maxStringWidth = (int) $width;
256+
257+
return $dump;
258+
}
259+
260+
/**
261+
* Enables/disables objects' global identifiers dumping.
262+
*
263+
* @return self
264+
*/
265+
public function withRefHandles($handles)
266+
{
267+
$dump = clone $this;
268+
$this->dumpOnDestruct = false;
269+
$dump->useRefHandles = (bool) $handles;
270+
271+
return $dump;
272+
}
273+
274+
/**
275+
* Adds or replaces casters for resources and objects.
276+
*
277+
* @return self
278+
*/
279+
public function withCasters(array $casters, $replace = false)
280+
{
281+
$dump = clone $this;
282+
$this->dumpOnDestruct = false;
283+
$dump->casters = $casters;
284+
$dump->replaceCasters = (bool) $replace;
285+
286+
return $dump;
287+
}
288+
289+
/**
290+
* A bit field of Caster::EXCLUDE_* constants.
291+
*
292+
* @return self
293+
*/
294+
public function withFilter($filter)
295+
{
296+
$dump = clone $this;
297+
$this->dumpOnDestruct = false;
298+
$dump->filter = (int) $filter;
299+
300+
return $dump;
301+
}
302+
303+
/**
304+
* A bit field of Caster::EXCLUDE_* constants.
305+
*
306+
* @return self
307+
*/
308+
public function withLightArray($light = true)
309+
{
310+
$dump = clone $this;
311+
$this->dumpOnDestruct = false;
312+
$dump->lightArray = (int) $light;
313+
314+
return $dump;
315+
}
316+
317+
/**
318+
* A bit field of Caster::EXCLUDE_* constants.
319+
*
320+
* @return self
321+
*/
322+
public function withStringLength($length = true)
323+
{
324+
$dump = clone $this;
325+
$this->dumpOnDestruct = false;
326+
$dump->stringLength = (bool) $length;
327+
328+
return $dump;
329+
}
330+
331+
/**
332+
* Sets the character encoding to use for non-UTF8 strings.
333+
*
334+
* @return self
335+
*/
336+
public function withCharset($charset)
337+
{
338+
$dump = clone $this;
339+
$this->dumpOnDestruct = false;
340+
$dump->charset = (string) $charset;
341+
342+
return $dump;
343+
}
344+
}

0 commit comments

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