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

Skip to content

Commit 8ad26a8

Browse files
committed
[VarDumper] Fix CliDumper coloration
When using AbstractDumper::DUMP_LIGHT_ARRAY
1 parent b30f4c1 commit 8ad26a8

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,8 @@ public function enterHash(Cursor $cursor, $type, $class, $hasChild)
268268
} elseif (Cursor::HASH_RESOURCE === $type) {
269269
$prefix = $this->style('note', $class.' resource').($hasChild ? ' {' : ' ');
270270
} else {
271-
$prefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? $this->style('note', 'array:'.$class).' [' : '[';
271+
$unstyledPrefix = $class && !(self::DUMP_LIGHT_ARRAY & $this->flags) ? 'array:'.$class : '';
272+
$prefix = $this->style('note', $unstyledPrefix).($unstyledPrefix ? ' [' : '[');
272273
}
273274

274275
if ($cursor->softRefCount || 0 < $cursor->softRefHandle) {

src/Symfony/Component/VarDumper/Tests/Dumper/CliDumperTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\VarDumper\Cloner\VarCloner;
16+
use Symfony\Component\VarDumper\Dumper\AbstractDumper;
1617
use Symfony\Component\VarDumper\Dumper\CliDumper;
1718
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
1819
use Twig\Environment;
@@ -572,6 +573,57 @@ public function testIncompleteClass()
572573
);
573574
}
574575

576+
public function provideDumpArrayWithColor()
577+
{
578+
yield [
579+
['foo' => 'bar'],
580+
0,
581+
<<<EOTXT
582+
\e[0;38;5;208m\e[38;5;38marray:1\e[0;38;5;208m [\e[m
583+
\e[0;38;5;208m"\e[38;5;113mfoo\e[0;38;5;208m" => "\e[1;38;5;113mbar\e[0;38;5;208m"\e[m
584+
\e[0;38;5;208m]\e[m
585+
586+
EOTXT
587+
];
588+
589+
yield [[], AbstractDumper::DUMP_LIGHT_ARRAY, "\e[0;38;5;208m\e[38;5;38m\e[0;38;5;208m[]\e[m\n"];
590+
591+
yield [
592+
['foo' => 'bar'],
593+
AbstractDumper::DUMP_LIGHT_ARRAY,
594+
<<<EOTXT
595+
\e[0;38;5;208m\e[38;5;38m\e[0;38;5;208m[\e[m
596+
\e[0;38;5;208m"\e[38;5;113mfoo\e[0;38;5;208m" => "\e[1;38;5;113mbar\e[0;38;5;208m"\e[m
597+
\e[0;38;5;208m]\e[m
598+
599+
EOTXT
600+
];
601+
602+
yield [[], 0, "\e[0;38;5;208m\e[38;5;38m\e[0;38;5;208m[]\e[m\n"];
603+
}
604+
605+
/**
606+
* @dataProvider provideDumpArrayWithColor
607+
*/
608+
public function testDumpArrayWithColor($value, $flags, $expectedOut)
609+
{
610+
if ('\\' === \DIRECTORY_SEPARATOR) {
611+
$this->markTestSkipped('Windows console does not support coloration');
612+
}
613+
614+
$out = '';
615+
$dumper = new CliDumper(function ($line, $depth) use (&$out) {
616+
if ($depth >= 0) {
617+
$out .= str_repeat(' ', $depth).$line."\n";
618+
}
619+
}, null, $flags);
620+
$dumper->setColors(true);
621+
$cloner = new VarCloner();
622+
$dumper->dump($cloner->cloneVar($value));
623+
624+
$this->assertSame($expectedOut, $out);
625+
}
626+
575627
private function getSpecialVars()
576628
{
577629
foreach (array_keys($GLOBALS) as $var) {

0 commit comments

Comments
 (0)