[ Index ]
 

Code source de PRADO 3.0.6

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/framework/Util/ -> TVarDumper.php (source)

   1  <?php
   2  /**
   3   * TVarDumper class file
   4   *
   5   * @author Qiang Xue <qiang.xue@gmail.com>
   6   * @link http://www.pradosoft.com/
   7   * @copyright Copyright &copy; 2005 PradoSoft
   8   * @license http://www.pradosoft.com/license/
   9   * @version $Id: TVarDumper.php 1397 2006-09-07 07:55:53Z wei $
  10   * @package System.Util
  11   */
  12  
  13  /**
  14   * TVarDumper class.
  15   *
  16   * TVarDumper is intended to replace the buggy PHP function var_dump and print_r.
  17   * It can correctly identify the recursively referenced objects in a complex
  18   * object structure. It also has a recurisve depth control to avoid indefinite
  19   * recursive display of some peculiar variables.
  20   *
  21   * TVarDumper can be used as follows,
  22   * <code>
  23   *   echo TVarDumper::dump($var);
  24   * </code>
  25   *
  26   * @author Qiang Xue <qiang.xue@gmail.com>
  27   * @version $Id: TVarDumper.php 1397 2006-09-07 07:55:53Z wei $
  28   * @package System.Util
  29   * @since 3.0
  30   */
  31  class TVarDumper
  32  {
  33      private static $_objects;
  34      private static $_output;
  35      private static $_depth;
  36  
  37      /**
  38       * Converts a variable into a string representation.
  39       * This method achieves the similar functionality as var_dump and print_r
  40       * but is more robust when handling complex objects such as PRADO controls.
  41       * @param mixed variable to be dumped
  42       * @param integer maximum depth that the dumper should go into the variable. Defaults to 10.
  43       * @return string the string representation of the variable
  44       */
  45  	public static function dump($var,$depth=10,$highlight=false)
  46      {
  47          self::$_output='';
  48          self::$_objects=array();
  49          self::$_depth=$depth;
  50          self::dumpInternal($var,0);
  51          if($highlight)
  52          {
  53              Prado::using('System.3rdParty.geshi.geshi');
  54              $geshi = new GeSHi(self::$_output, 'vardump');
  55              return $geshi->parse_code();
  56          }
  57          else
  58              return self::$_output;
  59      }
  60  
  61  	private static function dumpInternal($var,$level)
  62      {
  63          switch(gettype($var))
  64          {
  65              case 'boolean':
  66                  self::$_output.=$var?'true':'false';
  67                  break;
  68              case 'integer':
  69                  self::$_output.="$var";
  70                  break;
  71              case 'double':
  72                  self::$_output.="$var";
  73                  break;
  74              case 'string':
  75                  self::$_output.="'$var'";
  76                  break;
  77              case 'resource':
  78                  self::$_output.='{resource}';
  79                  break;
  80              case 'NULL':
  81                  self::$_output.="null";
  82                  break;
  83              case 'unknown type':
  84                  self::$_output.='{unknown}';
  85                  break;
  86              case 'array':
  87                  if(self::$_depth<=$level)
  88                      self::$_output.='array(...)';
  89                  else if(empty($var))
  90                      self::$_output.='array()';
  91                  else
  92                  {
  93                      $keys=array_keys($var);
  94                      $spaces=str_repeat(' ',$level*4);
  95                      self::$_output.="array\n".$spaces.'(';
  96                      foreach($keys as $key)
  97                      {
  98                          self::$_output.="\n".$spaces."    [$key] => ";
  99                          self::$_output.=self::dumpInternal($var[$key],$level+1);
 100                      }
 101                      self::$_output.="\n".$spaces.')';
 102                  }
 103                  break;
 104              case 'object':
 105                  if(($id=array_search($var,self::$_objects,true))!==false)
 106                      self::$_output.=get_class($var).'#'.($id+1).'(...)';
 107                  else if(self::$_depth<=$level)
 108                      self::$_output.=get_class($var).'(...)';
 109                  else
 110                  {
 111                      $id=array_push(self::$_objects,$var);
 112                      $className=get_class($var);
 113                      $members=(array)$var;
 114                      $keys=array_keys($members);
 115                      $spaces=str_repeat(' ',$level*4);
 116                      self::$_output.="$className#$id\n".$spaces.'(';
 117                      foreach($keys as $key)
 118                      {
 119                          $keyDisplay=strtr(trim($key),array("\0"=>':'));
 120                          self::$_output.="\n".$spaces."    [$keyDisplay] => ";
 121                          self::$_output.=self::dumpInternal($members[$key],$level+1);
 122                      }
 123                      self::$_output.="\n".$spaces.')';
 124                  }
 125                  break;
 126          }
 127      }
 128  }
 129  
 130  ?>


Généré le : Sun Feb 25 21:07:04 2007 par Balluche grâce à PHPXref 0.7