-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathDumper.php
executable file
·68 lines (59 loc) · 1.73 KB
/
Dumper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
namespace BeyondCode\DumpServer;
use BeyondCode\DumpServer\FallbackDumper;
use Symfony\Component\VarDumper\Cloner\VarCloner;
use Symfony\Component\VarDumper\Dumper\CliDumper;
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
use Symfony\Component\VarDumper\Server\Connection;
class Dumper
{
/**
* The connection.
*
* @var \Symfony\Component\VarDumper\Server\Connection|null
*/
private $connection;
/**
* The fallback dumper to use if there is no active connection.
*
* @var \BeyondCode\DumpServer\FallbackDumper|null
*/
private $fallbackDumper;
/**
* Dumper constructor.
*
* @param \Symfony\Component\VarDumper\Server\Connection|null $connection
* @param \BeyondCode\DumpServer\FallbackDumper|null $fallbackDumper
* @return void
*/
public function __construct(Connection $connection = null, FallbackDumper $fallbackDumper = null)
{
$this->connection = $connection;
$this->fallbackDumper = $fallbackDumper;
}
/**
* Dump a value with elegance.
*
* @param mixed $value
* @return void
*/
public function dump($value)
{
if (class_exists(CliDumper::class)) {
$data = $this->createVarCloner()->cloneVar($value);
if ($this->connection === null || $this->connection->write($data) === false) {
$dumper = $this->fallbackDumper ?? (in_array(PHP_SAPI, ['cli', 'phpdbg']) ? new CliDumper : new HtmlDumper);
$dumper->dump($data);
}
} else {
var_dump($value);
}
}
/**
* @return VarCloner
*/
protected function createVarCloner(): VarCloner
{
return new VarCloner();
}
}