1 | <?php |
||
2 | |||
3 | namespace HexMakina\Debugger |
||
4 | { |
||
5 | trait Debugger |
||
6 | { |
||
7 | public function __debugInfo() |
||
8 | { |
||
9 | return [json_encode(get_object_vars($this))]; |
||
10 | } |
||
11 | |||
12 | public static function init() |
||
13 | { |
||
14 | // just to load the class, required to get the shortcuts defined in namespace \ |
||
15 | } |
||
16 | |||
17 | public static function display_errors($error_message=null) |
||
18 | { |
||
19 | $should_display = ini_get('display_errors') == '1'; |
||
20 | |||
21 | if($should_display && !empty($error_message)) |
||
22 | echo('<pre style="z-index:9999; background-color:#FFF; color:#000; padding:0.5em; font-size:0.7em; margin:0 0 1em 0; font-family:courier;">'.$error_message.'</pre>'); |
||
23 | } |
||
24 | |||
25 | // ----------------------------------------------------------- visual dump (depends on env) |
||
26 | public static function vd($var, $var_name = null, $full_backtrace = false) |
||
27 | { |
||
28 | self::display_errors(self::dump($var, $var_name, $full_backtrace)); |
||
29 | } |
||
30 | |||
31 | // ----------------------------------------------------------- visual dump and DIE |
||
32 | public static function dd($var, $var_name = null, $full_backtrace = true) |
||
33 | { |
||
34 | self::vd($var, $var_name, $full_backtrace); |
||
35 | die; |
||
36 | } |
||
37 | |||
38 | |||
39 | // ----------------------------------------------------------- dump on variable type (Throwables, array, anything else) |
||
40 | public static function dump($var, $var_name = null, $full_backtrace = true) |
||
41 | { |
||
42 | $short_file_path_length = 5; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
43 | |||
44 | if(is_object($var) && (is_subclass_of($var, 'Error') || is_subclass_of($var, 'Exception'))) |
||
45 | { |
||
46 | $backtrace = $var->getTrace(); |
||
47 | $full_backtrace = true; |
||
48 | $var_dump = self::format_throwable_message(get_class($var), $var->getCode(), $var->getFile(), $var->getLine(), $var->getMessage()); |
||
49 | } |
||
50 | else |
||
51 | { |
||
52 | $backtrace = debug_backtrace(); |
||
53 | |||
54 | ob_start(); |
||
55 | var_dump($var); |
||
56 | $var_dump = ob_get_clean(); |
||
57 | } |
||
58 | |||
59 | return PHP_EOL."*******".(empty($var_name) ? '' : " ($var_name) ")."*******".PHP_EOL.self::format_trace($backtrace, $full_backtrace).PHP_EOL.$var_dump; |
||
60 | } |
||
61 | |||
62 | // ----------------------------------------------------------- formatting |
||
63 | |||
64 | // ----------------------------------------------------------- formatting : first line of \Throwable-based error |
||
65 | public static function format_throwable_message($class, $code, $file, $line, $message) |
||
66 | { |
||
67 | return sprintf(PHP_EOL.'%s (%d) in file %s:%d'.PHP_EOL.'%s', $class, $code, self::format_file($file), $line, $message); |
||
68 | } |
||
69 | |||
70 | // ----------------------------------------------------------- formatting : shorten file path to [self::REDUCE_FILE_PATH_DEPTH_TO] elements |
||
71 | public static function format_file($file,$reduce_file_depth_to = 5) |
||
72 | { |
||
73 | return implode('/', array_slice(explode('/', $file), -$reduce_file_depth_to, $reduce_file_depth_to)); |
||
74 | } |
||
75 | |||
76 | // ----------------------------------------------------------- formatting : nice backtrace |
||
77 | public static function format_trace($traces, $full_backtrace) |
||
78 | { |
||
79 | $formated_traces = []; |
||
80 | |||
81 | foreach($traces as $depth => $trace) |
||
82 | { |
||
83 | $function_name = $trace['function'] ?? '?'; |
||
84 | |||
85 | $class_name = $trace['class'] ?? '?'; |
||
86 | |||
87 | if(($function_name === 'dump' || $function_name === 'vd' || $function_name === 'dd') && $class_name === __CLASS__) |
||
88 | continue; |
||
89 | |||
90 | if($function_name !== 'vd' && $function_name !== 'dd' && $function_name !== 'vdt' && $function_name !== 'ddt' && isset($trace['args'])) |
||
91 | { |
||
92 | $args = []; |
||
93 | foreach($trace['args'] as $arg) |
||
94 | { |
||
95 | if(is_null($arg)) |
||
96 | $args[]= 'null'; |
||
97 | elseif(is_bool($arg)) |
||
98 | $args[]= $arg === true ? 'bool:true' : 'bool:false'; |
||
99 | elseif(is_string($arg) || is_numeric($arg)) |
||
100 | $args[]= $arg; |
||
101 | elseif(is_object($arg)) |
||
102 | $args[]= get_class($arg); |
||
103 | elseif(is_array($arg)) |
||
104 | $args[]= 'Array #'.count($arg); |
||
105 | else |
||
106 | { |
||
107 | var_dump($arg); |
||
108 | $args[] = '!!OTHER!!'; |
||
109 | } |
||
110 | } |
||
111 | $args = implode(', ', $args); |
||
112 | } |
||
113 | else |
||
114 | $args = microtime(true); |
||
115 | |||
116 | $call_file = isset($trace['file']) ? pathinfo($trace['file'], PATHINFO_BASENAME) : '?'; |
||
117 | $call_line = $trace['line'] ?? '?'; |
||
118 | |||
119 | $formated_traces []= sprintf('[%-23.23s %3s] %'.($depth*3).'s%s%s(%s)', $call_file, $call_line,' ', "$class_name::", $function_name, $args); |
||
120 | |||
121 | if($full_backtrace === false) |
||
122 | break; |
||
123 | } |
||
124 | |||
125 | return implode(PHP_EOL, array_reverse($formated_traces)); |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | |||
130 | namespace |
||
131 | { |
||
132 | if(!function_exists('vd')) { |
||
133 | function vd($var, $var_name=null){ return \HexMakina\Debugger\Debugger::vd($var, $var_name, false);} |
||
134 | } |
||
135 | if(!function_exists('dd')) { |
||
136 | function dd($var, $var_name=null){ return \HexMakina\Debugger\Debugger::dd($var, $var_name, false);} |
||
137 | } |
||
138 | if(!function_exists('vdt')) { |
||
139 | function vdt($var, $var_name=null){ return \HexMakina\Debugger\Debugger::vd($var, $var_name, true);} |
||
140 | } |
||
141 | if(!function_exists('ddt')) { |
||
142 | function ddt($var, $var_name=null){ return \HexMakina\Debugger\Debugger::dd($var, $var_name, true);} |
||
143 | } |
||
144 | } |
||
145 | ?> |
||
0 ignored issues
–
show
It is not recommended to use PHP's closing tag
?> in files other than templates.
Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore. A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever. ![]() |
|||
146 |