Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1ef0751

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 1ef0751

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
@@ -107,6 +107,67 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
107107
);
108108
}
109109

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

0 commit comments

Comments
 (0)