Var_Dump
[ class tree: Var_Dump ] [ index: Var_Dump ] [ all elements ]

Source for file Var_Dump.php

Documentation is available at Var_Dump.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Frederic Poeydomenge <frederic.poeydomenge@free.fr>         |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id:
  20.  
  21. require_once 'PEAR.php';
  22. require_once 'Var_Dump/Renderer.php';
  23.  
  24. /**
  25.  * Wrapper for the var_dump function.
  26.  *
  27.  * " The var_dump function displays structured information about expressions
  28.  * that includes its type and value. Arrays are explored recursively
  29.  * with values indented to show structure. "
  30.  *
  31.  * The Var_Dump class captures the output of the var_dump function,
  32.  * by using output control functions, and then uses external renderer
  33.  * classes for displaying the result in various graphical ways :
  34.  * simple text, (X)HTML text, (X)HTML table, XML, ...
  35.  *
  36.  * @package Var_Dump
  37.  * @category PHP
  38.  * @author Frederic Poeydomenge <frederic.poeydomenge@free.fr>
  39.  */
  40.  
  41. // {{{ constants
  42. define ('VAR_DUMP_START_GROUP',       1);
  43. define ('VAR_DUMP_FINISH_GROUP',      2);
  44. define ('VAR_DUMP_START_ELEMENT_NUM'3);
  45. define ('VAR_DUMP_START_ELEMENT_STR'4);
  46. define ('VAR_DUMP_FINISH_ELEMENT',    5);
  47. define ('VAR_DUMP_FINISH_ELEMENT_NL'6);
  48. // }}}
  49.  
  50. class Var_Dump
  51. {
  52.  
  53.     // {{{ properties
  54.  
  55.     /**
  56.      * Run-time configuration options.
  57.      * @var array 
  58.      * @access public
  59.      */
  60.     var $options = array(
  61.         'display_mode' => 'XHTML_Text'
  62.     );
  63.  
  64.     /**
  65.      * Rendering configuration options.
  66.      * See Var_Dump/Renderer/*.php for the complete list of options
  67.      * @var array 
  68.      * @access public
  69.      */
  70.     var $rendererOptions = array();
  71.  
  72.     /**
  73.      * Rendering object.
  74.      * @var object 
  75.      * @access public
  76.      */
  77.     var $renderer = NULL;
  78.  
  79.     // }}}
  80.  
  81.     // {{{ constructor
  82.     /**
  83.      * Class constructor.
  84.          * The factory approach must be used in relationship with the
  85.          * toString() method.
  86.      * See Var_Dump/Renderer/*.php for the complete list of options
  87.      * @see Var_Dump::toString()
  88.      * @param array $options         Global parameters.
  89.      * @param array $rendererOptions Parameters for the rendering.
  90.      * @access public
  91.      */
  92.     function Var_Dump($options = array()$rendererOptions = array())
  93.         {
  94.         $this->options array_merge (
  95.             $this->options,
  96.             $options
  97.         );
  98.         $this->rendererOptions $rendererOptions;
  99.         $this->renderer Var_Dump_Renderer::factory(
  100.             $this->options['display_mode'],
  101.             $this->rendererOptions
  102.         );
  103.     }
  104.     // }}}
  105.     
  106.     // {{{ factory()
  107.     /**
  108.      * Attempt to return a concrete Var_Dump instance.
  109.          * The factory approach must be used in relationship with the
  110.          * toString() method.
  111.      * See Var_Dump/Renderer/*.php for the complete list of options
  112.      * @see Var_Dump::toString()
  113.      * @param array $options         Global parameters.
  114.      * @param array $rendererOptions Parameters for the rendering.
  115.      * @access public
  116.      */
  117.     function factory($options = array()$rendererOptions = array())
  118.     {
  119.         $obj new Var_Dump($options$rendererOptions);
  120.         return $obj;
  121.     }
  122.     // }}}
  123.  
  124.     // {{{ toString()
  125.     /**
  126.      * Uses a renderer object to return the string representation of a variable.
  127.      * @param mixed $expression The variable to parse.
  128.      * @return string The string representation of the variable.
  129.      * @access public
  130.      */
  131.     function toString($expression)
  132.     {
  133.  
  134.         if (PEAR::isError($this->renderer)) {
  135.             return '';
  136.         }
  137.  
  138.         $family = array()// element family
  139.         $depth  = array()// element depth
  140.         $type   = array()// element type
  141.         $value  = array()// element value
  142.         $variation = 0;
  143.  
  144.         ob_start();
  145.         var_dump($expression);
  146.         $variable ob_get_contents();
  147.         ob_end_clean();
  148.  
  149.         // The numbers between the square brackets [] are the reference of the captured subpattern
  150.         // and correspond to the entries in the resulting $matches array
  151.         preg_match_all(
  152.             '/^
  153.               (\s*)                             # 2 spaces for each depth level
  154.               (?:                               # start of alternative
  155.                 (?:\[("?)(.*?)\\2\]=>)          #   = Key [2-3]
  156.                   |                             #   OR
  157.                 (?:                             #   = Value, multiline [4-5]
  158.                   (&?string\([0-9]*\))          #     - type [4]
  159.                   \s+"((?:.*[^"])?$\n           #     - value [5], first line
  160.                   (?:^.*[^"]$\n)*               #     - value [5], multiple middle line
  161.                   ^.*)"                         #     - value [5], last line
  162.                 )                               #
  163.                   |                             #   OR
  164.                 (                               #   = Value [6-11]
  165.                   (&?)                          #     - reference [7]
  166.                   (bool|string|int|float|       #     - type [8]
  167.                   resource|NULL|\*RECURSION\*)  #
  168.                   (?:\((.*?)\))?                #     - complement [9]
  169.                   (?:\s+(?:                     #     - value [10-11]
  170.                     (?:"(.*?)") |               #       . string [10] OR
  171.                     (?:of\ type\ \((.*?)\))     #       . resource [11]
  172.                   ))?                           #
  173.                 )                               #
  174.                   |                             #   OR
  175.                 (})                             #   = End of array [12]
  176.                   |                             #   OR
  177.                 (?:(&?(?:array|object).*)\ {)   #   = Start of array [13]
  178.               )                                 # end of alternative
  179.             $/Smx',
  180.             $variable,
  181.             $matches,
  182.             PREG_SET_ORDER
  183.         );
  184.  
  185.         $stack = array();
  186.         $keyLen = array();
  187.         $maxLen = 0;
  188.  
  189.         foreach($matches AS $match{
  190.             $count count($match);
  191.             if ($count>=4 and $count<=14{
  192.                 $depth[strlen($match[1]>> 1;
  193.                 switch ($count{
  194.                     case 4:
  195.  
  196.                         $len strlen($match[3]);
  197.                         if ($len $maxLen{
  198.                             $maxLen $len;
  199.                         }
  200.  
  201.                         if (empty($match[2])) {
  202.                             $family[VAR_DUMP_START_ELEMENT_NUM;
  203.                         else {
  204.                             $family[VAR_DUMP_START_ELEMENT_STR;
  205.                                               }
  206.                         $type[= NULL;
  207.                         $value[$match[3];
  208.  
  209.                         break;
  210.                     case 6: 
  211.                         $variation += substr_count($match[5]"\n");
  212.                         $family[VAR_DUMP_FINISH_ELEMENT_NL;
  213.                         $type[$match[4];
  214.                         $value[$match[5];
  215.                         break;
  216.                     case 7:
  217.                     case 8:
  218.                     case 9:
  219.                     case 10:
  220.                     case 11:
  221.                     case 12:
  222.                         $family[VAR_DUMP_FINISH_ELEMENT;
  223.                         switch ($match[8]{
  224.                           case 'bool':
  225.                           case 'int':
  226.                           case 'float':
  227.                               $type[$match[7$match[8];
  228.                               $value[$match[9];
  229.                               break;
  230.                           case 'string':
  231.                               $type[$match[7$match[8'(' $match[9')';
  232.                               $value[$match[10];
  233.                               break;
  234.                           case 'resource':
  235.                               $type[$match[7$match[8'(' $match[11')';
  236.                               $value[$match[9];
  237.                               break;
  238.                           default:
  239.                               $type[$match[7].$match[8];
  240.                               $value['';
  241.                               break;
  242.                         }
  243.                         break;
  244.                     case 13:
  245.  
  246.                         $oldLen array_pop($stack);
  247.                         $keyLen[$oldLen[0]] $maxLen;
  248.                         $maxLen $oldLen[1];
  249.  
  250.                         $family[VAR_DUMP_FINISH_GROUP;
  251.                         $type[= NULL;
  252.                         $value[$match[$count-1];
  253.  
  254.                         break;
  255.                     case 14:
  256.  
  257.                         array_push($stackarray(count($family)$maxLen));
  258.                         $maxLen = 0;
  259.  
  260.                         $family[VAR_DUMP_START_GROUP;
  261.                         $type[= NULL;
  262.                         $value[$match[$count-1];
  263.  
  264.                         break;
  265.                 }
  266.             }
  267.         }
  268.  
  269.         // Check if there was a parse error
  270.         if (count($matches$variation != count(explode("\n"$variable)) - 1{
  271.             PEAR::raiseError('Var_Dump: parse error, the resulting matches do not match the initial variable.'FALSEPEAR_ERROR_TRIGGER);
  272.         }
  273.  
  274.         $this->renderer->initialize($family$depth$type$value$keyLen);
  275.                 
  276.         return $this->renderer->toString();
  277.  
  278.     }
  279.     // }}}
  280.  
  281.     // {{{ singleton()
  282.     /**
  283.      * Attempt to return a concrete singleton Var_Dump instance.
  284.          * The singleton approach must be used in relationship with the
  285.          * displayInit() and display() methods.
  286.      * See Var_Dump/Renderer/*.php for the complete list of options
  287.      * @see Var_Dump::display(), Var_Dump::displayInit()
  288.      * @return object Var_Dump instance
  289.      * @access public
  290.      */
  291.     function singleton()
  292.     {
  293.         static $instance;
  294.         if (!isset($instance)) {
  295.             $instance = new Var_Dump(array()array());
  296.         }
  297.         return $instance;
  298.     }
  299.     // }}}
  300.  
  301.     // {{{ displayInit()
  302.     /**
  303.      * Initialise the singleton object used by the display() method.
  304.      * @see Var_Dump::singleton(), Var_Dump::display()
  305.      * @param array $options         Global parameters.
  306.      * @param array $rendererOptions Parameters for the rendering.
  307.      * @access public
  308.      */
  309.     function displayInit($options = array()$rendererOptions = array())
  310.     {
  311.         $displayInit Var_Dump::singleton();
  312.                 $displayInit->Var_Dump($options$rendererOptions);
  313.     }
  314.     // }}}
  315.  
  316.     // {{{ display()
  317.     /**
  318.      * Outputs or returns a string representation of a variable.
  319.      * @see Var_Dump::singleton(), Var_Dump::displayInit()
  320.      * @param mixed $expression The variable to parse.
  321.      * @param bool  $return     Whether the variable should be echoed or returned.
  322.      * @return string If returned, the string representation of the variable.
  323.      * @access public
  324.      */
  325.     function display($expression$return=FALSE)
  326.     {
  327.         $display Var_Dump::singleton();
  328.         if ($return{
  329.             return $display->toString($expression);
  330.         else {
  331.             echo $display->toString($expression);
  332.         }
  333.     }
  334.     // }}}
  335.  
  336. }
  337.  
  338. ?>

Documentation generated on Mon, 11 Mar 2019 10:16:32 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.