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.  
  62.     /**
  63.      * Default configuration options.
  64.      * @var    array 
  65.      * @access public
  66.      */
  67.     var $defaultOptions = array(
  68.         'display_mode' => 'XHTML_Text'
  69.     );
  70.  
  71.     /**
  72.      * Rendering configuration options, one entry for each renderer.
  73.      * See Var_Dump/Renderer/*.php for the complete list of options
  74.      * @var    array 
  75.      * @access public
  76.      */
  77.     var $rendererOptions = array();
  78.  
  79.     /**
  80.      * Rendering object.
  81.      * @var    object 
  82.      * @access public
  83.      */
  84.     var $renderer = NULL;
  85.  
  86.     // }}}
  87.  
  88.     // {{{ constructor
  89.     /**
  90.      * Class constructor.
  91.      * See Var_Dump/Renderer/*.php for the complete list of options
  92.      * @param  array  $options         Global parameters.
  93.      * @param  array  $rendererOptions Parameters for the rendering.
  94.      * @access public
  95.      */
  96.     function Var_Dump($options = array()$rendererOptions = array())
  97.         {
  98.         $this->setOptions($options);
  99.         $displayMode=$this->options['display_mode'];
  100.         if (!isset($this->rendererOptions[$displayMode])) {
  101.             $this->rendererOptions[$displayMode$rendererOptions;
  102.         else {
  103.             $this->rendererOptions[$displayModearray_merge(
  104.                 $this->rendererOptions[$displayMode],
  105.                 $rendererOptions
  106.             );
  107.         }
  108.         $this->renderer Var_Dump_Renderer::singleton(
  109.             $displayMode,
  110.             $this->rendererOptions[$displayMode]
  111.         );
  112.     }
  113.     // }}}
  114.     
  115.     // {{{ factory()
  116.     /**
  117.      * Attempt to return a concrete Var_Dump instance.
  118.      * See Var_Dump/Renderer/*.php for the complete list of options
  119.      * @param  array  $options         Global parameters.
  120.      * @param  array  $rendererOptions Parameters for the rendering.
  121.      * @access public
  122.      */
  123.     function factory($options = array()$rendererOptions = array())
  124.     {
  125.         $obj new Var_Dump($options$rendererOptions);
  126.         return $obj;
  127.     }
  128.     // }}}
  129.  
  130.     // {{{ singleton()
  131.     /**
  132.      * Attempt to return a concrete singleton Var_Dump instance.
  133.      * See Var_Dump/Renderer/*.php for the complete list of options
  134.      * @param  array  $options         Run-time configuration options.
  135.      * @param  array  $rendererOptions Parameters for the rendering.
  136.      * @access public
  137.      */
  138.     function singleton($options = array()$rendererOptions = array())
  139.     {
  140.         static $instance;
  141.         if (!isset($instance)) {
  142.             $instance = new Var_Dump($options$rendererOptions);
  143.         else {
  144.             $instance->Var_Dump($options$rendererOptions);
  145.         }
  146.         return $instance;
  147.     }
  148.     // }}}
  149.  
  150.     // {{{ setOptions()
  151.     /**
  152.      * Set run-time configuration options for the renderer
  153.      * @param  array  $options Run-time configuration options.
  154.      * @access public
  155.      */
  156.     function setOptions($options = array())
  157.     {
  158.         $this->options array_merge (
  159.             $this->defaultOptions,
  160.             $this->options,
  161.             $options
  162.         );
  163.     }
  164.     // }}}
  165.  
  166.     // {{{ display()
  167.     /**
  168.      * Outputs or returns a string representation of a variable.
  169.      * @param  mixed  $expression The variable to parse.
  170.      * @param  bool   $return     Whether the variable should be echoed or returned.
  171.      * @return string             If returned, the string representation of the variable.
  172.      * @access public
  173.      */
  174.     function display($expression$return=FALSE)
  175.     {
  176.         $display Var_Dump::singleton();
  177.         if ($return{
  178.             return $display->toString($expression);
  179.         else {
  180.             echo $display->toString($expression);
  181.         }
  182.     }
  183.     // }}}
  184.  
  185.     // {{{ toString()
  186.     /**
  187.      * Uses a renderer object to return the string representation of a variable.
  188.      * @param  mixed  $expression The variable to parse.
  189.      * @return string             The string representation of the variable.
  190.      * @access public
  191.      */
  192.     function toString($expression)
  193.     {
  194.  
  195.         if (PEAR::isError($this->renderer)) {
  196.             return '';
  197.         }
  198.  
  199.         $family = array()// element family
  200.         $depth  = array()// element depth
  201.         $type   = array()// element type
  202.         $value  = array()// element value
  203.  
  204.         ob_start();
  205.         var_dump($expression);
  206.         $variable ob_get_contents();
  207.         ob_end_clean();
  208.  
  209.         // The numbers between the square brackets [] are the reference of the captured subpattern
  210.         // and correspond to the entries in the resulting $matches array
  211.         preg_match_all(
  212.             '/^
  213.                 (\s*)                             # 2 spaces for each depth level
  214.                 (?:                               # start of alternative
  215.                   (?:\[("?)(.*?)\\2\]=>)          #   = Key [2-3]
  216.                     |                             #   OR
  217.                   (?:                             #   = Value, multiline [4-5]
  218.                       (string\([0-9]*\))          #     - type [4]
  219.                       \s+"(.*[^"]$\n              #     - value [5], first line (no " at the end)
  220.                       (?:^.*[^"]$\n)*             #     - value [5], multiple middle line (no " at the end)
  221.                       ^.*)"                       #     - value [5], last line (closing ")
  222.                   )                               #
  223.                     |                             #   OR
  224.                   (                               #   = Value [6-10]
  225.                     (bool|string|int|float|       #     - type [7]
  226.                      resource|NULL|\*RECURSION\*) #
  227.                     (?:\((.*?)\))?                #     - complement [8]
  228.                     (?:\s+(?:                     #     - value [9-10]
  229.                       (?:"(.*?)") |               #       . string [9] OR
  230.                       (?:of\ type\ \((.*?)\))     #       . resource [10]
  231.                     ))?                           #
  232.                   )                               #
  233.                     |                             #   OR
  234.                   (})                             #   = End of array [11]
  235.                     |                             #   OR
  236.                   (?:(&?(?:array|object).*)\ {)   #   = Start of array [12]
  237.                 )                                 # end of alternative
  238.             $/Smx',
  239.             $variable,
  240.             $matches,
  241.             PREG_SET_ORDER
  242.         );
  243.  
  244.         $stack = array();
  245.         $keyLen = array();
  246.         $maxLen = 0;
  247.  
  248.         foreach($matches AS $match{
  249.             $count count($match);
  250.             if ($count>=4 and $count<=13{
  251.                 $depth[strlen($match[1]>> 1;
  252.                 switch ($count{
  253.                     case 4:
  254.  
  255.                         $len strlen($match[3]);
  256.                         if ($len $maxLen{
  257.                             $maxLen $len;
  258.                         }
  259.  
  260.                         if (empty($match[2])) {
  261.                             $family[VAR_DUMP_START_ELEMENT_NUM;
  262.                         else {
  263.                             $family[VAR_DUMP_START_ELEMENT_STR;
  264.                                               }
  265.                         $type[= NULL;
  266.                         $value[$match[3];
  267.  
  268.                         break;
  269.                     case 6: 
  270.                         $family[VAR_DUMP_FINISH_ELEMENT_NL;
  271.                         $type[$match[4];
  272.                         $value[$match[5];
  273.                         break;
  274.                     case 7:
  275.                     case 8:
  276.                     case 9:
  277.                     case 10:
  278.                     case 11:
  279.                         $family[VAR_DUMP_FINISH_ELEMENT;
  280.                         switch ($match[7]{
  281.                           case 'bool':
  282.                           case 'int':
  283.                           case 'float':
  284.                               $type[$match[7];
  285.                               $value[$match[8];
  286.                               break;
  287.                           case 'string':
  288.                               $type[$match[7].'('.$match[8].')';
  289.                               $value[$match[9];
  290.                               break;
  291.                           case 'resource':
  292.                               $type[$match[7].'('.$match[10].')';
  293.                               $value[$match[8];
  294.                               break;
  295.                           default:
  296.                               $type[$match[7];
  297.                               $value['';
  298.                               break;
  299.                         }
  300.                         break;
  301.                     case 12:
  302.  
  303.                         $oldLen array_pop($stack);
  304.                         $keyLen[$oldLen[0]] $maxLen;
  305.                         $maxLen $oldLen[1];
  306.  
  307.                         $family[VAR_DUMP_FINISH_GROUP;
  308.                         $type[= NULL;
  309.                         $value[$match[$count-1];
  310.  
  311.                         break;
  312.                     case 13:
  313.  
  314.                         array_push($stackarray(count($family)$maxLen));
  315.                         $maxLen = 0;
  316.  
  317.                         $family[VAR_DUMP_START_GROUP;
  318.                         $type[= NULL;
  319.                         $value[$match[$count-1];
  320.  
  321.                         break;
  322.                 }
  323.             }
  324.         }
  325.  
  326.         $this->renderer->initialize(
  327.             $family,
  328.             $depth,
  329.             $type,
  330.             $value,
  331.             $keyLen
  332.         );
  333.                 
  334.         return $this->renderer->toString();
  335.  
  336.     }
  337.     // }}}
  338.  
  339. }
  340.  
  341. ?>

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