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:
<?php
class Debug
{
const STRICT_MODE = 'strict_mode';
const DISPLAY_DATABASE_QUERY = 'display_database_query';
private static $enabled = true;
private static $options = array();
private static $html_output = true;
public static function __static()
{
$file = new File(PATH_TO_ROOT . '/cache/debug.php');
if ($file->exists())
{
include $file->get_path();
self::$enabled = $enabled;
self::$options = $options;
}
}
public static function enabled_current_script_debug(array $options = array())
{
self::$enabled = true;
self::$options = $options;
}
public static function enabled_debug_mode(array $options = array())
{
self::$enabled = true;
self::$options = $options;
self::write_debug_file();
}
public static function disable_debug_mode()
{
self::$enabled = false;
self::$options = array();
self::write_debug_file();
}
private static function write_debug_file()
{
$file = new File(PATH_TO_ROOT . '/cache/debug.php');
$file->write('<?php ' . "\n" .
'$enabled = ' . var_export(self::$enabled, true) . ';' . "\n" .
'$options = ' . var_export(self::$options, true) . ';' . "\n" .
' ?>');
$file->close();
}
public static function is_debug_mode_enabled()
{
return self::$enabled;
}
public static function is_strict_mode_enabled()
{
return self::is_debug_mode_enabled() && self::get_option(self::STRICT_MODE, false);
}
public static function is_display_database_query_enabled()
{
return self::is_debug_mode_enabled() && self::get_option(self::DISPLAY_DATABASE_QUERY, false);
}
private static function get_option($key, $default)
{
if (array_key_exists($key, self::$options))
{
return self::$options[$key];
}
return $default;
}
public static function is_output_html()
{
return self::$html_output;
}
public static function set_plain_text_output_mode()
{
self::$html_output = false;
}
public static function fatal($exception)
{
if (!self::$html_output)
{
$message = get_class($exception) . ': ' . $exception->getMessage();
if (empty($message))
{
$message .= 'An exception has been thrown';
}
echo $message . "\n--------------------------------------------------------------------------------\n";
Debug::print_stacktrace(0, $exception);
}
else
{
$printer = new HTTPFatalExceptionPrinter($exception);
echo $printer->render();
}
exit;
}
public static function stop($object = null)
{
if ($object != null)
{
Debug::dump($object);
}
self::print_stacktrace();
exit;
}
public static function get_exception_context()
{
return new Exception();
}
public static function get_stacktrace()
{
$stack = self::get_exception_context()->getTrace();
unset($stack[0]);
return array_merge($stack, array());
}
public static function get_stacktrace_as_string($start_trace_index = 0, $exception = null)
{
$string_stacktrace = '';
$stacktrace = null;
if ($exception === null)
{
$stacktrace = self::get_stacktrace();
}
else
{
$start_trace_index--;
$stacktrace = $exception->getTrace();
}
$stacktrace_size = count($stacktrace);
$start_trace_index = $start_trace_index + 1;
for ($i = $start_trace_index; $i < $stacktrace_size; $i++)
{
$trace =& $stacktrace[$i];
$string_stacktrace .= '[' . ($i - $start_trace_index) . '] ' . ExceptionUtils::get_file($trace) .
':' . ExceptionUtils::get_line($trace) . ' - ' . ExceptionUtils::get_method_prototype($trace) . "\n";
}
$string_stacktrace .= '[URL] ' . $_SERVER['REQUEST_URI'];
if (self::is_output_html())
{
$string_stacktrace = str_replace("\n", '<br />', $string_stacktrace);
}
return $string_stacktrace;
}
public static function print_stacktrace($start_trace_index = 0, Exception $exception = null)
{
if ($exception !== null)
{
$start_trace_index--;
}
echo self::get_stacktrace_as_string($start_trace_index + 1, $exception);
}
public static function dump($object)
{
if (self::$html_output)
{
echo '<pre>'; print_r($object); echo '</pre>';
}
else
{
echo "\n"; print_r($object); echo "\n";
}
}
}
?>