Last active
January 5, 2017 05:17
-
-
Save darkcolonist/25047a7ab74740da7a2b20d0268985bf to your computer and use it in GitHub Desktop.
create code backtrace - list all files and lines passed through before the function
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 | |
/** | |
* display backtrace of the code, and the list of files the code has | |
* passed through to reach this function. | |
* | |
* example: | |
* Array | |
* ( | |
* [0] => /www/index.php:118 | |
* [1] => /www/system/core/Bootstrap.php:55 | |
* [2] => /www/system/core/Event.php:209 | |
* [3] => /www/system/core/Kohana.php:291 | |
* [4] => /www/application/controllers/test/util.php:92 | |
* ) | |
* | |
* @param string $type [string|array] | |
* @param int $directorySubstr [0 for whole name, n for first n characters of name] | |
* @return mixed [depends on $type] | |
*/ | |
static function backtrace($type = "array", $directorySubstr = 0){ | |
$trimFromFilePath = array( | |
'/www/', | |
'.php' | |
); | |
$omitFromTrace = array( | |
"system/core/Kohana.php", | |
"system/core/Event.php", | |
"system/core/Bootstrap.php", | |
"index.php" | |
); | |
$backtrace = debug_backtrace(); | |
$simplifiedBacktrace = array(); | |
foreach ($backtrace as $btkey => $backitem) { | |
if(isset($backitem['file'])) { | |
foreach ($omitFromTrace as $rftkey => $rftval) { | |
if(strpos($backitem['file'], $rftval) !== false) | |
continue 2; | |
} | |
foreach ($trimFromFilePath as $rffkey => $rffval) { | |
$backitem['file'] = str_replace($rffval, '', $backitem['file']); | |
} | |
if($directorySubstr != 0){ | |
$fileSubsecs = explode(DIRECTORY_SEPARATOR, $backitem['file']); | |
foreach ($fileSubsecs as $fsskey => $fssval) { | |
if($fsskey != count($fileSubsecs) - 1){ | |
$fileSubsecs[$fsskey] = substr($fileSubsecs[$fsskey], 0, $directorySubstr); | |
} | |
} | |
$backitem['file'] = implode(DIRECTORY_SEPARATOR, $fileSubsecs); | |
} | |
$simplifiedBacktrace[] = $backitem['file'].":".$backitem['line']; | |
} | |
} | |
$simplifiedBacktrace = array_reverse($simplifiedBacktrace); | |
if($type == 'array'){ | |
return $simplifiedBacktrace; | |
}else{ | |
$stringify = ''; | |
$simplifiedBacktrace = array_reverse($simplifiedBacktrace); | |
foreach ($simplifiedBacktrace as $sbtkey => $sbtval) { | |
$stringify .= "$sbtval < "; | |
} | |
$stringify = rtrim($stringify, " < "); | |
// stringify the array | |
return $stringify; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment