-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheasydump.php
executable file
·448 lines (405 loc) · 15.4 KB
/
easydump.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
<?php
/**
* Utility class to easily and beautifully dump PHP variables
* the functions d() and dd() where inspired Kint
*
* @author Yosko <[email protected]>
* @version 0.8
* @copyright none: free and opensource
* @link https://github.com/yosko/easydump
*/
class EasyDump
{
//display configuration
public const IS_STATIC = 16;
public const IS_PUBLIC = 1;
public const IS_PROTECTED = 2;
public const IS_PRIVATE = 4;
public static array $config = array(
'showCall' => true, //true to show file name and line number of each call to EasyDump
'showTime' => true, //true to show the execution date, time and microsecond of each call
'showVarNames' => true, //true to show names of the given variables
'showSource' => false, //true to show the code of each PHP call to EasyDump
'color' => array( //default theme based on Earthsong by daylerees
'text' => '#EBD1B7',
'border' => '#7A7267',
'background' => '#36312c',
'name' => '#F8BB39',
'type' => '#DB784D',
'value' => '#95CC5E'
)
);
private static array $objectsDisplayed;
/**
* For debug purpose only
* @param mixed $variables any number of variables of any type
* @throws Exception
*/
public static function debug()
{
$call = null;
self::$objectsDisplayed = [];
$trace = debug_backtrace();
if (self::$config['showCall'] || self::$config['showVarNames'] || self::$config['showSource']) {
$call = self::readCall($trace);
}
echo '<pre class="easydump" style="display: block !important; border: 0.5em solid ' . self::$config['color']['border'] . '; color: ' . self::$config['color']['text'] . '; background-color: ' . self::$config['color']['background'] . '; margin: 0; padding: 0.5em; white-space: pre-wrap;font-family:\'DejaVu Sans Mono\',monospace;font-size:11px;text-align:left;min-width:300px;">';
//show file and line
if (self::$config['showCall']) {
self::showCall($call);
}
//show file and line
if (self::$config['showTime']) {
echo self::microDateTime() . "\r\n";
}
//show PHP source of the call
if (self::$config['showSource']) {
self::showSource($call);
}
//get the variable names (if available)
$varNames = [];
if (self::$config['showVarNames']) {
$varNames = self::guessVarName($call);
}
//show the values (with variable names if available)
foreach ($trace[0]['args'] as $k => $v) {
self::showVar((self::$config['showVarNames'] ? $varNames[$k] : $k), $v);
}
echo '</pre>';
}
/**
* Read information from the backtrace and the PHP file about the call to EasyDump
* This function uses SplFileObject, only available on PHP 5.1.0+
*
* @param array $trace backtrace executed PHP code
* @return array information about the call
*/
protected static function readCall(array $trace): array
{
//echo '<pre>'; var_dump(count($trace), $trace);
//called de()
if (count($trace) >= 3
&& $trace[1]['function'] === 'debugExit'
&& $trace[2]['function'] === 'de'
) {
$rank = 2;
//called EasyDump::debugExit() or d()
} elseif (count($trace) >= 2
&& ($trace[1]['function'] === 'debugExit'
|| $trace[1]['function'] === 'd')
) {
$rank = 1;
//called EasyDump::debug()
} else {
$rank = 0;
}
$line = --$trace[$rank]['line'];
$file = new SplFileObject($trace[$rank]['file']);
$file->seek($line);
$call = trim($file->current());
$callMultiline = $file->current();
//read the PHP file backward to the beginning of the call
$regex = '/' . $trace[$rank]['function'] . '\((.*)\);/';
while (!preg_match($regex, $call, $match)) {
$file->seek(--$line);
$call = trim($file->current()) . $call;
$callMultiline = $file->current() . $callMultiline;
}
$call = $match[1];
$callMultiline = htmlentities($callMultiline);
return array(
'code' => $call,
'formattedCode' => $callMultiline,
'rank' => $rank,
'line' => $line + 1,
'file' => $trace[$rank]['file']
);
}
/**
* Display the filename and line number where EasyDump was called
* @param array $call information about the call
*/
protected static function showCall(array $call)
{
echo "<span style=\"color:" . self::$config['color']['type'] . ";\">File \"" . $call['file'] . "\" line " . $call['line'] . ":</span>\r\n";
}
protected static function microDateTime(): string
{
list($microSec, $timeStamp) = explode(' ', microtime());
return date('Y-m-d H:i:s.', $timeStamp) . ($microSec * 1000000);
}
/**
* Display the PHP code where EasyDump was called
* useful for tracking lots of different calls with values/functions as parameters
* @param array $call information about the call
*/
protected static function showSource(array $call)
{
echo $call['formattedCode']
. "\r\n"
. "<span style=\"color:" . self::$config['color']['type'] . ";\">Results:</span>"
. "\r\n";
}
/**
* Get the variable names used in the function call
*
* @param array $call
* @return array list of variable names (if available)
*/
protected static function guessVarName(array $call): array
{
$varNames = array();
$results = self::parse($call['code']);
foreach ($results as $k => $v) {
$processString = trim($v);
if (preg_match('/^\$/', $processString)) {
$varNames[] = $processString;
} elseif (is_numeric($processString)
|| $processString[0] === "'"
|| $processString[0] === '"'
|| strpos($processString, 'array') === 0
) {
//TODO: not working for empty string
$varNames[] = '[value]';
} elseif (preg_match('([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)', $processString, $matches)) {
$varNames[] = $processString;
} else {
$varNames[] = '[unknown]';
}
}
return $varNames;
}
/**
* Pars PHP code to extract comma separated elements into an array
* @param string $code PHP code
* @return array list of elements
*/
protected static function parse(string $code): array
{
$names = array();
$currentName = '';
$depth = 0;
$escapeNext = false;
$delimiter = array(
'(' => ')',
'[' => ']',
'{' => '}',
);
$inQuotes = '';
$inDelimiter = '';
$len = strlen($code);
for ($i = 0; $i < $len; $i++) {
$stackChar = true;
if (!$escapeNext) {
//escape char inside a string between single/double quotes
if (!empty($inQuotes) && $code[$i] === '\\') {
$escapeNext = true;
//leaving a quoted string
} elseif (!empty($inQuotes) && $code[$i] === $inQuotes) {
$inQuotes = '';
//entering a quoted string
} elseif (empty($inQuotes) && ($code[$i] === '\'' || $code[$i] === '"')) {
$inQuotes = $code[$i];
//recursive use of delimiter, add a level
} elseif (!empty($inDelimiter) && $code[$i] === $inDelimiter) {
$depth++;
//recursive use of delimiter, remove a level
} elseif (!empty($inDelimiter) && $code[$i] === $delimiter[$inDelimiter]) {
$depth--;
//leaving the parent delimiter
if ($depth === 0) {
$inDelimiter = '';
}
//entering a parent delimiter
} elseif (empty($inDelimiter) && array_key_exists($code[$i], $delimiter)) {
$inDelimiter = $code[$i];
$depth++;
//a root, breaking comma
} elseif (empty($inDelimiter) && empty($inQuotes) && $code[$i] === ',') {
$names[] = $currentName;
$currentName = '';
$stackChar = false;
}
} else {
$escapeNext = false;
}
//add the char to the currently processed name
if ($stackChar) {
$currentName .= $code[$i];
}
}
//add the last name to the array
$names[] = $currentName;
return $names;
}
/**
* For debug purpose only, used by debug()
* Recursive (for arrays) function to display variable in a nice formatted way
*
* @param string $name name/value of the variable's index
* @param mixed $value value to display
* @param int $level for indentation purpose, used in recursion
* @param int $modifier IS_PUBLIC, IS_PRIVATE, IS_PROTECTED, IS_STATIC
* @throws Exception
*/
protected static function showVar(string $name, $value, $level = 0, $modifier = self::IS_PUBLIC, $declaredType = '')
{
// deprecated: used to be an (unused) argument
$dumpArray = false;
$indent = " ";
for ($lvl = 0; $lvl < $level; $lvl++) {
echo $indent;
}
echo '<span style="color:' . self::$config['color']['name'] . ';">' . ($level === 0 ? $name : (is_string($name) ? '"' . $name . '"' : '[' . $name . ']')) . " </span>";
echo '<span style="color:' . self::$config['color']['type'] . ';">(';
if ($modifier) {
if ($modifier >= self::IS_STATIC) {
$modifier -= self::IS_STATIC;
echo 'static ';
}
echo $modifier == self::IS_PRIVATE ? 'private ' : ($modifier == self::IS_PROTECTED ? 'protected ' : '');
}
echo(is_object($value) ? get_class($value) : ($declaredType ?: gettype($value)));
echo ")</span>\t= ";
if (!$dumpArray && $level <= 5 && self::isTraversable($value)) {
$count = 0;
if (is_array($value)) {
echo '[';
foreach ($value as $k => $v) {
$count++;
}
} else { // object
// detect infinite recursivity: only display a same object once
if (in_array($value, self::$objectsDisplayed, true)) {
$key = array_search($value, self::$objectsDisplayed,true);
echo "{recursive display: object #$key}\n";
return;
}
self::$objectsDisplayed[] = $value;
echo '{';
$ref = new ReflectionObject($value);
foreach ($ref->getProperties() as $prop) {
$count++;
}
}
if ($count > 0) {
echo "\r\n";
if (is_array($value)) {
foreach ($value as $k => $v) {
self::showVar($k, $v, $level + 1);
}
} else { // object
foreach ($ref->getProperties() as $prop) {
$modifier = $prop->isPrivate() ? self::IS_PRIVATE : ($prop->isProtected() ? self::IS_PROTECTED : self::IS_PUBLIC);
if ($prop->isStatic()) {
$modifier += self::IS_STATIC;
}
$prop->setAccessible(true);
$name = $prop->getName();
// TODO: detect declared type? (as opposed to current value type)
if ($prop->isInitialized($value)) {
$val = $prop->getValue($value);
} else {
$val = null;
}
$type = $prop->getType();
if (is_object($type)) {
if ($type->allowsNull())
$type = '?' . $type->getName();
else
$type = $type->getName();
}
self::showVar($name, $val, $level + 1, $modifier, $type);
}
}
for ($lvl = 0; $lvl < $level; $lvl++) {
echo $indent;
}
}
echo is_array($value) ? ']' : '}';
echo "\r\n";
} else {
echo '<span style="color:' . self::$config['color']['value'] . ';">';
if (is_object($value) || is_resource($value)) {
ob_start();
var_dump($value);
$result = ob_get_clean();
//trim the var_dump because EasyDump already handle the newline after dump
echo trim($result);
} elseif (is_array($value)) {
echo serialize($value);
} elseif (is_string($value)) {
echo '"' . htmlentities($value) . '"';
} elseif (is_bool($value)) {
echo $value ? 'true' : 'false';
} elseif (is_null($value)) {
echo 'NULL';
} elseif (is_numeric($value)) {
echo $value;
} else {
echo 'N/A';
}
echo "</span>\r\n";
}
}
/**
* Check if given variable is traversable in any way (array, traversable object even if it doesn't
* implements Traversable)
* @param mixed $variable backtrace executed PHP code
* @return bool information about the call
* @throws Exception
*/
protected static function isTraversable($variable): bool
{
//most common cases
if (is_array($variable) || $variable instanceof StdClass || $variable instanceof Traversable) {
return true;
}
if (!is_object($variable)) {
return false;
}
set_error_handler(function ($errno, $errstr) {
throw new Exception($errstr, $errno);
});
//try to loop through object
try {
foreach ($variable as $k => $v) {
break;
}
} catch (Exception $e) {
restore_error_handler();
return false;
}
restore_error_handler();
return true;
}
/**
* For debug purpose only. Exits after dump
* @param mixed $variable the variable to dump
*/
public static function debugExit()
{
call_user_func_array(array(__CLASS__, 'debug'), func_get_args());
exit;
}
}
/**
* Dump variable
* Alias of EasyDump::debug()
*/
if (!function_exists('d')) {
function d()
{
call_user_func_array(array('EasyDump', 'debug'), func_get_args());
}
}
/**
* Dump variable, then exit script
* Alias of EasyDump::debugExit()
*/
if (!function_exists('de')) {
function de()
{
call_user_func_array(array('EasyDump', 'debugExit'), func_get_args());
}
}