8000 [VarDumper] Added a way to print or not comma separator and/or trailing comma by lyrixx · Pull Request #21653 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[VarDumper] Added a way to print or not comma separator and/or trailing comma #21653

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
Feb 20, 2017
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 src/Symfony/Component/VarDumper/Dumper/AbstractDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
{
const DUMP_LIGHT_ARRAY = 1;
const DUMP_STRING_LENGTH = 2;
const DUMP_COMMA_SEPARATOR = 4;
const DUMP_TRAILING_COMMA = 8;

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

Expand Down
23 changes: 19 additions & 4 deletions src/Symfony/Component/VarDumper/Dumper/CliDumper.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public function dumpScalar(Cursor $cursor, $type, $value)

$this->line .= $this->style($style, $value, $attr);

$this->dumpLine($cursor->depth, true);
$this->endValue($cursor);
}

/**
Expand All @@ -171,7 +171,7 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
}
if ('' === $str) {
$this->line .= '""';
$this->dumpLine($cursor->depth, true);
$this->endValue($cursor);
} else {
$attr += array(
'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0,
Expand Down Expand Up @@ -237,7 +237,11 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
$lineCut = 0;
}

$this->dumpLine($cursor->depth, $i > $m);
if ($i > $m) {
$this->endValue($cursor);
} else {
$this->dumpLine($cursor->depth);
}
}
}
}
Expand Down Expand Up @@ -280,7 +284,7 @@ public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
{
$this->dumpEllipsis($cursor, $hasChild, $cut);
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
$this->dumpLine($cursor->depth, true);
$this->endValue($cursor);
}

/**
Expand Down Expand Up @@ -486,4 +490,15 @@ protected function dumpLine($depth, $endOfValue = false)
}
parent::dumpLine($depth);
}

protected function endValue(Cursor $cursor)
{
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
$this->line .= ',';
} elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) {
$this->line .= ',';
}

$this->dumpLine($cursor->depth, true);
}
}
61 changes: 61 additions & 0 deletions src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,67 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
);
}

/**
* @dataProvider provideDumpWithCommaFlagTests
*/
public function testDumpWithCommaFlag($expected, $flags)
{
$dumper = new CliDumper(null, null, $flags);
$dumper->setColors(false);
$cloner = new VarCloner();

$var = array(
'array' => array('a', 'b'),
'string' => 'hello',
'multiline string' => "this\nis\na\multiline\nstring",
);

$dump = $dumper->dump($cloner->cloneVar($var), true);

$this->assertSame($expected, $dump);
}

public function provideDumpWithCommaFlagTests()
{
$expected = <<<'EOTXT'
array:3 [
"array" => array:2 [
0 => "a",
1 => "b"
],
"string" => "hello",
"multiline string" => """
this\n
is\n
a\multiline\n
string
"""
]

EOTXT;

yield array($expected, CliDumper::DUMP_COMMA_SEPARATOR);

$expected = <<<'EOTXT'
array:3 [
"array" => array:2 [
0 => "a",
1 => "b",
],
"string" => "hello",
"multiline string" => """
this\n
is\n
a\multiline\n
string
""",
]

EOTXT;

yield array($expected, CliDumper::DUMP_TRAILING_COMMA);
}

/**
* @requires extension xml
*/
Expand Down
0