[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
This commit is contained in:
parent
0a3cd973ae
commit
1ef07515c1
@ -23,6 +23,8 @@ abstract class AbstractDumper implements DataDumperInterface, DumperInterface
|
|||||||
{
|
{
|
||||||
const DUMP_LIGHT_ARRAY = 1;
|
const DUMP_LIGHT_ARRAY = 1;
|
||||||
const DUMP_STRING_LENGTH = 2;
|
const DUMP_STRING_LENGTH = 2;
|
||||||
|
const DUMP_COMMA_SEPARATOR = 4;
|
||||||
|
const DUMP_TRAILING_COMMA = 8;
|
||||||
|
|
||||||
public static $defaultOutput = 'php://output';
|
public static $defaultOutput = 'php://output';
|
||||||
|
|
||||||
|
@ -155,7 +155,7 @@ class CliDumper extends AbstractDumper
|
|||||||
|
|
||||||
$this->line .= $this->style($style, $value, $attr);
|
$this->line .= $this->style($style, $value, $attr);
|
||||||
|
|
||||||
$this->dumpLine($cursor->depth, true);
|
$this->endValue($cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -171,7 +171,7 @@ class CliDumper extends AbstractDumper
|
|||||||
}
|
}
|
||||||
if ('' === $str) {
|
if ('' === $str) {
|
||||||
$this->line .= '""';
|
$this->line .= '""';
|
||||||
$this->dumpLine($cursor->depth, true);
|
$this->endValue($cursor);
|
||||||
} else {
|
} else {
|
||||||
$attr += array(
|
$attr += array(
|
||||||
'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0,
|
'length' => 0 <= $cut ? mb_strlen($str, 'UTF-8') + $cut : 0,
|
||||||
@ -237,7 +237,11 @@ class CliDumper extends AbstractDumper
|
|||||||
$lineCut = 0;
|
$lineCut = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->dumpLine($cursor->depth, $i > $m);
|
if ($i > $m) {
|
||||||
|
$this->endValue($cursor);
|
||||||
|
} else {
|
||||||
|
$this->dumpLine($cursor->depth);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -280,7 +284,7 @@ class CliDumper extends AbstractDumper
|
|||||||
{
|
{
|
||||||
$this->dumpEllipsis($cursor, $hasChild, $cut);
|
$this->dumpEllipsis($cursor, $hasChild, $cut);
|
||||||
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
|
$this->line .= Cursor::HASH_OBJECT === $type ? '}' : (Cursor::HASH_RESOURCE !== $type ? ']' : ($hasChild ? '}' : ''));
|
||||||
$this->dumpLine($cursor->depth, true);
|
$this->endValue($cursor);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -486,4 +490,15 @@ class CliDumper extends AbstractDumper
|
|||||||
}
|
}
|
||||||
parent::dumpLine($depth);
|
parent::dumpLine($depth);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
protected function endValue(Cursor $cursor)
|
||||||
|
{
|
||||||
|
if (self::DUMP_TRAILING_COMMA & $this->flags && 0 < $cursor->depth) {
|
||||||
|
$this->line .= ',';
|
||||||
|
} elseif (self::DUMP_COMMA_SEPARATOR & $this->flags && 1 < $cursor->hashLength - $cursor->hashIndex) {
|
||||||
|
$this->line .= ',';
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->dumpLine($cursor->depth, true);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -107,6 +107,67 @@ EOTXT
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideDumpWithCommaFlagTests
|
||||||
|
*/
|
||||||
|
public function testDumpWithCommaFlag($expected, $flags)
|
||||||
|
{
|
||||||
|
$dumper = new CliDumper(null, null, $flags);
|
||||||
|
$dumper->setColors(false);
|
||||||
|
$cloner = new VarCloner();
|
||||||
|
|
||||||
|
$var = array(
|
||||||
|
'array' => array('a', 'b'),
|
||||||
|
'string' => 'hello',
|
||||||
|
'multiline string' => "this\nis\na\multiline\nstring",
|
||||||
|
);
|
||||||
|
|
||||||
|
$dump = $dumper->dump($cloner->cloneVar($var), true);
|
||||||
|
|
||||||
|
$this->assertSame($expected, $dump);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideDumpWithCommaFlagTests()
|
||||||
|
{
|
||||||
|
$expected = <<<'EOTXT'
|
||||||
|
array:3 [
|
||||||
|
"array" => array:2 [
|
||||||
|
0 => "a",
|
||||||
|
1 => "b"
|
||||||
|
],
|
||||||
|
"string" => "hello",
|
||||||
|
"multiline string" => """
|
||||||
|
this\n
|
||||||
|
is\n
|
||||||
|
a\multiline\n
|
||||||
|
string
|
||||||
|
"""
|
||||||
|
]
|
||||||
|
|
||||||
|
EOTXT;
|
||||||
|
|
||||||
|
yield array($expected, CliDumper::DUMP_COMMA_SEPARATOR);
|
||||||
|
|
||||||
|
$expected = <<<'EOTXT'
|
||||||
|
array:3 [
|
||||||
|
"array" => array:2 [
|
||||||
|
0 => "a",
|
||||||
|
1 => "b",
|
||||||
|
],
|
||||||
|
"string" => "hello",
|
||||||
|
"multiline string" => """
|
||||||
|
this\n
|
||||||
|
is\n
|
||||||
|
a\multiline\n
|
||||||
|
string
|
||||||
|
""",
|
||||||
|
]
|
||||||
|
|
||||||
|
EOTXT;
|
||||||
|
|
||||||
|
yield array($expected, CliDumper::DUMP_TRAILING_COMMA);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @requires extension xml
|
* @requires extension xml
|
||||||
*/
|
*/
|
||||||
|
Reference in New Issue
Block a user