| 148 |
lars |
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 OutputInterface $output;
|
|
|
25 |
private ?CliDumper $dumper;
|
|
|
26 |
private ?ClonerInterface $cloner;
|
|
|
27 |
private \Closure $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 ??= 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 ??= new VarCloner())->cloneVar($var)->withRefHandles(false), true));
|
|
|
41 |
};
|
|
|
42 |
} else {
|
|
|
43 |
$this->handler = function ($var): string {
|
|
|
44 |
return match (true) {
|
|
|
45 |
null === $var => 'null',
|
|
|
46 |
true === $var => 'true',
|
|
|
47 |
false === $var => 'false',
|
|
|
48 |
\is_string($var) => '"'.$var.'"',
|
|
|
49 |
default => rtrim(print_r($var, true)),
|
|
|
50 |
};
|
|
|
51 |
};
|
|
|
52 |
}
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
public function __invoke(mixed $var): string
|
|
|
56 |
{
|
|
|
57 |
return ($this->handler)($var);
|
|
|
58 |
}
|
|
|
59 |
}
|