Skip to content

[VarDumper] Add flags to allow fine tuning dumps representation #17305

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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=');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about always resetting these env vars in setUp()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks overkill to me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should just keep it in mind then in case we add similar tests in the future.

}

public function testThrowingCaster()
{
$out = fopen('php://memory', 'r+b');
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/VarDumper/phpunit.xml.dist
Original file line number Diff line number Diff line change
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