00001 <?php
00010 final class debug {
00011
00017 private static $cfg;
00018
00024 private static $timer = array();
00025
00026 private static function initCfg() {
00027 if (!self::$cfg)
00028 self::$cfg = new config(factory::loadCfg(__CLASS__));
00029 }
00030
00038 public static function callFrom($nb=1, $sep='-') {
00039 $nb++;
00040 $dbt = debug_backtrace();
00041 if ($sep === null)
00042 return $dbt[$nb]['class'];
00043 else
00044 return $dbt[$nb]['class'].$sep.$dbt[$nb]['function'];
00045 }
00046
00054 public static function trace($obj, $printExit=false) {
00055 $ret = '<pre>'.htmlentities(print_r($obj, true)).'</pre>';
00056 if ($printExit > 1)
00057 response::getInstance()->sendText($ret);
00058 else if ($printExit)
00059 echo $ret;
00060 else
00061 return $ret;
00062 }
00063
00064 public static function errorHandler($code, $message, $file, $line) {
00065 self::initCfg();
00066 $msg = self::$cfg->getInArray('errors', $code).': '.$message.' at '.$file.':'.$line;
00067
00068 $stopCfg = self::$cfg->stop;
00069 $stop = is_array($stopCfg)? in_array($code, $stopCfg) : $stopCfg;
00070
00071 if ($stop) {
00072 $e = new nException($msg, $code);
00073 $e->line = $line;
00074 $e->file = $file;
00075 throw $e;
00076 return true;
00077 } else {
00078
00079 }
00080 }
00081
00088 public static function debugger(array $elts=null) {
00089 if (is_null($elts)) {
00090 debug::timer('nyro');
00091 debug::timer('nyroRender');
00092 return debug::debugger(array(
00093 'timing'=>array('Timing', debug::timer(), 'time'),
00094 'included'=>array('Included Files', get_included_files(), array('name'=>'code_red', 'type'=>'script')),
00095 'session'=>array('Session vars', $_SESSION, 'shield'),
00096 'db_queries'=>array('DB Queries', db::log(), 'database'),
00097 'consts'=>array('Constants', array_reverse(get_defined_constants(true), true), array('name'=>'gear', 'type'=>'script')),
00098 'request'=>array('Request', request::get(), array('name'=>'right', 'type'=>'arrow')),
00099 'cookies'=>array('Cookies', $_COOKIE, array('name'=>'gray', 'type'=>'user')),
00100 'get'=>array('Get', $_GET, array('name'=>'show', 'type'=>'tag')),
00101 'post'=>array('Post', $_POST, array('name'=>'green', 'type'=>'tag')),
00102 'files'=>array('Files', $_FILES, array('name'=>'orange', 'type'=>'tag')),
00103 'response'=>array('Response', array('Headers'=>response::getInstance()->getHeader(), 'Included Files'=>response::getInstance()->getIncFiles()), array('name'=>'right', 'type'=>'arrow')),
00104 ));
00105 }
00106 if (request::get('out') != 'html')
00107 return;
00108
00109 $menu = array();
00110 $content = array();
00111 $close = utils::getIcon(array('name'=>'cross', 'type'=>'default', 'attr'=>array('class'=>'close', 'alt'=>'Close')));
00112 foreach($elts as $k=>$v) {
00113 $icon = array_key_exists(2, $v)
00114 ? (utils::getIcon(is_array($v[2])? $v[2] : array('name'=>'show', 'type'=>$v[2])))
00115 : null;
00116 $menu[] = '<a rel="'.$k.'">'.$icon.$v[0].'</a>';
00117 $tmp = '<div class="debugElt" id="'.$k.'" style="display: none;">'.$close.'<h2>'.$icon.$v[0].'</h2>';
00118 if (is_array($v[1])) {
00119 if (is_numeric(key($v[1])) && !is_array($v[1]))
00120 $tmp.= '<ol><li>'.implode('</li><li>', $v[1]).'</li></ol>';
00121 else
00122 $tmp.= debug::trace($v[1]);
00123 } else
00124 $tmp.= $v[1];
00125 $tmp.= '</div>';
00126 $content[] = $tmp;
00127 }
00128
00129 $resp = response::getInstance();
00130 return '<div id="nyroDebugger">'
00131 .$resp->getIncludeTagFile('js', 'debug')
00132 .$resp->getIncludeTagFile('css', 'debug')
00133 .'<ul><li id="close">'.$close.'</li><li>'.implode('</li><li>', $menu).'</li></ul>'
00134 .implode("\n", $content)
00135 .'</div>';
00136 }
00137
00144 public static function timer($name=null) {
00145 if (is_null($name)) {
00146 $tmp = array();
00147 foreach(self::$timer as $k=>$v)
00148 if (array_key_exists(1, $v))
00149 $tmp[$k] = $v[1];
00150 return $tmp;
00151 }
00152
00153 if (!array_key_exists($name, self::$timer))
00154 self::$timer[$name] = array(microtime(true)*1000);
00155 else {
00156 self::$timer[$name][1] = (microtime(true)*1000 - self::$timer[$name][0]).' ms';
00157 return self::$timer[$name][1];
00158 }
00159 }
00160
00161 }