Skip to content

Commit 0476eb5

Browse files
committed
feature #21653 [VarDumper] Added a way to print or not comma separator and/or trailing comma (lyrixx)
This PR was merged into the 3.3-dev branch. Discussion ---------- [VarDumper] Added a way to print or not comma separator and/or trailing comma | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - --- Usecase: Be able to display a dump on one line. It's already used in the following projets: https://github.com/bobthecow/psysh/blob/master/src/Psy/VarDumper/Dumper.php#L93-L95 Commits ------- 1ef0751 [VarDumper] Added a way to print or not comma separator and/or trailing comma
2 parents eb678a0 + 1ef0751 commit 0476eb5

File tree

3 files changed

+82
-4
lines changed

3 files changed

+82
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
2323
{
2424
const DUMP_LIGHT_ARRAY = 1;
2525
const DUMP_STRING_LENGTH = 2;
26+
const DUMP_COMMA_SEPARATOR = 4;
27+
const DUMP_TRAILING_COMMA = 8;
2628

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

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ public function dumpScalar(Cursor $cursor, $type, $value)
155155

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

158-
$this->dumpLine($cursor->depth, true);
158+
$this->endValue($cursor);
159159
}
160160

161161
/**
@@ -171,7 +171,7 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
171171
}
172172
if ('' === $str) {
173173
$this->line .= '""';
174-
$this->dumpLine($cursor->depth, true);
174+
$this->endValue($cursor);
175175
} else {
176176
$attr += array(
177177
'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0,
@@ -237,7 +237,11 @@ public function dumpString(Cursor $cursor, $str, $bin, $cut)
237237
$lineCut = 0;
238238
}
239239

240-
$this->dumpLine($cursor->depth, $i > $m);
240+
if ($i > $m) {
241+
$this->endValue($cursor);
242+
} else {
243+
$this->dumpLine($cursor->depth);
244+
}
241245
}
242246
}
243247
}
@@ -280,7 +284,7 @@ public function leaveHash(Cursor $cursor, $type, $class, $hasChild, $cut)
280284
{
281285
$this->dumpEllipsis($cursor, $hasChild, $cut);
282286
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
283-
$this->dumpLine($cursor->depth, true);
287+
$this->endValue($cursor);
284288
}
285289

286290
/**
@@ -486,4 +490,15 @@ protected function dumpLine($depth, $endOfValue = false)
486490
}
487491
parent::dumpLine($depth);
488492
}
493+
494+
protected function endValue(Cursor $cursor)
495+
{
496+
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
497+
$this->line .= ',';
498+
} elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) {
499+
$this->line .= ',';
500+
}
501+
502+
$this->dumpLine($cursor->depth, true);
503+
}
489504
}

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

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,67 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
108108
);
109109
}
110110

111+
/**
112+
* @dataProvider provideDumpWithCommaFlagTests
113+
*/
114+
public function testDumpWithCommaFlag($expected, $flags)
115+
{
116+
$dumper = new CliDumper(null, null, $flags);
117+
$dumper->setColors(false);
118+
$cloner = new VarCloner();
119+
120+
$var = array(
121+
'array' => array('a', 'b'),
122+
'string' => 'hello',
123+
'multiline string' => "this\nis\na\multiline\nstring",
124+
);
125+
126+
$dump = $dumper->dump($cloner->cloneVar($var), true);
127+
128+
$this->assertSame($expected, $dump);
129+
}
130+
131+
public function provideDumpWithCommaFlagTests()
132+
{
133+
$expected = <<<'EOTXT'
134+
array:3 [
135+
"array" => array:2 [
136+
0 => "a",
137+
1 => "b"
138+
],
139+
"string" => "hello",
140+
"multiline string" => """
141+
this\n
142+
is\n
143+
a\multiline\n
144+
string
145+
"""
146+
]
147+
148+
EOTXT;
149+
150+
yield array($expected, CliDumper::DUMP_COMMA_SEPARATOR);
151+
152+
$expected = <<<'EOTXT'
153+
array:3 [
154+
"array" => array:2 [
155+
0 => "a",
156+
1 => "b",
157+
],
158+
"string" => "hello",
159+
"multiline string" => """
160+
this\n
161+
is\n
162+
a\multiline\n
163+
string
164+
""",
165+
]
166+
167+
EOTXT;
168+
169+
yield array($expected, CliDumper::DUMP_TRAILING_COMMA);
170+
}
171+
111172
/**
112173
* @requires extension xml
113174
*/

0 commit comments

Comments
 (0)