1
|
|
|
<?php |
2
|
|
|
namespace Xicrow\PhpDebug; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Class Debugger |
6
|
|
|
* |
7
|
|
|
* @package Xicrow\PhpDebug |
8
|
|
|
*/ |
9
|
|
|
class Debugger |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string|null |
13
|
|
|
*/ |
14
|
|
|
public static $documentRoot = null; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var bool |
18
|
|
|
*/ |
19
|
|
|
public static $showCalledFrom = true; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
public static $output = true; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private static $outputStyles = true; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $data |
33
|
|
|
* @param array $options |
34
|
|
|
* |
35
|
|
|
* @codeCoverageIgnore |
36
|
|
|
*/ |
37
|
|
|
public static function output($data, array $options = []) |
38
|
|
|
{ |
39
|
|
|
$options = array_merge([ |
40
|
|
|
'trace_offset' => 0, |
41
|
|
|
], $options); |
42
|
|
|
|
43
|
|
|
if (!self::$output || !is_string($data)) { |
44
|
|
|
return; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (php_sapi_name() == 'cli') { |
48
|
|
|
echo str_pad(' DEBUG ', 100, '-', STR_PAD_BOTH); |
49
|
|
|
echo "\n"; |
50
|
|
|
if (self::$showCalledFrom) { |
51
|
|
|
echo self::getCalledFrom($options['trace_offset'] + 2); |
52
|
|
|
echo "\n"; |
53
|
|
|
} |
54
|
|
|
echo $data; |
55
|
|
|
echo "\n"; |
56
|
|
|
} else { |
57
|
|
|
if (self::$showCalledFrom) { |
58
|
|
|
echo '<pre class="sf-dump sf-dump-called-from">'; |
59
|
|
|
echo self::getCalledFrom($options['trace_offset'] + 2); |
60
|
|
|
echo '</pre>'; |
61
|
|
|
} |
62
|
|
|
echo $data; |
63
|
|
|
if (self::$showCalledFrom && self::$outputStyles) { |
64
|
|
|
echo '<style type="text/css">'; |
65
|
|
|
echo 'pre.sf-dump.sf-dump-called-from { margin-bottom: 0; color: #AAA; }'; |
66
|
|
|
echo 'pre.sf-dump { margin-top: 0; }'; |
67
|
|
|
echo '</style>'; |
68
|
|
|
|
69
|
|
|
self::$outputStyles = false; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param mixed $data |
76
|
|
|
* @param array $options |
77
|
|
|
* |
78
|
|
|
* @codeCoverageIgnore |
79
|
|
|
*/ |
80
|
|
|
public static function debug($data, array $options = []) |
81
|
|
|
{ |
82
|
|
|
$options = array_merge([ |
83
|
|
|
'trace_offset' => 0, |
84
|
|
|
], $options); |
85
|
|
|
|
86
|
|
|
$cloner = new \Symfony\Component\VarDumper\Cloner\VarCloner(); |
87
|
|
|
if (php_sapi_name() == 'cli') { |
88
|
|
|
$dumper = new \Symfony\Component\VarDumper\Dumper\CliDumper(); |
89
|
|
|
} else { |
90
|
|
|
$dumper = new \Symfony\Component\VarDumper\Dumper\HtmlDumper(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
self::output($dumper->dump($cloner->cloneVar($data), true), $options); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $options |
98
|
|
|
* |
99
|
|
|
* @codeCoverageIgnore |
100
|
|
|
*/ |
101
|
|
|
public static function showTrace(array $options = []) |
102
|
|
|
{ |
103
|
|
|
$options = array_merge([ |
104
|
|
|
'trace_offset' => 0, |
105
|
|
|
'reverse' => false, |
106
|
|
|
], $options); |
107
|
|
|
|
108
|
|
|
$backtrace = ($options['reverse'] ? array_reverse(debug_backtrace()) : debug_backtrace()); |
109
|
|
|
|
110
|
|
|
$output = ''; |
111
|
|
|
$traceIndex = ($options['reverse'] ? 1 : count($backtrace)); |
112
|
|
|
foreach ($backtrace as $trace) { |
113
|
|
|
$output .= $traceIndex . ': '; |
114
|
|
|
$output .= self::getCalledFromTrace($trace); |
115
|
|
|
$output .= "\n"; |
116
|
|
|
|
117
|
|
|
$traceIndex += ($options['reverse'] ? 1 : -1); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
self::output($output); |
121
|
|
|
} |
122
|
1 |
|
|
123
|
1 |
|
/** |
124
|
|
|
* @param int $index |
125
|
1 |
|
* |
126
|
|
|
* @return string |
127
|
1 |
|
*/ |
128
|
1 |
|
public static function getCalledFrom($index = 0) |
129
|
1 |
|
{ |
130
|
1 |
|
$backtrace = debug_backtrace(); |
131
|
1 |
|
|
132
|
|
|
if (!isset($backtrace[$index])) { |
133
|
1 |
|
return 'Unknown trace with index: ' . $index; |
134
|
1 |
|
} |
135
|
1 |
|
|
136
|
1 |
|
return self::getCalledFromTrace($backtrace[$index]); |
137
|
1 |
|
} |
138
|
1 |
|
|
139
|
1 |
|
/** |
140
|
|
|
* @param array $trace |
141
|
1 |
|
* |
142
|
1 |
|
* @return string |
143
|
|
|
*/ |
144
|
1 |
|
public static function getCalledFromTrace($trace) |
145
|
1 |
|
{ |
146
|
1 |
|
$traceDescription = ''; |
147
|
1 |
|
if (empty($traceDescription) && isset($trace['file'])) { |
148
|
1 |
|
$traceDescription .= $trace['file'] . ' line ' . $trace['line']; |
149
|
|
|
$traceDescription = str_replace('\\', '/', $traceDescription); |
150
|
1 |
|
$traceDescription = (!empty(self::$documentRoot) ? substr($traceDescription, strlen(self::$documentRoot)) : $traceDescription); |
151
|
1 |
|
$traceDescription = trim($traceDescription, '/'); |
152
|
1 |
|
} |
153
|
1 |
|
if (empty($traceDescription) && isset($trace['function'])) { |
154
|
|
|
$traceDescription .= (isset($trace['class']) ? $trace['class'] : ''); |
155
|
1 |
|
$traceDescription .= (isset($trace['type']) ? $trace['type'] : ''); |
156
|
1 |
|
$traceDescription .= $trace['function'] . '()'; |
157
|
1 |
|
} |
158
|
|
|
if (empty($traceDescription)) { |
159
|
1 |
|
$traceDescription = 'Unable to get called from trace'; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
return $traceDescription; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: