Last active
April 6, 2016 16:07
-
-
Save jcottrell/86bb6d732395974d7e93397ea99ba051 to your computer and use it in GitHub Desktop.
PHP Debug formatter for web
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* this is NOT mine: see http://php.net/manual/en/function.var-dump.php#116041 */ | |
function dump_debug($input, $collapse=false) { | |
$recursive = function($data, $level=0) use (&$recursive, $collapse) { | |
global $argv; | |
$isTerminal = isset($argv); | |
if (!$isTerminal && $level == 0 && !defined("DUMP_DEBUG_SCRIPT")) { | |
define("DUMP_DEBUG_SCRIPT", true); | |
echo '<script language="Javascript">function toggleDisplay(id) {'; | |
echo 'var state = document.getElementById("container"+id).style.display;'; | |
echo 'document.getElementById("container"+id).style.display = state == "inline" ? "none" : "inline";'; | |
echo 'document.getElementById("plus"+id).style.display = state == "inline" ? "inline" : "none";'; | |
echo '}</script>'."\n"; | |
} | |
$type = !is_string($data) && is_callable($data) ? "Callable" : ucfirst(gettype($data)); | |
$type_data = null; | |
$type_color = null; | |
$type_length = null; | |
switch ($type) { | |
case "String": | |
$type_color = "green"; | |
$type_length = strlen($data); | |
$type_data = "\"" . htmlentities($data) . "\""; break; | |
case "Double": | |
case "Float": | |
$type = "Float"; | |
$type_color = "#0099c5"; | |
$type_length = strlen($data); | |
$type_data = htmlentities($data); break; | |
case "Integer": | |
$type_color = "red"; | |
$type_length = strlen($data); | |
$type_data = htmlentities($data); break; | |
case "Boolean": | |
$type_color = "#92008d"; | |
$type_length = strlen($data); | |
$type_data = $data ? "TRUE" : "FALSE"; break; | |
case "NULL": | |
$type_length = 0; break; | |
case "Array": | |
$type_length = count($data); | |
} | |
if (in_array($type, array("Object", "Array"))) { | |
$notEmpty = false; | |
foreach($data as $key => $value) { | |
if (!$notEmpty) { | |
$notEmpty = true; | |
if ($isTerminal) { | |
echo $type . ($type_length !== null ? "(" . $type_length . ")" : "")."\n"; | |
} else { | |
$id = substr(md5(rand().":".$key.":".$level), 0, 8); | |
echo "<a href=\"javascript:toggleDisplay('". $id ."');\" style=\"text-decoration:none\">"; | |
echo "<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span>"; | |
echo "</a>"; | |
echo "<span id=\"plus". $id ."\" style=\"display: " . ($collapse ? "inline" : "none") . ";\"> ⤵</span>"; | |
echo "<div id=\"container". $id ."\" style=\"display: " . ($collapse ? "inline" : "none") . ";\">"; | |
echo "<br />"; | |
} | |
for ($i=0; $i <= $level; $i++) { | |
echo $isTerminal ? "| " : "<span style='color:black'>|</span> "; | |
} | |
echo $isTerminal ? "\n" : "<br />"; | |
} | |
for ($i=0; $i <= $level; $i++) { | |
echo $isTerminal ? "| " : "<span style='color:black'>|</span> "; | |
} | |
echo $isTerminal ? "[" . $key . "] => " : "<span style='color:black'>[" . $key . "] => </span>"; | |
call_user_func($recursive, $value, $level+1); | |
} | |
if ($notEmpty) { | |
for ($i=0; $i <= $level; $i++) { | |
echo $isTerminal ? "| " : "<span style='color:black'>|</span> "; | |
} | |
if (!$isTerminal) { | |
echo "</div>"; | |
} | |
} else { | |
echo $isTerminal ? | |
$type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " : | |
"<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span> "; | |
} | |
} else { | |
echo $isTerminal ? | |
$type . ($type_length !== null ? "(" . $type_length . ")" : "") . " " : | |
"<span style='color:#666666'>" . $type . ($type_length !== null ? "(" . $type_length . ")" : "") . "</span> "; | |
if ($type_data != null) { | |
echo $isTerminal ? $type_data : "<span style='color:" . $type_color . "'>" . $type_data . "</span>"; | |
} | |
} | |
echo $isTerminal ? "\n" : "<br />"; | |
}; | |
call_user_func($recursive, $input); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment