nyroFwk  0.2
debug.class.php
Go to the documentation of this file.
1 <?php
10 final class debug {
11 
17  private static $cfg;
18 
24  private static $timer = array();
25 
26  private static function initCfg() {
27  if (!self::$cfg)
28  self::$cfg = new config(factory::loadCfg(__CLASS__));
29  }
30 
38  public static function callFrom($nb=1, $sep='-') {
39  $nb++;
40  $dbt = debug_backtrace();
41  if ($sep === null)
42  return $dbt[$nb]['class'];
43  else
44  return $dbt[$nb]['class'].$sep.$dbt[$nb]['function'];
45  }
46 
54  public static function trace($obj, $printExit=false) {
55  $ret = '<pre>'.htmlentities(print_r($obj, true)).'</pre>';
56  if ($printExit > 1)
57  response::getInstance()->sendText($ret);
58  else if ($printExit)
59  echo $ret;
60  else
61  return $ret;
62  }
63 
64  public static function errorHandler($code, $message, $file, $line) {
65  self::initCfg();
66  $msg = self::$cfg->getInArray('errors', $code).': '.$message.' at '.$file.':'.$line;
67 
68  $stopCfg = self::$cfg->stop;
69  $stop = is_array($stopCfg)? in_array($code, $stopCfg) : $stopCfg;
70 
71  if ($stop) {
72  $e = new nException($msg, $code);
73  $e->line = $line;
74  $e->file = $file;
75  throw $e;
76  return true;
77  } else {
78  // Maybe log something somewhere?
79  }
80  }
81 
88  public static function debugger(array $elts=null) {
89  if (is_null($elts)) {
90  debug::timer('nyro');
91  debug::timer('nyroRender');
92  return debug::debugger(array(
93  'timing'=>array('Timing', debug::timer(), 'time'),
94  'included'=>array('Included Files', get_included_files(), array('name'=>'code_red', 'type'=>'script')),
95  'session'=>array('Session vars', $_SESSION, 'shield'),
96  'db_queries'=>array('DB Queries', db::log(), 'database'),
97  'consts'=>array('Constants', array_reverse(get_defined_constants(true), true), array('name'=>'gear', 'type'=>'script')),
98  'request'=>array('Request', request::get(), array('name'=>'right', 'type'=>'arrow')),
99  'cookies'=>array('Cookies', $_COOKIE, array('name'=>'gray', 'type'=>'user')),
100  'get'=>array('Get', $_GET, array('name'=>'show', 'type'=>'tag')),
101  'post'=>array('Post', $_POST, array('name'=>'green', 'type'=>'tag')),
102  'files'=>array('Files', $_FILES, array('name'=>'orange', 'type'=>'tag')),
103  'response'=>array('Response', array('Headers'=>response::getInstance()->getHeader(), 'Included Files'=>response::getInstance()->getIncFiles()), array('name'=>'right', 'type'=>'arrow')),
104  ));
105  }
106  if (request::get('out') != 'html')
107  return;
108 
109  $menu = array();
110  $content = array();
111  $close = utils::getIcon(array('name'=>'cross', 'type'=>'default', 'attr'=>array('class'=>'close', 'alt'=>'Close')));
112  foreach($elts as $k=>$v) {
113  $icon = array_key_exists(2, $v)
114  ? (utils::getIcon(is_array($v[2])? $v[2] : array('name'=>'show', 'type'=>$v[2])))
115  : null;
116  $menu[] = '<a rel="'.$k.'">'.$icon.$v[0].'</a>';
117  $tmp = '<div class="debugElt" id="'.$k.'" style="display: none;">'.$close.'<h2>'.$icon.$v[0].'</h2>';
118  if (is_array($v[1])) {
119  if (is_numeric(key($v[1])) && !is_array($v[1]))
120  $tmp.= '<ol><li>'.implode('</li><li>', $v[1]).'</li></ol>';
121  else
122  $tmp.= debug::trace($v[1]);
123  } else
124  $tmp.= $v[1];
125  $tmp.= '</div>';
126  $content[] = $tmp;
127  }
128 
129  $resp = response::getInstance();
130  return '<div id="nyroDebugger">'
131  .$resp->getIncludeTagFile('js', 'debug')
132  .$resp->getIncludeTagFile('css', 'debug')
133  .'<ul><li id="close">'.$close.'</li><li>'.implode('</li><li>', $menu).'</li></ul>'
134  .implode("\n", $content)
135  .'</div>';
136  }
137 
144  public static function timer($name=null) {
145  if (is_null($name)) {
146  $tmp = array();
147  foreach(self::$timer as $k=>$v)
148  if (array_key_exists(1, $v))
149  $tmp[$k] = $v[1];
150  return $tmp;
151  }
152 
153  if (!array_key_exists($name, self::$timer))
154  self::$timer[$name] = array(microtime(true)*1000);
155  else {
156  self::$timer[$name][1] = (microtime(true)*1000 - self::$timer[$name][0]).' ms';
157  return self::$timer[$name][1];
158  }
159  }
160 
161 }
static callFrom($nb=1, $sep='-')
Definition: debug.class.php:38
static get($get=null)
static $timer
Definition: debug.class.php:24
static getIcon(array $prm)
static timer($name=null)
static debugger(array $elts=null)
Definition: debug.class.php:88
static $cfg
Definition: debug.class.php:17
static errorHandler($code, $message, $file, $line)
Definition: debug.class.php:64
static getInstance()
static log($sql=null, $bind=null)
Definition: db.class.php:156
static loadCfg($className, $searchParent=true)
static initCfg()
Definition: debug.class.php:26
static trace($obj, $printExit=false)
Definition: debug.class.php:54
Generated on Sun Oct 15 2017 22:25:20 for nyroFwk by doxygen 1.8.13