Created
August 18, 2016 08:24
-
-
Save hiephm/ae0a4e401c5490677287ca2fbd9dad7b to your computer and use it in GitHub Desktop.
Debug helper functions
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
function __die($obj) | |
{ | |
print_r($obj);die(); | |
} | |
function __debug($message) | |
{ | |
$file = __DIR__ . '/../var/log/dump.log'; | |
file_put_contents($file, $message, FILE_APPEND); | |
file_put_contents($file, "\n", FILE_APPEND); | |
} | |
function __dumpException(\Exception $e, $returnAsString = false) | |
{ | |
$newline = (PHP_SAPI == 'cli') ? "\n" : "<br/>\n"; | |
$tab = (PHP_SAPI == 'cli') ? "\t" : " "; | |
$output = ''; | |
$output .= "Message: {$e->getMessage()}{$newline}"; | |
foreach ($e->getTrace() as $step => $trace) { | |
$file = isset($trace['file']) ? $trace['file'] : ''; | |
$line = isset($trace['line']) ? $trace['line'] : ''; | |
$func = isset($trace['function']) ? $trace['function'] : ''; | |
$class = isset($trace['class']) ? $trace['class'] : ''; | |
$type = isset($trace['type']) ? $trace['type'] : ''; | |
$args = ''; | |
if (isset($trace['args'])) { | |
$items = array(); | |
foreach ($trace['args'] as $pos => $arg) { | |
if ($arg === '') { | |
$arg = '\'\''; | |
} elseif ($arg === null) { | |
$arg = '[null]'; | |
} elseif (is_bool($arg)) { | |
$arg = $arg ? '[true]' : '[false]'; | |
} elseif (is_scalar($arg)) { | |
$arg = strval($arg); | |
} elseif (is_array($arg)) { | |
$arg = '[array]'; | |
}elseif (is_object($arg)) { | |
$arg = sprintf('[object %s]', get_class($arg)); | |
} else { | |
$arg = '[unknown]'; | |
} | |
$items[] = "{$pos}:$arg"; | |
} | |
$args = '(' . implode(', ', $items) . ')'; | |
} | |
$output .= "#{$step} {$file} ({$line}):{$newline} {$tab} {$class}{$type}{$func}{$args}{$newline}{$newline}"; | |
} | |
if ($returnAsString) { | |
return $output; | |
} else { | |
echo $output; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment