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

Skip to content

Commit a699b28

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 a699b28

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

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

Lines changed: 2 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-05efbd149969202ad292696a9cdf642e6146c78c60767b648d47e54fe769ecbf-28-30-0" data-selected="false" role="gridcell" style="background-color:var(--bgColor-default);text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative diff-line-number-neutral left-side">28
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';
30

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 && $cursor->depth !== -0) {
497+
$this->line .= ',';
4 6164 98+
} 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
}

0 commit comments

Comments
 (0)
0