Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Add dumper #28898

Merged
merged 1 commit into from
Mar 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions src/Symfony/Component/Console/Helper/Dumper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Helper;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\VarDumper\Cloner\ClonerInterface;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;

/**
* @author Roland Franssen <franssen.roland@gmail.com>
*/
final class Dumper
{
private $output;
private $dumper;
private $cloner;
private $handler;

public function __construct(OutputInterface $output, CliDumper $dumper = null, ClonerInterface $cloner = null)
{
$this->output = $output;
$this->dumper = $dumper;
$this->cloner = $cloner;

if (class_exists(CliDumper::class)) {
$this->handler = function ($var): string {
$dumper = $this->dumper ?? $this->dumper = new CliDumper(null, null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
$dumper->setColors($this->output->isDecorated());
ro0NL marked this conversation as resolved.
Show resolved Hide resolved

return rtrim($dumper->dump(($this->cloner ?? $this->cloner = new VarCloner())->cloneVar($var)->withRefHandles(false), true));
};
} else {
$this->handler = function ($var): string {
ro0NL marked this conversation as resolved.
Show resolved Hide resolved
switch (true) {
case null === $var:
return 'null';
case true === $var:
return 'true';
case false === $var:
return 'false';
case \is_string($var):
return '"'.$var.'"';
default:
return rtrim(print_r($var, true));
}
};
}
}

public function __invoke($var): string
ro0NL marked this conversation as resolved.
Show resolved Hide resolved
{
return ($this->handler)($var);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Helper;

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\PhpUnit\ClassExistsMock;
use Symfony\Component\Console\Helper\Dumper;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\VarDumper\Dumper\CliDumper;

class DumperNativeFallbackTest extends TestCase
{
public static function setUpBeforeClass()
{
ClassExistsMock::register(Dumper::class);
ClassExistsMock::withMockedClasses([
CliDumper::class => false,
]);
}

public static function tearDownAfterClass()
{
ClassExistsMock::withMockedClasses([]);
}

/**
* @dataProvider provideVariables
*/
public function testInvoke($variable, $primitiveString)
{
$dumper = new Dumper($this->getMockBuilder(OutputInterface::class)->getMock());

$this->assertSame($primitiveString, $dumper($variable));
}

public function provideVariables()
{
return [
[null, 'null'],
[true, 'true'],
[false, 'false'],
[1, '1'],
[-1.5, '-1.5'],
['string', '"string"'],
[[1, '2'], "Array\n(\n [0] => 1\n [1] => 2\n)"],
[new \stdClass(), "stdClass Object\n(\n)"],
];
}
}
58 changes: 58 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/DumperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Console\Tests\Helper;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Helper\Dumper;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;

class DumperTest extends TestCase
{
use VarDumperTestTrait;

public static function setUpBeforeClass()
{
putenv('DUMP_LIGHT_ARRAY=1');
putenv('DUMP_COMMA_SEPARATOR=1');
}

public static function tearDownAfterClass()
{
putenv('DUMP_LIGHT_ARRAY');
putenv('DUMP_COMMA_SEPARATOR');
}

/**
* @dataProvider provideVariables
*/
public function testInvoke($variable)
{
$dumper = new Dumper($this->getMockBuilder(OutputInterface::class)->getMock());

$this->assertDumpMatchesFormat($dumper($variable), $variable);
}

public function provideVariables()
{
return [
[null],
[true],
[false],
[1],
[-1.5],
['string'],
[[1, '2']],
[new \stdClass()],
];
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"symfony/dependency-injection": "~3.4|~4.0",
"symfony/lock": "~3.4|~4.0",
"symfony/process": "~3.4|~4.0",
"symfony/var-dumper": "^4.3",
"psr/log": "~1.0"
},
"provide": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ protected function getDump($data, $key = null, $filter = 0)
{
$flags = getenv('DUMP_LIGHT_ARRAY') ? CliDumper::DUMP_LIGHT_ARRAY : 0;
$flags |= getenv('DUMP_STRING_LENGTH') ? CliDumper::DUMP_STRING_LENGTH : 0;
$flags |= getenv('DUMP_COMMA_SEPARATOR') ? CliDumper::DUMP_COMMA_SEPARATOR : 0;

$cloner = new VarCloner();
$cloner->setMaxItems(-1);
Expand Down