1
|
|
|
<?php |
2
|
|
|
namespace nebula\component\debug\attach; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* 打印 |
6
|
|
|
*/ |
7
|
|
|
trait DumpTrait |
8
|
|
|
{ |
9
|
|
|
protected static $dumpStringLength = 80; |
10
|
|
|
|
11
|
|
|
protected static function objectToString(object $object, int $deep):string |
12
|
|
|
{ |
13
|
|
|
$objectName = get_class($object); |
14
|
|
|
$parameterString = ''; |
15
|
|
|
if ($deep > 0) { |
16
|
|
|
$vars = get_class_vars($objectName); |
17
|
|
|
foreach ($vars as $key => $value) { |
18
|
|
|
$parameterString.=static::valueToString($key, $value, $deep); |
19
|
|
|
} |
20
|
|
|
$parameterString.= static::objectGetter($objectName, $object, $deep); |
21
|
|
|
} else { |
22
|
|
|
$parameterString = '...'; |
23
|
|
|
} |
24
|
|
|
return $objectName.' {'.trim($parameterString, ',').'}'; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected static function objectGetter(string $objectName, object $object, int $deep) |
28
|
|
|
{ |
29
|
|
|
$methods = get_class_methods($objectName); |
30
|
|
|
$parameterString =''; |
31
|
|
|
foreach ($methods as $method) { |
32
|
|
|
if (strpos($method, 'get') === 0) { |
33
|
|
|
$methodRef = new \ReflectionMethod($object, $method); |
34
|
|
|
$ignore = \preg_match('/@ignore(-d|D)ump/i', $methodRef->getDocComment()??'') > 0; |
35
|
|
|
if (count($methodRef->getParameters()) === 0 && !$ignore) { |
36
|
|
|
$parameterString.=static::valueToString($method.'()', $object->$method(), $deep); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
return $parameterString; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
protected static function arrayToString(array $array, int $deep) |
44
|
|
|
{ |
45
|
|
|
$parameterString = ''; |
46
|
|
|
if ($deep > 0) { |
47
|
|
|
foreach ($array as $key => $value) { |
48
|
|
|
$parameterString.=static::valueToString($key, $value, $deep); |
49
|
|
|
} |
50
|
|
|
} else { |
51
|
|
|
$parameterString = '...'; |
52
|
|
|
} |
53
|
|
|
return '['.trim($parameterString, ',').']'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* 属性转换成字符串 |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param mixed $value |
61
|
|
|
* @param integer $deep |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
protected static function valueToString(string $key, $value, int $deep):string |
65
|
|
|
{ |
66
|
|
|
if (is_string($value) && strlen($value) > static::$dumpStringLength) { |
67
|
|
|
return $key.'='.json_encode(substr($value, 0, static::$dumpStringLength), JSON_UNESCAPED_UNICODE) .'... ,'; |
68
|
|
|
} elseif (is_bool($value)) { |
69
|
|
|
return $key.'='.($value ? 'true' : 'false').' ,'; |
70
|
|
|
} else { |
71
|
|
|
return $key.'='.self::parameterToString($value, $deep-1) .' ,'; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public static function parameterToString($object, int $deep=2) |
76
|
|
|
{ |
77
|
|
|
if (is_null($object)) { |
78
|
|
|
return 'NULL'; |
79
|
|
|
} elseif ($object instanceof \Exception) { |
80
|
|
|
return static::dumpException($object); |
81
|
|
|
} elseif (is_object($object)) { |
82
|
|
|
return static::objectToString($object, $deep); |
83
|
|
|
} elseif (is_array($object)) { |
84
|
|
|
return static::arrayToString($object, $deep); |
85
|
|
|
} |
86
|
|
|
return $object; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public static function dumpTrace(array $backtrace, bool $str=true, string $perfix='') |
90
|
|
|
{ |
91
|
|
|
$tracesConsole=[]; |
92
|
|
|
foreach ($backtrace as $trace) { |
93
|
|
|
$tracesConsole[]=static::buildTraceLine($trace); |
94
|
|
|
} |
95
|
|
|
if ($str) { |
96
|
|
|
$str=''; |
97
|
|
|
foreach ($tracesConsole as $trace_info) { |
98
|
|
|
$str.=$perfix.preg_replace('/\n/', "\n".$perfix."\t", $trace_info).PHP_EOL; |
99
|
|
|
} |
100
|
|
|
return $str; |
101
|
|
|
} |
102
|
|
|
return $tracesConsole; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected static function buildTraceLine(array $trace):string |
106
|
|
|
{ |
107
|
|
|
$line = ''; |
108
|
|
|
if (isset($trace['file'])) { |
109
|
|
|
$line = $trace['file'].':'.$trace['line']; |
110
|
|
|
} |
111
|
|
|
if (isset($trace['class'])) { |
112
|
|
|
$function = $trace['class'].$trace['type'].$trace['function']; |
113
|
|
|
} else { |
114
|
|
|
$function = $trace['function']; |
115
|
|
|
} |
116
|
|
|
$argsDump=''; |
117
|
|
|
if (!empty($trace['args'])) { |
118
|
|
|
foreach ($trace['args'] as $arg) { |
119
|
|
|
$argsDump.= self::parameterToString($arg) .','; |
120
|
|
|
} |
121
|
|
|
$argsDump = rtrim($argsDump, ','); |
122
|
|
|
} |
123
|
|
|
$line.=' '.$function.'('.$argsDump.')'; |
124
|
|
|
return $line; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public static function dumpException(\Exception $e) |
128
|
|
|
{ |
129
|
|
|
$dump = get_class($e).':'. $e->getMessage() .PHP_EOL; |
130
|
|
|
$dump.= 'At: ' . $e->getFile().':'.$e->getLine().PHP_EOL; |
131
|
|
|
$dump.= static::dumpTrace($e->getTrace()); |
132
|
|
|
return $dump; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|