Skip to content

Commit

Permalink
[VarDumper] Add flags to allow fine tuning dumps representation
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Jan 13, 2016
1 parent b15c734 commit a35ceb0
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 7 deletions.
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Expand Up @@ -11,6 +11,8 @@
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
<env name="DUMP_LIGHT_ARRAY" value="" />
<env name="DUMP_STRING_LENGTH" value="" />
</php>

<testsuites>
Expand Down
8 changes: 7 additions & 1 deletion src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php
Expand Up @@ -21,22 +21,28 @@
*/
abstract class AbstractDumper implements DataDumperInterface, DumperInterface
{
const DUMP_LIGHT_ARRAY = 1;
const DUMP_STRING_LENGTH = 2;

public static $defaultOutput = 'php://output';

protected $line = '';
protected $lineDumper;
protected $outputStream;
protected $decimalPoint; // This is locale dependent
protected $indentPad = ' ';
protected $flags;

private $charset;

/**
* @param callable|resource|string|null $output A line dumper callable, an opened stream or an output path, defaults to static::$defaultOutput.
* @param string $charset The default character encoding to use for non-UTF8 strings.
* @param int $flags A bit field of static::DUMP_* constants to fine tune dumps representation.
*/
public function __construct($output = null, $charset = null)
public function __construct($output = null, $charset = null, $flags = 0)
{
$this->flags = (int) $flags;
$this->setCharset($charset ?: ini_get('php.output_encoding') ?: ini_get('default_charset') ?: 'UTF-8');
$this->decimalPoint = (string) 0.5;
$this->decimalPoint = $this->decimalPoint[1];
Expand Down
12 changes: 9 additions & 3 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Expand Up @@ -54,9 +54,9 @@ class CliDumper extends AbstractDumper
/**
* {@inheritdoc}
*/
public function __construct($output = null, $charset = null)
public function __construct($output = null, $charset = null, $flags = 0)
{
parent::__construct($output, $charset);
parent::__construct($output, $charset, $flags);

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

if (self::DUMP_STRING_LENGTH & $this->flags) {
$this->line .= '('.$attr['length'].') ';
}
if ($bin) {
$this->line .= 'b';
}
Expand Down Expand Up @@ -249,7 +252,7 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
} elseif (Cursor::HASH_RESOURCE === $type) {
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
} else {
$prefix = $class ? $this->style('note', 'array:'.$class).' [' : '[';
$prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class).' [' : '[';
}

if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {
Expand Down Expand Up @@ -314,6 +317,9 @@ protected function dumpKey(Cursor $cursor)
switch ($cursor->hashType) {
default:
case Cursor::HASH_INDEXED:
if (self::DUMP_LIGHT_ARRAY & $this->flags) {
break;
}
$style = 'index';
case Cursor::HASH_ASSOC:
if (is_int($key)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/VarDumper/Dumper/HtmlDumper.php
Expand Up @@ -48,9 +48,9 @@ class HtmlDumper extends CliDumper
/**
* {@inheritdoc}
*/
public function __construct($output = null, $charset = null)
public function __construct($output = null, $charset = null, $flags = 0)
{
AbstractDumper::__construct($output, $charset);
AbstractDumper::__construct($output, $charset, $flags);
$this->dumpId = 'sf-dump-'.mt_rand();
}

Expand Down
5 changes: 4 additions & 1 deletion src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php
Expand Up @@ -31,10 +31,13 @@ public function assertDumpMatchesFormat($dump, $data, $message = '')

protected function getDump($data)
{
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;

$h = fopen('php://memory', 'r+b');
$cloner = new VarCloner();
$cloner->setMaxItems(-1);
$dumper = new CliDumper($h);
$dumper = new CliDumper($h, null, $flags);
$dumper->setColors(false);
$dumper->dump($cloner->cloneVar($data)->withRefHandles(false));
$data = stream_get_contents($h, -1, 0);
Expand Down
32 changes: 32 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Expand Up @@ -160,6 +160,38 @@ public function testClosedResource()
);
}

public function testFlags()
{
putenv('DUMP_LIGHT_ARRAY=1');
putenv('DUMP_STRING_LENGTH=1');

$var = array(
range(1,3),
array('foo', 2 => 'bar'),
);

$this->assertDumpEquals(
<<<EOTXT
[
[
1
2
3
]
[
0 => (3) "foo"
2 => (3) "bar"
]
]
EOTXT
,
$var
);

putenv('DUMP_LIGHT_ARRAY=');
putenv('DUMP_STRING_LENGTH=');
}

public function testThrowingCaster()
{
$out = fopen('php://memory', 'r+b');
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/phpunit.xml.dist
Expand Up @@ -8,6 +8,8 @@
>
<php>
<ini name="error_reporting" value="-1" />
<env name="DUMP_LIGHT_ARRAY" value="" />
<env name="DUMP_STRING_LENGTH" value="" />
</php>

<testsuites>
Expand Down

0 comments on commit a35ceb0

Please sign in to comment.