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 <fpoeydomenge at free dot fr>          |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id$
  20.  
  21. require_once 'Var_Dump/Renderer.php';
  22.  
  23. /**
  24.  * Wrapper for the var_dump function.
  25.  *
  26.  * " The var_dump function displays structured information about expressions
  27.  * that includes its type and value. Arrays are explored recursively
  28.  * with values indented to show structure. "
  29.  *
  30.  * The Var_Dump class captures the output of the var_dump function,
  31.  * by using output control functions, and then uses external renderer
  32.  * classes for displaying the result in various graphical ways :
  33.  * simple text, HTML/XHTML text, HTML/XHTML table, XML, ...
  34.  *
  35.  * @package Var_Dump
  36.  * @category PHP
  37.  * @author Frederic Poeydomenge <fpoeydomenge at free dot fr>
  38.  */
  39.  
  40. define ('VAR_DUMP_START_GROUP',          1);
  41. define ('VAR_DUMP_FINISH_GROUP',         2);
  42. define ('VAR_DUMP_START_ELEMENT_NUM',    3);
  43. define ('VAR_DUMP_START_ELEMENT_STR',    4);
  44. define ('VAR_DUMP_FINISH_ELEMENT',       5);
  45. define ('VAR_DUMP_FINISH_STRING',        6);
  46.  
  47. define ('VAR_DUMP_TYPE_ARRAY',           1);
  48. define ('VAR_DUMP_TYPE_OBJECT',          2);
  49.  
  50. define ('VAR_DUMP_PREG_MATCH',           0);
  51. define ('VAR_DUMP_PREG_SPACES',          1);
  52. define ('VAR_DUMP_PREG_KEY_QUOTE',       2);
  53. define ('VAR_DUMP_PREG_KEY',             3);
  54. define ('VAR_DUMP_PREG_STRING_TYPE',     4);
  55. define ('VAR_DUMP_PREG_STRING_VALUE',    5);
  56. define ('VAR_DUMP_PREG_VALUE',           6);
  57. define ('VAR_DUMP_PREG_VALUE_REFERENCE'7);
  58. define ('VAR_DUMP_PREG_VALUE_TYPE',      8);
  59. define ('VAR_DUMP_PREG_VALUE_COMPL',     9);
  60. define ('VAR_DUMP_PREG_VALUE_RESOURCE'10);
  61. define ('VAR_DUMP_PREG_ARRAY_END',      11);
  62. define ('VAR_DUMP_PREG_ARRAY_START',    12);
  63. define ('VAR_DUMP_PREG_ARRAY_TYPE',     13);
  64. define ('VAR_DUMP_PREG_ARRAY_COUNT',    14);
  65. define ('VAR_DUMP_PREG_STRING_COMPL',   15);
  66.  
  67. class Var_Dump
  68. {
  69.  
  70.     /**
  71.      * Run-time configuration options.
  72.      *
  73.      * @var array 
  74.      * @access public
  75.      */
  76.     var $options = array();
  77.  
  78.     /**
  79.      * Default configuration options.
  80.      *
  81.      * @var array 
  82.      * @access public
  83.      */
  84.     var $defaultOptions = array(
  85.         'display_mode' => 'XHTML_Text'
  86.     );
  87.  
  88.     /**
  89.      * Rendering configuration options.
  90.      *
  91.      * See Var_Dump/Renderer/*.php for the complete list of options
  92.      *
  93.      * @var array 
  94.      * @access public
  95.      */
  96.     var $rendererOptions = array();
  97.  
  98.     /**
  99.      * Rendering object.
  100.      *
  101.      * @var object 
  102.      * @access public
  103.      */
  104.     var $renderer = NULL;
  105.  
  106.     /**
  107.      * Class constructor.
  108.      *
  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.      *
  113.      * @see Var_Dump::toString()
  114.      * @param array $options         Global parameters.
  115.      * @param array $rendererOptions Parameters for the rendering.
  116.      * @access public
  117.      */
  118.     function Var_Dump($options = array()$rendererOptions = array())
  119.     {
  120.         $this->options array_merge (
  121.             $this->defaultOptions,
  122.             $options
  123.         );
  124.         $this->rendererOptions $rendererOptions;
  125.         $this->renderer Var_Dump_Renderer::factory(
  126.             $this->options['display_mode'],
  127.             $this->rendererOptions
  128.         );
  129.     }
  130.  
  131.     /**
  132.      * Attempt to return a concrete Var_Dump instance.
  133.      *
  134.      * The factory approach must be used in relationship with the
  135.      * toString() method.
  136.      * See Var_Dump/Renderer/*.php for the complete list of options
  137.      *
  138.      * @see Var_Dump::toString()
  139.      * @param array $options         Global parameters.
  140.      * @param array $rendererOptions Parameters for the rendering.
  141.      * @access public
  142.      */
  143.     function factory($options = array()$rendererOptions = array())
  144.     {
  145.         $obj new Var_Dump($options$rendererOptions);
  146.         return $obj;
  147.     }
  148.  
  149.     /**
  150.      * Uses a renderer object to return the string representation of a variable.
  151.      *
  152.      * @param mixed $expression The variable to parse.
  153.      * @return string The string representation of the variable.
  154.      * @access public
  155.      */
  156.     function toString($expression)
  157.     {
  158.  
  159.         if (is_null($this->renderer)) {
  160.             return '';
  161.         }
  162.  
  163.         $family = array()// element family
  164.         $depth  = array()// element depth
  165.         $type   = array()// element type
  166.         $value  = array()// element value
  167.  
  168.         // When xdebug is loaded, disable the custom fancy var_dump() function,
  169.         // that is not compatible with the regexp parsing below, by forcing
  170.         // the "html_errors" configuration option to "off"
  171.         if (extension_loaded('xdebug')) {
  172.             ini_set('html_errors''0');
  173.         }
  174.  
  175.         // Captures the output of the var_dump function,
  176.         // by using output control functions.
  177.  
  178.         ob_start();
  179.         var_dump($expression);
  180.         $variable ob_get_contents();
  181.         ob_end_clean();
  182.  
  183.         // When xdebug is loaded, restore the value of the
  184.         // "html_errors" configuration option
  185.         if (extension_loaded('xdebug')) {
  186.             ini_restore('html_errors');
  187.         }
  188.  
  189.         // Regexp that parses the output of the var_dump function.
  190.         // The numbers between square brackets [] are the reference
  191.         // of the captured subpattern, and correspond to the entries
  192.         // in the resulting $matches array.
  193.  
  194.         preg_match_all(
  195.             '!^
  196.               (\s*)                                 # 2 spaces for each depth level
  197.               (?:                                   #
  198.                 (?:\[("?)(.*?)\\2\]=>)              # Key [2-3]
  199.                   |                                 #   or
  200.                 (?:(&?string\(\d+\))\s+"(.*))       # String [4-5]
  201.                   |                                 #   or
  202.                 (                                   # Value [6-10]
  203.                   (&?)                              #   - reference [7]
  204.                   (bool|int|float|resource|         #   - type [8]
  205.                   NULL|\*RECURSION\*|UNKNOWN:0)     #
  206.                   (?:\((.*?)\))?                    #   - complement [9]
  207.                   (?:\sof\stype\s\((.*?)\))?        #   - resource [10]
  208.                 )                                   #
  209.                   |                                 #   or
  210.                 (})                                 # End of array/object [11]
  211.                   |                                 #   or
  212.                 (?:(&?(array|object)\((.+)\).*)\ {) # Start of array/object [12-14]
  213.                   |                                 #   or
  214.                 (.*)                                # String (additional lines) [15]
  215.               )                                     #
  216.             $!Smx',
  217.             $variable,
  218.             $matches,
  219.             PREG_SET_ORDER
  220.         );
  221.  
  222.         // Frees the memory used by the temporary variable
  223.         unset($variable);
  224.  
  225.         // Used to keep the maxLen of the keys for each nested variable.
  226.  
  227.         $stackLen = array();
  228.         $keyLen = array();
  229.         $maxLen = 0;
  230.  
  231.         // Used when matching a string, to count the remaining
  232.         // number of chars before the end of the string.
  233.  
  234.         $countdown = 0;
  235.  
  236.         // Used to keep a pointer (reference) on every
  237.         // variable parsed through the following loop.
  238.  
  239.         $reference $expression;
  240.         $stackReference = array();
  241.         $arrReference = array(
  242.             VAR_DUMP_PREG_STRING_TYPE,
  243.             VAR_DUMP_PREG_STRING_VALUE,
  244.             VAR_DUMP_PREG_ARRAY_START,
  245.             VAR_DUMP_PREG_ARRAY_TYPE,
  246.             VAR_DUMP_PREG_ARRAY_COUNT
  247.         );
  248.  
  249.         // Loop through the matches of the previously defined regexp.
  250.  
  251.         foreach ($matches AS $match{
  252.  
  253.             $count count($match- 1;
  254.  
  255.             // If we are waiting for additional lines of a string, decrease
  256.             // the countdown by the len of the match + 1 (\n),
  257.             // and skip to next loop.
  258.  
  259.             if ($countdown > 0{
  260.                 $countdown -= strlen($match[VAR_DUMP_PREG_MATCH]+ 1;
  261.                 continue;
  262.             }
  263.  
  264.             // If matched a string or the beginning of an array/object,
  265.             // initialize a reference on the variable (=$obj).
  266.  
  267.             if (in_array($count$arrReference)) {
  268.                 if (empty($stackReference)) {
  269.                     $obj $reference;
  270.                 else {
  271.                     list($vtyp$vkeyend($stackReference);
  272.                     switch ($vtyp{
  273.                         case VAR_DUMP_TYPE_ARRAY:
  274.                             $obj $vkey[$reference];
  275.                             break;
  276.                         case VAR_DUMP_TYPE_OBJECT:
  277.                             $obj $vkey->$reference;
  278.                             break;
  279.                     }
  280.                 }
  281.             }
  282.  
  283.             // Find which alternative has been matched in the regexp,
  284.             // by looking at the number of elements in the $match array.
  285.  
  286.             switch ($count{
  287.  
  288.                 // Key
  289.                 //=====
  290.                 // - Compute the maxLen of the keys at the actual depth
  291.                 // - Store the key name in $reference
  292.  
  293.                 case VAR_DUMP_PREG_KEY:
  294.                     $len strlen($match[VAR_DUMP_PREG_KEY]);
  295.                     if ($len $maxLen{
  296.                         $maxLen $len;
  297.                     }
  298.                     if (empty($match[VAR_DUMP_PREG_KEY_QUOTE])) {
  299.                         $family[VAR_DUMP_START_ELEMENT_NUM;
  300.                         $reference = (integer) $match[VAR_DUMP_PREG_KEY];
  301.                     else {
  302.                         $family[VAR_DUMP_START_ELEMENT_STR;
  303.                         $reference = (string) $match[VAR_DUMP_PREG_KEY];
  304.                     }
  305.                     $depth[strlen($match[VAR_DUMP_PREG_SPACES]>> 1;
  306.                     $type[]  = NULL;
  307.                     $value[$match[VAR_DUMP_PREG_KEY];
  308.                     break;
  309.  
  310.                 // String
  311.                 //========
  312.                 // - Set the countdown (remaining number of chars before eol) =
  313.                 //   len of the string - matched len + 1 (final ")
  314.  
  315.                 case VAR_DUMP_PREG_STRING_TYPE:
  316.                 case VAR_DUMP_PREG_STRING_VALUE:
  317.                     $countdown =
  318.                         strlen($obj)
  319.                         - strlen($match[VAR_DUMP_PREG_STRING_VALUE])
  320.                         + 1;
  321.                     $family[VAR_DUMP_FINISH_STRING;
  322.                     $depth[strlen($match[VAR_DUMP_PREG_SPACES]>> 1;
  323.                     $type[$match[VAR_DUMP_PREG_STRING_TYPE];
  324.                     $value[$obj;
  325.                     break;
  326.  
  327.                 // Value
  328.                 //=======
  329.  
  330.                 case VAR_DUMP_PREG_VALUE:
  331.                 case VAR_DUMP_PREG_VALUE_REFERENCE:
  332.                 case VAR_DUMP_PREG_VALUE_TYPE:
  333.                 case VAR_DUMP_PREG_VALUE_COMPL:
  334.                 case VAR_DUMP_PREG_VALUE_RESOURCE:
  335.                     $family[VAR_DUMP_FINISH_ELEMENT;
  336.                     $depth[strlen($match[VAR_DUMP_PREG_SPACES]>> 1;
  337.                     switch ($match[VAR_DUMP_PREG_VALUE_TYPE]{
  338.                         case 'bool':
  339.                         case 'int':
  340.                         case 'float':
  341.                             $type[=
  342.                                 $match[VAR_DUMP_PREG_VALUE_REFERENCE.
  343.                                 $match[VAR_DUMP_PREG_VALUE_TYPE];
  344.                             $value[$match[VAR_DUMP_PREG_VALUE_COMPL];
  345.                             break;
  346.                         case 'resource':
  347.                             $type[=
  348.                                 $match[VAR_DUMP_PREG_VALUE_REFERENCE.
  349.                                 $match[VAR_DUMP_PREG_VALUE_TYPE.
  350.                                 '(' $match[VAR_DUMP_PREG_VALUE_RESOURCE')';
  351.                             $value[$match[VAR_DUMP_PREG_VALUE_COMPL];
  352.                             break;
  353.                         default:
  354.                             $type[=
  355.                                 $match[VAR_DUMP_PREG_VALUE_REFERENCE.
  356.                                 $match[VAR_DUMP_PREG_VALUE_TYPE];
  357.                             $value[= NULL;
  358.                             break;
  359.                     }
  360.                     break;
  361.  
  362.                 // End of array
  363.                 //==============
  364.                 // - Pop the maxLen of the keys off the end of the stack
  365.                 // - Pop the reference on the variable off the end of the stack
  366.                 // - If the last element on the stack is an array(0) or object(0),
  367.                 //   replace it by a standard element
  368.  
  369.                 case VAR_DUMP_PREG_ARRAY_END:
  370.                     $oldLen array_pop($stackLen);
  371.                     $keyLen[$oldLen[0]] $maxLen;
  372.                     $maxLen $oldLen[1];
  373.                     list($vtyp$vkeyarray_pop($stackReference);
  374.                     if (
  375.                         ($family[count($family- 1== VAR_DUMP_START_GROUP)
  376.                             and
  377.                         ($type[count($type- 1=== 0)
  378.                     {
  379.                         $family[count($family- 1VAR_DUMP_FINISH_ELEMENT;
  380.                         $type[count($type- 1$value[count($value- 1];
  381.                         $value[count($value- 1= NULL;
  382.                     else {
  383.                         $family[VAR_DUMP_FINISH_GROUP;
  384.                         $depth[strlen($match[VAR_DUMP_PREG_SPACES]>> 1;
  385.                         $type[= NULL;
  386.                         $value[$match[VAR_DUMP_PREG_ARRAY_END];
  387.                                         }
  388.                     break;
  389.  
  390.                 // Start of array
  391.                 //================
  392.                 // - Push the maxLen of the keys onto the end of the stack
  393.                 // - Initialize new maxLen to 0
  394.                 // - Push the reference on the variable onto the end of the stack
  395.  
  396.                 case VAR_DUMP_PREG_ARRAY_START:
  397.                 case VAR_DUMP_PREG_ARRAY_TYPE:
  398.                 case VAR_DUMP_PREG_ARRAY_COUNT:
  399.                     array_push($stackLenarray(count($family)$maxLen));
  400.                     $maxLen = 0;
  401.                     switch ($match[VAR_DUMP_PREG_ARRAY_TYPE]{
  402.                         case 'array':
  403.                             array_push(
  404.                                 $stackReference,
  405.                                 array(VAR_DUMP_TYPE_ARRAY$obj)
  406.                             );
  407.                             break;
  408.                         case 'object':
  409.                             array_push(
  410.                                 $stackReference,
  411.                                 array(VAR_DUMP_TYPE_OBJECT$obj)
  412.                             );
  413.                             break;
  414.                     }
  415.                     $family[VAR_DUMP_START_GROUP;
  416.                     $depth[strlen($match[VAR_DUMP_PREG_SPACES]>> 1;
  417.                     $type[= (int) $match[VAR_DUMP_PREG_ARRAY_COUNT];
  418.                     $value[$match[VAR_DUMP_PREG_ARRAY_START];
  419.                     break;
  420.  
  421.             // switch ($count)
  422.  
  423.         }
  424.  
  425.         $this->renderer->initialize($family$depth$type$value$keyLen);
  426.  
  427.         $toString $this->renderer->toString();
  428.  
  429.         // Frees the memory used by the matches
  430.         unset($matches);
  431.  
  432.         return $toString;
  433.  
  434.     }
  435.  
  436.     /**
  437.      * Attempt to return a concrete singleton Var_Dump instance.
  438.      *
  439.      * The singleton approach must be used in relationship with the
  440.      * displayInit() and display() methods.
  441.      * See Var_Dump/Renderer/*.php for the complete list of options
  442.      *
  443.      * @see Var_Dump::display(), Var_Dump::displayInit()
  444.      * @return object Var_Dump instance
  445.      * @access public
  446.      */
  447.     function singleton()
  448.     {
  449.         static $instance;
  450.         if (isset($instance)) {
  451.             $instance = new Var_Dump(array()array());
  452.         }
  453.         return $instance;
  454.     }
  455.  
  456.     /**
  457.      * Initialise the singleton object used by the display() method.
  458.      *
  459.      * @see Var_Dump::singleton(), Var_Dump::display()
  460.      * @param array $options         Global parameters.
  461.      * @param array $rendererOptions Parameters for the rendering.
  462.      * @access public
  463.      */
  464.     function displayInit($options = array()$rendererOptions = array())
  465.     {
  466.         $displayInit Var_Dump::singleton();
  467.         $displayInit->Var_Dump($options$rendererOptions);
  468.     }
  469.  
  470.     /**
  471.      * Outputs or returns a string representation of a variable.
  472.      *
  473.      * @see Var_Dump::singleton(), Var_Dump::displayInit()
  474.      * @param mixed $expression The variable to parse.
  475.      * @param bool  $return     Whether the variable should be echoed or returned.
  476.      * @return string If returned, the string representation of the variable.
  477.      * @access public
  478.      */
  479.     function display($expression$return = FALSE)
  480.     {
  481.         $display Var_Dump::singleton();
  482.         if ($return{
  483.             return $display->toString($expression);
  484.         else {
  485.             echo $display->toString($expression);
  486.         }
  487.     }
  488.  
  489. }
  490.  
  491. ?>

Documentation generated on Mon, 11 Mar 2019 13:59:00 -0400 by phpDocumentor 1.4.4. PEAR Logo Copyright © PHP Group 2004.