Advertisement
Guest User

sten

a guest
Nov 13th, 2015
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.06 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CVarDumper class file.
  4.  *
  5.  * @author Qiang Xue <qiang.xue@gmail.com>
  6.  * @link http://www.yiiframework.com/
  7.  * @copyright 2008-2013 Yii Software LLC
  8.  * @license http://www.yiiframework.com/license/
  9.  */
  10.  
  11. /**
  12.  * CVarDumper is intended to replace the buggy PHP function var_dump and print_r.
  13.  * It can correctly identify the recursively referenced objects in a complex
  14.  * object structure. It also has a recursive depth control to avoid indefinite
  15.  * recursive display of some peculiar variables.
  16.  *
  17.  * CVarDumper can be used as follows,
  18.  * <pre>
  19.  * CVarDumper::dump($var);
  20.  * </pre>
  21.  *
  22.  * @author Qiang Xue <qiang.xue@gmail.com>
  23.  * @package system.utils
  24.  * @since 1.0
  25.  */
  26. class CVarDumper
  27. {
  28.     private static $_objects;
  29.     private static $_output;
  30.     private static $_depth;
  31.  
  32.     /**
  33.      * Displays a variable.
  34.      * This method achieves the similar functionality as var_dump and print_r
  35.      * but is more robust when handling complex objects such as Yii controllers.
  36.      * @param mixed $var variable to be dumped
  37.      * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  38.      * @param boolean $highlight whether the result should be syntax-highlighted
  39.      */
  40.     public static function dump($var,$depth=10,$highlight=false)
  41.     {
  42.         echo self::dumpAsString($var,$depth,$highlight);
  43.     }
  44.  
  45.     /**
  46.      * Dumps a variable in terms of a string.
  47.      * This method achieves the similar functionality as var_dump and print_r
  48.      * but is more robust when handling complex objects such as Yii controllers.
  49.      * @param mixed $var variable to be dumped
  50.      * @param integer $depth maximum depth that the dumper should go into the variable. Defaults to 10.
  51.      * @param boolean $highlight whether the result should be syntax-highlighted
  52.      * @return string the string representation of the variable
  53.      */
  54.     public static function dumpAsString($var,$depth=10,$highlight=false)
  55.     {
  56.         self::$_output='';
  57.         self::$_objects=array();
  58.         self::$_depth=$depth;
  59.         self::dumpInternal($var,0);
  60.         if($highlight)
  61.         {
  62.             $result=highlight_string("<?php\n".self::$_output,true);
  63.             self::$_output=preg_replace('/&lt;\\?php<br \\/>/','',$result,1);
  64.         }
  65.         return self::$_output;
  66.     }
  67.  
  68.     /*
  69.      * @param mixed $var variable to be dumped
  70.      * @param integer $level depth level
  71.      */
  72.     private static function dumpInternal($var,$level)
  73.     {
  74.         switch(gettype($var))
  75.         {
  76.             case 'boolean':
  77.                 self::$_output.=$var?'true':'false';
  78.                 break;
  79.             case 'integer':
  80.                 self::$_output.="$var";
  81.                 break;
  82.             case 'double':
  83.                 self::$_output.="$var";
  84.                 break;
  85.             case 'string':
  86.                 self::$_output.="'".addslashes($var)."'";
  87.                 break;
  88.             case 'resource':
  89.                 self::$_output.='{resource}';
  90.                 break;
  91.             case 'NULL':
  92.                 self::$_output.="null";
  93.                 break;
  94.             case 'unknown type':
  95.                 self::$_output.='{unknown}';
  96.                 break;
  97.             case 'array':
  98.                 if(self::$_depth<=$level)
  99.                     self::$_output.='array(...)';
  100.                 elseif(empty($var))
  101.                     self::$_output.='array()';
  102.                 else
  103.                 {
  104.                     $keys=array_keys($var);
  105.                     $spaces=str_repeat(' ',$level*4);
  106.                     self::$_output.="array\n".$spaces.'(';
  107.                     foreach($keys as $key)
  108.                     {
  109.                         self::$_output.="\n".$spaces.'    ';
  110.                         self::dumpInternal($key,0);
  111.                         self::$_output.=' => ';
  112.                         self::dumpInternal($var[$key],$level+1);
  113.                     }
  114.                     self::$_output.="\n".$spaces.')';
  115.                 }
  116.                 break;
  117.             case 'object':
  118.                 if(($id=array_search($var,self::$_objects,true))!==false)
  119.                     self::$_output.=get_class($var).'#'.($id+1).'(...)';
  120.                 elseif(self::$_depth<=$level)
  121.                     self::$_output.=get_class($var).'(...)';
  122.                 else
  123.                 {
  124.                     $id=array_push(self::$_objects,$var);
  125.                     $className=get_class($var);
  126.                     $members=(array)$var;
  127.                     $spaces=str_repeat(' ',$level*4);
  128.                     self::$_output.="$className#$id\n".$spaces.'(';
  129.                     foreach($members as $key=>$value)
  130.                     {
  131.                         $keyDisplay=strtr(trim($key),array("\0"=>':'));
  132.                         self::$_output.="\n".$spaces."    [$keyDisplay] => ";
  133.                         self::$_output.=self::dumpInternal($value,$level+1);
  134.                     }
  135.                     self::$_output.="\n".$spaces.')';
  136.                 }
  137.                 break;
  138.         }
  139.     }
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement