Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc52af28

Browse files
committed
feature#28898 [Console] Add dumper (ro0NL)
This PR was squashed before being merged into the 4.3-dev branch (closes#28898).Discussion----------[Console] Add dumper| Q | A| ------------- | ---| Branch? | master| Bug fix? | no| New feature? | yes| BC breaks? | no <!-- seehttps://symfony.com/bc -->| Deprecations? | no| Tests pass? | yes <!-- please add some, will be required by reviewers -->| Fixed tickets | #... <!-- #-prefixed issue number(s), if any -->| License | MIT| Doc PR |symfony/symfony-docs#10502This PR adds a new `Dumper` helper in the Console component. As there are 2 types of dumps- debug purpose (e.g. `dd()`, `dump()`)- output purpose (see#24208,#27684)For the latter we cannot use the global system (debug) dumper, i.e. `VarDumper::dump()`, we need something tied to the current output and dependency free. Here it is:```php$io = new SymfonyStyle($input, $output);$dumper = new Dumper($io);$io->writeln($dumper([-0.5, 0, 1]));$io->writeln($dumper(new \stdClass()));$io->writeln($dumper(123));$io->writeln($dumper('foo'));$io->writeln($dumper(null));$io->writeln($dumper(true));```With VarDumper comonent:![image](https://user-images.githubusercontent.com/1047696/47069483-4cc26f80-d1ef-11e8-902e-2f9b0f040f25.png)Without:![image](https://user-images.githubusercontent.com/1047696/47069517-6663b700-d1ef-11e8-9328-ae1db0b83d7e.png)>#27684 (comment) var-dumper is not a mandatory dep of fwb, can we do without?Now we can :)Commits-------fc7465c [Console] Add dumper
2 parents468bca8 +fc7465c commitc52af28

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+
namespaceSymfony\Component\Console\Helper;
13+
14+
useSymfony\Component\Console\Output\OutputInterface;
15+
useSymfony\Component\VarDumper\Cloner\ClonerInterface;
16+
useSymfony\Component\VarDumper\Cloner\VarCloner;
17+
useSymfony\Component\VarDumper\Dumper\CliDumper;
18+
19+
/**
20+
* @author Roland Franssen <franssen.roland@gmail.com>
21+
*/
22+
finalclass Dumper
23+
{
24+
private$output;
25+
private$dumper;
26+
private$cloner;
27+
private$handler;
28+
29+
publicfunction__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 =newCliDumper(null,null, CliDumper::DUMP_LIGHT_ARRAY | CliDumper::DUMP_COMMA_SEPARATOR);
38+
$dumper->setColors($this->output->isDecorated());
39+
40+
returnrtrim($dumper->dump(($this->cloner ??$this->cloner =newVarCloner())->cloneVar($var)->withRefHandles(false),true));
41+
};
42+
}else {
43+
$this->handler =function ($var):string {
44+
switch (true) {
45+
casenull ===$var:
46+
return'null';
47+
casetrue ===$var:
48+
return'true';
49+
casefalse ===$var:
50+
return'false';
51+
case\is_string($var):
52+
return'"'.$var.'"';
53+
default:
54+
returnrtrim(print_r($var,true));
55+
}
56+
};
57+
}
58+
}
59+
60+
publicfunction__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+
namespaceSymfony\Component\Console\Tests\Helper;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Bridge\PhpUnit\ClassExistsMock;
16+
useSymfony\Component\Console\Helper\Dumper;
17+
useSymfony\Component\Console\Output\OutputInterface;
18+
useSymfony\Component\VarDumper\Dumper\CliDumper;
19+
20+
class DumperNativeFallbackTestextends TestCase
21+
{
22+
publicstaticfunctionsetUpBeforeClass()
23+
{
24+
ClassExistsMock::register(Dumper::class);
25+
ClassExistsMock::withMockedClasses([
26+
CliDumper::class =>false,
27+
]);
28+
}
29+
30+
publicstaticfunctiontearDownAfterClass()
31+
{
32+
ClassExistsMock::withMockedClasses([]);
33+
}
34+
35+
/**
36+
* @dataProvider provideVariables
37+
*/
38+
publicfunctiontestInvoke($variable,$primitiveString)
39+
{
40+
$dumper =newDumper($this->getMockBuilder(OutputInterface::class)->getMock());
41+
42+
$this->assertSame($primitiveString,$dumper($variable));
43+
}
44+
45+
publicfunctionprovideVariables()
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+
namespaceSymfony\Component\Console\Tests\Helper;
13+
14+
usePHPUnit\Framework\TestCase;
15+
useSymfony\Component\Console\Helper\Dumper;
16+
useSymfony\Component\Console\Output\OutputInterface;
17+
useSymfony\Component\VarDumper\Test\VarDumperTestTrait;
18+
19+
class DumperTestextends TestCase
20+
{
21+
use VarDumperTestTrait;
22+
23+
publicstaticfunctionsetUpBeforeClass()
24+
{
25+
putenv('DUMP_LIGHT_ARRAY=1');
26+
putenv('DUMP_COMMA_SEPARATOR=1');
27+
}
28+
29+
publicstaticfunctiontearDownAfterClass()
30+
{
31+
putenv('DUMP_LIGHT_ARRAY');
32+
putenv('DUMP_COMMA_SEPARATOR');
33+
}
34+
35+
/**
36+
* @dataProvider provideVariables
37+
*/
38+
publicfunctiontestInvoke($variable)
39+
{
40+
$dumper =newDumper($this->getMockBuilder(OutputInterface::class)->getMock());
41+
42+
$this->assertDumpMatchesFormat($dumper($variable),$variable);
43+
}
44+
45+
publicfunctionprovideVariables()
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 =newVarCloner();
3839
$cloner->setMaxItems(-1);

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp