8000 [Console] Add dumper · symfony/symfony@fc7465c · GitHub
[go: up one dir, main page]

Skip to content

Commit fc7465c

Browse files
ro0NLfabpot
authored andcommitted
[Console] Add dumper
1 parent 522594a commit fc7465c

File tree

5 files changed

+182
-0
lines changed

5 files changed

+182
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Helper;
13+
14+
use Symfony\Component\Console\Output\OutputInterface;
15+
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
16+
use Symfony\Component\VarDumper\Cloner\VarCloner;
17+
use Symfony\Component\VarDumper\Dumper\CliDumper;
18+
19+
/**
20+
* @author Roland Franssen <franssen.roland@gmail.com>
21+
*/
22+
final class Dumper
23+
{
24+
private $output;
25+
private $dumper;
26+
private $cloner;
27+
private $handler;
28+
29+
public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null)
30+
{
31+
$this->output = $output;
32+
$this->dumper = $dumper;
33+
$this->cloner = $cloner;
34+
35+
if (class_exists(CliDumper::class)) {
36+
$this->handler = function ($var): string {
37+
$dumper = $this->dumper ?? $this->dumper = new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
38+
$dumper->setColors($this->output->isDecorated());
39+
40+
return rtrim($dumper->dump(($this->cloner ?? $this->cloner = new VarCloner())->cloneVar($var)->withRefHandles(false), true));
41+
};
42+
} else {
43+
$this->handler = function ($var): string {
44+
switch (true) {
45+
case null === $var:
46+
return 'null';
47+
case true === $var:
48+
return 'true';
49+
case false === $var:
50+
return 'false';
51+
case \is_string($var):
52+
return '"'.$var.'"';
53+
default:
54+
return rtrim(print_r($var, true));
55+
}
56+
};
57+
}
58+
}
59+
60+
public function __invoke($var): string
61+
{
62+
return ($this->handler)($var);
63+
}
64+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Helper;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Bridge\PhpUnit\ClassExistsMock;
16+
use Symfony\Component\Console\Helper\Dumper;
17+
use Symfony\Component\Console\Output\OutputInterface;
18+
use Symfony\Component\VarDumper\Dumper\CliDumper;
19+
20+
class DumperNativeFallbackTest extends TestCase
21+
{
22+
public static function setUpBeforeClass()
23+
{
24+
ClassExistsMock::register(Dumper::class);
25+
ClassExistsMock::withMockedClasses([
26+
CliDumper::class => false,
27+
]);
28+
}
29+
30+
public static function tearDownAfterClass()
31+
{
32+
ClassExistsMock::withMockedClasses([]);
33+
}
34+
35+
/**
36+
* @dataProvider provideVariables
37+
*/
38+
public function testInvoke($variable, $primitiveString)
39+
{
40+
$dumper = new Dumper($this->getMockBuilder(OutputInterface::class)->getMock());
41+
42+
$this->assertSame($primitiveString, $dumper($variable));
43+
}
44+
45+
public function provideVariables()
46+
{
47+
return [
48+
[null, 'null'],
49+
[true, 'true'],
50+
[false, 'false'],
51+
[1, '1'],
52+
[-1.5, '-1.5'],
53+
['string', '"string"'],
54+
[[1, '2'], "Array\n(\n [0] => 1\n [1] => 2\n)"],
55+
[new \stdClass(), "stdClass Object\n(\n)"],
56+
];
57+
}
58+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Console\Tests\Helper;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\Console\Helper\Dumper;
16+
use Symfony\Component\Console\Output\OutputInterface;
17+
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
18+
19+
class DumperTest extends TestCase
20+
{
21+
use VarDumperTestTrait;
22+
23+
public static function setUpBeforeClass()
24+
{
25+
putenv('DUMP_LIGHT_ARRAY=1');
26+
putenv('DUMP_COMMA_SEPARATOR=1');
27+
}
28+
29+
public static function tearDownAfterClass()
30+
{
31+
putenv('DUMP_LIGHT_ARRAY');
32+
putenv('DUMP_COMMA_SEPARATOR');
33+
}
34+
35+
/**
36+
* @dataProvider provideVariables
37+
*/
38+
public function testInvoke($variable)
39+
{
40+
$dumper = new Dumper($this->getMockBuilder(OutputInterface::class)->getMock());
41+
42+
$this->assertDumpMatchesFormat($dumper($variable), $variable);
43+
}
44+
45+
public function provideVariables()
46+
{
47+
return [
48+
[null],
49+
[true],
50+
[false],
51+
[1],
52+
[-1.5],
53+
['string'],
54+
[[1, '2']],
55+
[new \stdClass()],
56+
];
57+
}
58+
}

src/Symfony/Component/Console/composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
"symfony/dependency-injection": "~3.4|~4.0",
2828
"symfony/lock": "~3.4|~4.0",
2929
"symfony/process": "~3.4|~4.0",
30+
"symfony/var-dumper": "^4.3",
3031
"psr/log": "~1.0"
3132
},
3233
"provide": {

src/Symfony/Component/VarDumper/Test/VarDumperTestTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ protected function getDump($data, $key = null, $filter = 0)
3333
{
3434
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
3535
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
36+
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;
3637

3738
$cloner = new VarCloner();
3839
$cloner->setMaxItems(-1);

0 commit comments

Comments
 (0)
0