8000 [VarDumper] Added a way to print or not comma separator and/or traili… · symfony/symfony@dc30f7d · GitHub
[go: up one dir, main page]

Skip to content

Commit dc30f7d

Browse files
committed
[VarDumper] Added a way to print or not comma separator and/or trailing comma
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
1 parent 0a3cd97 commit dc30f7d

File tree

3 files changed

+83
-4
lines changed

3 files changed

+83
-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: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,68 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
107107
);
108108
}
109109

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

0 commit comments

Comments
 (0)
0