8000 [VarDumper] Add flags to allow fine tuning dumps representation · symfony/symfony@c7b5d86 · GitHub
[go: up one dir, main page]

Skip to content

Commit c7b5d86

Browse files
[VarDumper] Add flags to allow fine tuning dumps representation
1 parent b15c734 commit c7b5d86

File tree

7 files changed

+58
-7
lines changed

7 files changed

+58
-7
lines changed

phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<ini name="intl.default_locale" value="en" />
1212
<ini name="intl.error_level" value="0" />
1313
<ini name="memory_limit" value="-1" />
14+
<env name="DUMP_LIGHT_ARRAY" value="" />
15+
<env name="DUMP_STRING_LENGTH" value="" />
1416
</php>
1517

1618
<testsuites>

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,28 @@
2121
*/
2222
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
2323
{
24+
const DUMP_LIGHT_ARRAY = 1;
25+
const DUMP_STRING_LENGTH = 2;
26+
2427
public static $defaultOutput = 'php://output';
2528

2629
protected $line = '';
2730
protected $lineDumper;
2831
protected $outputStream;
2932
protected $decimalPoint; // This is locale dependent
3033
protected $indentPad = ' ';
34+
protected $flags;
3135

3236
private $charset;
3337

3438
/**
3539
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput.
3640
* @param string $charset The default character encoding to use for non-UTF8 strings.
41+
* @param int $flags A bit field of static::DUMP_* constants to fine tune dumps representation.
3742
*/
38-
public function __construct($output = null, $charset = null)
43+
public function __construct($output = null, $charset = null, $flags = 0)
3944
{
45+
$this->flags = (int) $flags;
4046
$this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
4147
$this->decimalPoint = (string) 0.5;
4248
$this->decimalPoint = $this->decimalPoint[1];

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ class CliDumper extends AbstractDumper
5454
/**
5555
* {@inheritdoc}
5656
*/
57-
public function __construct($output = null, $charset = null)
57+
public function __construct($output = null, $charset = null, $flags = 0)
5858
{
59-
parent::__construct($output, $charset);
59+
parent::__construct($output, $charset, $flags);
6060

6161
if ('\\' === DIRECTORY_SEPARATOR && false !== @getenv('ANSICON')) {
6262
// Use only the base 16 xterm colors when using ANSICON
@@ -180,6 +180,9 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
180180
$m = count($str) - 1;
181181
$i = $lineCut = 0;
182182

183+
if ($this->flags & self::DUMP_STRING_LENGTH) {
184+
$this->line .= '('.$attr['length'].') ';
185+
}
183186
if ($bin) {
184187
$this->line .= 'b';
185188
}
@@ -249,7 +252,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
249252
} elseif (Cursor::HASH_RESOURCE === $type) {
250253
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
251254
} else {
252-
$prefix = $class ? $this->style('note', 'array:'.$class).' [' : '[';
255+
$prefix = $class && !($this->flags & self::DUMP_LIGHT_ARRAY) ? $this->style('note', 'array:'.$class).' [' : '[';
253256
}
254257

255258
if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {
@@ -314,6 +317,9 @@ protected function dumpKey(Cursor $cursor)
314317
switch ($cursor->hashType) {
315318
default:
316319
case Cursor::HASH_INDEXED:
320+
if ($this->flags & self::DUMP_LIGHT_ARRAY) {
321+
break;
322+
}
317323
$style = 'index';
318324
case Cursor::HASH_ASSOC:
319325
if (is_int($key)) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class HtmlDumper extends CliDumper
4848
/**
4949
* {@inheritdoc}
5050
*/
51-
public function __construct($output = null, $charset = null)
51+
public function __construct($output = null, $charset = null, $flags = 0)
5252
{
53-
AbstractDumper::__construct($output, $charset);
53+
AbstractDumper::__construct($output, $charset, $flags);
5454
$this->dumpId = 'sf-dump-'.mt_rand();
5555
}
5656

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,13 @@ public function assertDumpMatchesFormat($dump, $data, $message = '')
3131

3232
protected function getDump($data)
3333
{
34+
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
35+
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
36+
3437
$h = fopen('php://memory', 'r+b');
3538
$cloner = new VarCloner();
3639
$cloner->setMaxItems(-1);
37-
$dumper = new CliDumper($h);
40+
$dumper = new CliDumper($h, null, $flags);
3841
$dumper->setColors(false);
3942
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
4043
$data = stream_get_contents($h, -1, 0);

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

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,38 @@ public function testClosedResource()
160160
);
161161
}
162162

163+
public function testFlags()
164+
{
165+
putenv('DUMP_LIGHT_ARRAY=1');
166+
putenv('DUMP_STRING_LENGTH=1');
167+
168+
$var = array(
169+
range(1,3),
170+
array('foo', 2 => 'bar'),
171+
);
172+
173+
$this->assertDumpEquals(
174+
<<<EOTXT
175+
[
176+
[
177+
1
178+
2
179+
3
180+
]
181+
[
182+
0 => (3) "foo"
183+
2 => (3) "bar"
184+
]
185+
]
186+
EOTXT
187+
,
188+
$var
189+
);
190+
191+
putenv('DUMP_LIGHT_ARRAY=');
192+
putenv('DUMP_STRING_LENGTH=');
193+
}
194+
163195
public function testThrowingCaster()
164196
{
165197
$out = fopen('php://memory', 'r+b');

src/Symfony/Component/VarDumper/phpunit.xml.dist

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
>
99
<php>
1010
<ini name="error_reporting" value="-1" />
11+
<env name="DUMP_LIGHT_ARRAY" value="" />
12+
<env name="DUMP_STRING_LENGTH" value="" />
1113
</php>
1214

1315
<testsuites>

0 commit comments

Comments
 (0)
0