1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\BlackBar; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @see: https://github.com/jacobstr/dumpling |
7
|
|
|
*/ |
8
|
|
|
class Dump |
9
|
|
|
{ |
10
|
|
|
public $depth; |
11
|
|
|
public $ignore; |
12
|
|
|
|
13
|
|
|
protected $level = 0; |
14
|
|
|
protected $result = []; |
15
|
|
|
protected $stack = []; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param mixed $value |
19
|
|
|
* @param int $depth |
20
|
|
|
* @return string |
21
|
|
|
*/ |
22
|
|
|
public function dump($value, $depth = 3, array $ignore = []) |
23
|
|
|
{ |
24
|
|
|
$this->depth = $depth; |
25
|
|
|
$this->ignore = $ignore; |
26
|
|
|
$this->reset(); |
27
|
|
|
$this->inspect($value); |
28
|
|
|
$result = rtrim(implode('', $this->result), "\n"); |
29
|
|
|
$this->reset(); |
30
|
|
|
return $result; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $key |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
protected function formatKey($key) |
38
|
|
|
{ |
39
|
|
|
$result = []; |
40
|
|
|
$result[] = str_repeat(' ', $this->level * 4).'['; |
41
|
|
|
if (is_string($key) && "\0" === $key[0]) { |
42
|
|
|
$keyParts = explode("\0", $key); |
43
|
|
|
$result[] = $keyParts[2].(('*' === $keyParts[1]) ? ':protected' : ':private'); |
44
|
|
|
} else { |
45
|
|
|
$result[] = $key; |
46
|
|
|
} |
47
|
|
|
$result[] = '] => '; |
48
|
|
|
return implode('', $result); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param mixed $subject |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
protected function inspect($subject) |
56
|
|
|
{ |
57
|
|
|
++$this->level; |
58
|
|
|
if ($subject instanceof \Closure) { |
59
|
|
|
$this->inspectClosure($subject); |
60
|
|
|
} elseif (is_object($subject)) { |
61
|
|
|
$this->inspectObject($subject); |
62
|
|
|
} elseif (is_array($subject)) { |
63
|
|
|
$this->inspectArray($subject); |
64
|
|
|
} else { |
65
|
|
|
$this->inspectPrimitive($subject); |
66
|
|
|
} |
67
|
|
|
--$this->level; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
|
|
protected function inspectArray(array $subject) |
74
|
|
|
{ |
75
|
|
|
if ($this->level > $this->depth) { |
76
|
|
|
$this->result[] = "Nested Array\n"; |
77
|
|
|
return; |
78
|
|
|
} |
79
|
|
|
if (empty($subject)) { |
80
|
|
|
$this->result[] = "Array ()\n"; |
81
|
|
|
return; |
82
|
|
|
} |
83
|
|
|
$this->result[] = "Array (\n"; |
84
|
|
|
foreach ($subject as $key => $val) { |
85
|
|
|
if (false === $this->isIgnoredKey($key)) { |
86
|
|
|
$this->result[] = str_repeat(' ', $this->level * 4).'['.$key.'] => '; |
87
|
|
|
$this->inspect($val); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
$this->result[] = str_repeat(' ', ($this->level - 1) * 4).")\n"; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
protected function inspectClosure(\Closure $subject) |
97
|
|
|
{ |
98
|
|
|
$reflection = new \ReflectionFunction($subject); |
99
|
|
|
$params = array_map(function ($param) { |
100
|
|
|
return ($param->isPassedByReference() ? '&$' : '$').$param->name; |
101
|
|
|
}, $reflection->getParameters()); |
102
|
|
|
$this->result[] = 'Closure ('.implode(', ', $params).') { ... }'."\n"; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @param object $subject |
107
|
|
|
* @return void |
108
|
|
|
*/ |
109
|
|
|
protected function inspectObject($subject) |
110
|
|
|
{ |
111
|
|
|
$classname = get_class($subject); |
112
|
|
|
if ($this->level > $this->depth) { |
113
|
|
|
$this->result[] = 'Nested '.$classname." Object\n"; |
114
|
|
|
return; |
115
|
|
|
} |
116
|
|
|
if ($subject instanceof \ArrayObject) { |
117
|
|
|
$this->result[] = $classname." ArrayObject (\n"; |
118
|
|
|
} else { |
119
|
|
|
$this->result[] = $classname." Object (\n"; |
120
|
|
|
$subject = (array) $subject; |
121
|
|
|
} |
122
|
|
|
foreach ($subject as $key => $val) { |
123
|
|
|
if (false === $this->isIgnoredKey($key)) { |
124
|
|
|
$this->result[] = $this->formatKey($key); |
125
|
|
|
$this->inspect($val); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
$this->result[] = str_repeat(' ', ($this->level - 1) * 4).")\n"; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param mixed $subject |
133
|
|
|
* @return void |
134
|
|
|
*/ |
135
|
|
|
protected function inspectPrimitive($subject) |
136
|
|
|
{ |
137
|
|
|
if (true === $subject) { |
138
|
|
|
$subject = '(bool) true'; |
139
|
|
|
} elseif (false === $subject) { |
140
|
|
|
$subject = '(bool) false'; |
141
|
|
|
} elseif (null === $subject) { |
142
|
|
|
$subject = '(null)'; |
143
|
|
|
} |
144
|
|
|
$this->result[] = $subject."\n"; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @param string $key |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
protected function isIgnoredKey($key) |
152
|
|
|
{ |
153
|
|
|
return in_array($key, $this->ignore); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
protected function reset() |
160
|
|
|
{ |
161
|
|
|
$this->level = 0; |
162
|
|
|
$this->result = []; |
163
|
|
|
$this->stack = []; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
|