-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDumperObject.php
198 lines (183 loc) · 9.47 KB
/
DumperObject.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<?php
namespace sb\prettydumper;
class DumperObject implements DumperPlugin
{
public static $collapse_level = 6;
public static $max_level = 3;
public static $tab = ' ';
private static $onclick = "var s1=document.getElementById('dump_object_close_%s').style; var s2=document.getElementById('dump_object_open_%s').style; s1.display=s1.display=='none'?'inline':'none'; s2.display=s2.display=='none'?'inline':'none';";
public static function is($arg)
{
return is_object($arg);
}
public static function format($arg, $tab = 0)
{
static $id;
if (self::$max_level > $tab)
{
if (is_null($id)) $id = 0;
$id++;
if (self::$collapse_level < $tab)
{
$open = Dumper::getStyle('display:none;border:1px;');
$close = '';
}
else
{
$open = '';
$close = Dumper::getStyle('display:none;');
}
$class = new \ReflectionObject($arg);
$cparam = $class->isInternal() ? 'internal ' : 'user ';
$cparam .= $class->isFinal() ? 'final ' : '';
$cparam .= $class->isAbstract() ? 'abstract ' : '';
$cparam .= $class->isInterface() ? 'interface' : 'class';
$extends = self::getReflectionExtends($class->getParentClass());
$interfaces = $class->getInterfaces();
$extends_out = '';
if (count($extends) > 0)
{
foreach ($extends as $ename) $extends_out .= ' << ' . $ename;
$extends_out .= '';
}
$interfaces_out = '';
if (count($interfaces) > 0)
{
foreach ($interfaces as $interface) $interfaces_out .= ', '.$interface->getName();
$interfaces_out = ' : '.substr($interfaces_out, 2);
}
$output = "<span id=\"dump_object_close_$id\"$close>";
$output .= '<span'.Dumper::getStyle('cursor:pointer;'.Dumper::$style_object).' onclick="'.sprintf(self::$onclick, $id, $id).'">object['.get_class($arg).']</span> (...)'."\n";
$output .= '</span>';
$output .= "<span id=\"dump_object_open_$id\"$open>";
$output .= '<span'.Dumper::getStyle('cursor:pointer;'.Dumper::$style_object).' onclick="'.sprintf(self::$onclick, $id, $id).'">object['.$cparam.' '.get_class($arg).']</span>'.$extends_out.$interfaces_out."\n";
$output .= str_repeat(self::$tab, $tab)."(\n";
$tab++;
$constants = $class->getConstants();
if ( !empty($constants))
{
$output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_key) .'>CONSTANTS</span></br>';
foreach ($constants as $constName => $constValue)
{
$output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_object).'>'.
'constant '. $constName.'</span> -> '.Dumper::varDumpExtend($constValue, $tab);
}
$output .= '</br>';
}
$properties = $class->getProperties();
if (! empty($properties))
{
$output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_key) .'>PROPERTIES</span></br>';
foreach ($properties as $prop)
{
$pparam = $prop->isPublic() ? 'public ' : '';
$pparam .= $prop->isPrivate() ? 'private ' : '';
$pparam .= $prop->isProtected() ? 'protected ' : '';
$pparam .= $prop->isStatic() ? 'static ' : '';
if ($prop->isPublic())
{
$value = $prop->getValue($arg);
$error = false;
}
else if ($prop->isPrivate())
{
$prop->setAccessible(true);
$value = $prop->getValue($arg);
$prop->setAccessible(false);
$error = false;
}
else $error = true;
if (!$error) $output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_object).
'>'.$pparam.'property '.$prop->getName().'</span> -> '.Dumper::varDumpExtend($value, $tab);
else $output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_object).'>'
.$pparam.'property '.$prop->getName()."</span> -> WARNING! value is hidden\n";
}
$output .= '</br>';
}
if (count($extends) > 0)
{
foreach ($extends as $ename)
{
$eclass = new \ReflectionClass($ename);
$eproperties = $eclass->getProperties();
foreach ($eproperties as $eprop)
{
if ($eprop->isPrivate())
{
$epparam = $eprop->isStatic() ? 'static ' : '';
try
{
$eprop->setAccessible(true);
$evalue = $eprop->getValue($arg);
$eprop->setAccessible(false);
}
catch (Exception $e)
{
$evalue = null;
}
$output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_object).'>private '.$epparam.'property '.$eclass->getName().'::'.$eprop->getName().'</span> -> '.Dumper::varDumpExtend($evalue, $tab);
}
}
}
}
$metods = $class->getMethods();
if (! empty($metods))
{
$output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_key) .'>METHODS</span></br>';
foreach ($metods as $method)
{
$mparam = $method->isAbstract() ? 'abstract ' : '';
$mparam .= $method->isFinal() ? 'final ' : '';
$mparam .= $method->isPublic() ? 'public ' : '';
$mparam .= $method->isPrivate() ? 'private ' : '';
$mparam .= $method->isProtected() ? 'protected ' : '';
$mparam .= $method->isStatic() ? 'static ' : '';
$mparam .= $method->isConstructor() ? '<span'.Dumper::getStyle(Dumper::$style_key)
.'>constructor</span>' : ($method->getName() == '__destruct' ? '<span'.Dumper::getStyle
(Dumper::$style_key) .'>destructor</span>' : 'method');
$param_out = '';
$opion = 0;
foreach ($method->getParameters() as $i => $param)
{
$pr = $param->getClass();
if ($param->isOptional())
{
$param_out .= '[';
$opion++;
}
$param_out .= (is_object($pr) ? $pr->getName().' ' : '').'$'.$param->getName().', ';
}
$param_out = substr($param_out, 0, -2);
$param_out .= str_repeat(']', $opion);
$output .= str_repeat(self::$tab, $tab).'<span'.Dumper::getStyle(Dumper::$style_object).'>'.$mparam.' '.$method->getName().'('.$param_out . ')</span>'."\n";
}
}
$tab--;
$output .= str_repeat(self::$tab, $tab).")\n";
$tab--;
$output .= "</span>";
}
else $output = "WARNING: max dump level\n";
return $output;
}
public static function getReflectionExtends($ref_class, $arr = array())
{
if (is_object($ref_class))
{
$arr[] = $ref_class->getName();
$extends = $ref_class->getParentClass();
if ($extends) $arr = self::getReflectionExtends($extends, $arr);
}
return $arr;
}
public static function getReflectionInterfaces($ref_class, $arr = array())
{
if (is_object($ref_class))
{
$arr[] = $ref_class->getName();
$extends = $ref_class->getInterfaces();
if ($extends) $arr = self::getReflectionInterfaces($extends, $arr);
}
return $arr;
}
}