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

Skip to content

Commit 60c2a18

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 60c2a18

File tree

3 files changed

+108
-5
lines changed

3 files changed

+108
-5
lines changed

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

Lines changed: 2 additions & 0 deletions
10000
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: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,92 @@ class: "Symfony\Component\VarDumper\Tests\CliDumperTest"
101101
b"bin-key-é" => ""
102102
]
103103
104+
EOTXT
105+
,
106+
$out
107+
);
108+
}
109+
110+
public function testGetWithCommaFlag()
111+
{
112+
require __DIR__.'/Fixtures/dumb-var.php';
B41A 113+
114+
$dumper = new CliDumper('php://output', null, CliDumper::DUMP_COMMA_SEPARATOR);
115+
$dumper->setColors(false);
116+
$cloner = new VarCloner();
117+
$cloner->addCasters(array(
118+
':stream' => function ($res, $a) {
119+
unset($a['uri'], $a['wrapper_data']);
120+
121+
return $a;
122+
},
123+
));
124+
$data = $cloner->cloneVar($var);
125+
126+
ob_start();
127+
$dumper->dump($data);
128+
$out = ob_get_clean();
129+
$out = preg_replace('/[ \t]+$/m', '', $out);
130+
131+
$intMax = PHP_INT_MAX;
132+
$res = (int) $var['res'];
133+
$r = defined('HHVM_VERSION') ? '' : '#%d';
134+
135+
$this->assertStringMatchesFormat(
136+
<<<EOTXT
137+
array:24 [
138+
"number" => 1,
139+
0 => &1 null,
140+
"const" => 1.1,
141+
1 => true,
142+
2 => false,
143+
3 => NAN,
144+
4 => INF,
145+
5 => -INF,
146+
6 => {$intMax},
147+
"str" => "déjà\\n",
148+
7 => b"é\\x00",
149+
"[]" => [],
150+
"res" => stream resource {@{$res}
151+
%A wrapper_type: "plainfile",
152+
stream_type: "STDIO",
153+
mode: "r",
154+
unread_bytes: 0,
155+
seekable: true,
156+
%A options: []
157+
},
158+
"obj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d
159+
+foo: "foo",
160+
+"bar": "bar"
161+
},
162+
"closure" => Closure {{$r}
163+
class: "Symfony\Component\VarDumper\Tests\CliDumperTest",
164+
this: Symfony\Component\VarDumper\Tests\CliDumperTest {{$r} …},
165+
parameters: {
166+
\$a: {},
167+
&\$b: {
168+
typeHint: "PDO",
169+
default: null
170+
}
171+
},
172+
file: "{$var['file']}",
173+
line: "{$var['line']} to {$var['line']}"
174+
},
175+
"line" => {$var['line']},
176+
"nobj" => array:1 [
177+
0 => &3 {#%d}
178+
],
179+
"recurs" => &4 array:1 [
180+
0 => &4 array:1 [&4]
181+
],
182+
8 => &1 null,
183+
"sobj" => Symfony\Component\VarDumper\Tests\Fixture\DumbFoo {#%d},
184+
"snobj" => &3 {#%d},
185+
"snobj2" => {#%d},
186+
"file" => "{$var['file']}",
187+
b"bin-key-é" => ""
188+
]
189+
104190
EOTXT
105191
,
106192
$out
@@ -274,7 +360,7 @@ public function testThrowingCaster()
274360
%sTwig.php:2: {
275361
: foo bar
276362
: twig source
277-
:
363+
:
278364
}
279365
%sTemplate.php:%d: {
280366
: try {

0 commit comments

Comments
 (0)
0