×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: Hendrik Hamming
Added: Oct 31, 2016 1:41 PM
Modified: Mar 5, 2020 3:20 PM
Views: 2178
Tags: debug
  1. <?php
  2.  
  3. /**
  4.  * debug view function
  5.  *
  6.  * @author Hendrik Hamming <hendrik@madscripter.eu>
  7.  *
  8.  * @param type $var
  9.  * @param string $label
  10.  * @return type
  11.  */
  12. function debug($var, $label = false){
  13.         if(!WP_DEBUG)return;
  14.         echo '<pre style="border:1px solid #222; padding: 5px;max-width: 800px;margin: 40px 20px;">';
  15.         $debug = DEBUG::i($var, $label);
  16.        
  17.         echo '<span style="border-bottom:1px solid #777;display:block; padding: 4px;font-style:italic; margin-bottom:10px;">'.$debug->getLabel().'</span>';
  18.         echo '<div style="">';
  19.  
  20.         if (is_null($var)|| is_int($var)||  is_bool($var) || $var == ''){
  21.                 var_dump($var);
  22.         } else {
  23.                 print_r($var);
  24.         }
  25.         echo '</div>';
  26.  
  27.         echo '<span style="font-style:italic; border-top: 1px solid #777; padding: 4px; margin-top: 10px;display:block;font-size: 12px;">' . $debug->footer .'</span>';
  28.         echo "</pre>";
  29. }
  30.  
  31. /**
  32.  * debug view function
  33.  *
  34.  * @author Hendrik Hamming <hendrik@madscripter.eu>
  35.  *
  36.  * @param array $array
  37.  * @uses DEBUG
  38.  * @return type
  39.  */
  40. function debug_array(array $array)
  41. {
  42.         foreach ($array as $key => $value)
  43.         {
  44.                 if(!WP_DEBUG)return;
  45.                 echo '<pre style="border:1px solid #222; padding: 5px;max-width: 800px;margin: 40px 20px;">';
  46.                 $debug = DEBUG::i($value, $key);
  47.  
  48.                 echo '<span style="border-bottom:1px solid #777;display:block; padding: 4px;font-style:italic; margin-bottom:10px;">'.$debug->getLabel().'</span>';
  49.                 echo '<div style="">';
  50.  
  51.                 if (is_null($value)|| is_int($value)||  is_bool($value) || $value == ''){
  52.                         var_dump($value);
  53.                 } else {
  54.                         print_r($value);
  55.                 }
  56.                 echo '</div>';
  57.  
  58.                 echo '<span style="font-style:italic; border-top: 1px solid #777; padding: 4px; margin-top: 10px;display:block;font-size: 12px;">' . $debug->footer .'</span>';
  59.                 echo "</pre>";
  60.         }
  61. }
  62.  
  63. class DEBUG {
  64.         private static $instance = false;
  65.         private static $instances = array();
  66.         private $label = array();
  67.         public static $counter = 0;
  68.         private $var;
  69.         private $file;
  70.         public $footer = '';
  71.         private $contents;
  72.         private $backtrace;
  73.  
  74.         public function __construct($var, $label = false) {
  75.                 $this->var = $var;
  76.                 $this->parse_backtrace();
  77.                 $this->setLabel($label);
  78.                 $this->setFooter();
  79.         }
  80.  
  81.         private function parse_backtrace (){
  82.                 $b = debug_backtrace();
  83.                 $this->backtrace = $b;
  84.                 $this->file = new stdClass();
  85.                 $this->file->path = str_replace(__DIR__.'/', '', $b[3]['file']);
  86.                 $this->file->line = $b[3]['line'];
  87.  
  88.                 $line = trim(file($b[3]['file'])[$b[3]['line']-1]);
  89.  
  90.                 preg_match_all('/debug(_array){0,1}\(([^\)]+)\)/', $line, $matches);
  91.                
  92.                 $this->contents = $matches[2][DEBUG::$counter-1];
  93.                 if(count($matches[2])==DEBUG::$counter){
  94.                         DEBUG::reset ();
  95.                 }
  96.         }
  97.  
  98.         private function setLabel ($label = false){
  99.                 if($label !== false){
  100.                         $this->label[] = '<b>Label:</b> ' . (string)$label;
  101.                 }
  102.                 if(isset($this->backtrace[4])){
  103.                         if( isset($this->backtrace[4]['class'])){
  104.                         $this->label[] = '<b>Class:</b> ' . $this->backtrace[4]['class'] . '';
  105.                         }
  106.                         if(isset($this->backtrace[4]['function'])){
  107.                         $this->label[] = '<b>Function:</b> ' . $this->backtrace[4]['function'];
  108.                         }
  109.                 }
  110.                 if(!preg_match('/^\'|^"/', substr($this->contents,0,1))){
  111.                         $this->label[] = '<b>Var:</b> ' . $this->contents;
  112.                 }
  113.         }
  114.  
  115.         public function getLabel (){
  116.                 return (string)join($this->label, "\r\n");
  117.         }
  118.  
  119.         private function setFooter (){
  120.                 $this->footer = $this->file->path . ' Line: ' . $this->file->line;
  121.         }
  122.  
  123.         private static function reset(){
  124.                 self::$instances = array();
  125.                 self::$counter =0;
  126.         }
  127.  
  128.         public static function getInstances(){
  129.                 return self::$instances;
  130.         }
  131.         public static function countInstances(){
  132.                 return count(self::$instances);
  133.         }
  134.  
  135.         public static function i($var, $label = false){
  136.  
  137.                 self::$counter++;
  138.                 self::$instance = new DEBUG($var, $label);
  139.                 self::$instances[] = self::$instance;
  140.  
  141.                 return self::$instance;
  142.         }
  143.  
  144. }
  145.