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.  
  143.         ob_start();
  144.         var_dump($expression);
  145.         $variable ob_get_contents();
  146.         ob_end_clean();
  147.  
  148.         // The numbers between the square brackets [] are the reference of the captured subpattern
  149.         // and correspond to the entries in the resulting $matches array
  150.         preg_match_all(
  151.             '/^
  152.               (\s*)                             # 2 spaces for each depth level
  153.               (?:                               # start of alternative
  154.                 (?:\[("?)(.*?)\\2\]=>)          #   = Key [2-3]
  155.                   |                             #   OR
  156.                 (?:                             #   = Value, multiline [4-5]
  157.                   (&?string\([0-9]*\))          #     - type [4]
  158.                   \s+"(.*[^"]$\n                #     - value [5], first line (no " at the end)
  159.                   (?:^.*[^"]$\n)*               #     - value [5], multiple middle line (no " at the end)
  160.                   ^.*)"                         #     - value [5], last line (closing ")
  161.                 )                               #
  162.                   |                             #   OR
  163.                 (                               #   = Value [6-11]
  164.                   (&?)                          #     - reference [7]
  165.                   (bool|string|int|float|       #     - type [8]
  166.                   resource|NULL|\*RECURSION\*)  #
  167.                   (?:\((.*?)\))?                #     - complement [9]
  168.                   (?:\s+(?:                     #     - value [10-11]
  169.                     (?:"(.*?)") |               #       . string [10] OR
  170.                     (?:of\ type\ \((.*?)\))     #       . resource [11]
  171.                   ))?                           #
  172.                 )                               #
  173.                   |                             #   OR
  174.                 (})                             #   = End of array [12]
  175.                   |                             #   OR
  176.                 (?:(&?(?:array|object).*)\ {)   #   = Start of array [13]
  177.               )                                 # end of alternative
  178.             $/Smx',
  179.             $variable,
  180.             $matches,
  181.             PREG_SET_ORDER
  182.         );
  183.  
  184.         $stack = array();
  185.         $keyLen = array();
  186.         $maxLen = 0;
  187.  
  188.         foreach($matches AS $match{
  189.             $count count($match);
  190.             if ($count>=4 and $count<=14{
  191.                 $depth[strlen($match[1]>> 1;
  192.                 switch ($count{
  193.                     case 4:
  194.  
  195.                         $len strlen($match[3]);
  196.                         if ($len $maxLen{
  197.                             $maxLen $len;
  198.                         }
  199.  
  200.                         if (empty($match[2])) {
  201.                             $family[VAR_DUMP_START_ELEMENT_NUM;
  202.                         else {
  203.                             $family[VAR_DUMP_START_ELEMENT_STR;
  204.                                               }
  205.                         $type[= NULL;
  206.                         $value[$match[3];
  207.  
  208.                         break;
  209.                     case 6: 
  210.                         $family[VAR_DUMP_FINISH_ELEMENT_NL;
  211.                         $type[$match[4];
  212.                         $value[$match[5];
  213.                         break;
  214.                     case 7:
  215.                     case 8:
  216.                     case 9:
  217.                     case 10:
  218.                     case 11:
  219.                     case 12:
  220.                         $family[VAR_DUMP_FINISH_ELEMENT;
  221.                         switch ($match[8]{
  222.                           case 'bool':
  223.                           case 'int':
  224.                           case 'float':
  225.                               $type[$match[7$match[8];
  226.                               $value[$match[9];
  227.                               break;
  228.                           case 'string':
  229.                               $type[$match[7$match[8'(' $match[9')';
  230.                               $value[$match[10];
  231.                               break;
  232.                           case 'resource':
  233.                               $type[$match[7$match[8'(' $match[11')';
  234.                               $value[$match[9];
  235.                               break;
  236.                           default:
  237.                               $type[$match[7].$match[8];
  238.                               $value['';
  239.                               break;
  240.                         }
  241.                         break;
  242.                     case 13:
  243.  
  244.                         $oldLen array_pop($stack);
  245.                         $keyLen[$oldLen[0]] $maxLen;
  246.                         $maxLen $oldLen[1];
  247.  
  248.                         $family[VAR_DUMP_FINISH_GROUP;
  249.                         $type[= NULL;
  250.                         $value[$match[$count-1];
  251.  
  252.                         break;
  253.                     case 14:
  254.  
  255.                         array_push($stackarray(count($family)$maxLen));
  256.                         $maxLen = 0;
  257.  
  258.                         $family[VAR_DUMP_START_GROUP;
  259.                         $type[= NULL;
  260.                         $value[$match[$count-1];
  261.  
  262.                         break;
  263.                 }
  264.             }
  265.         }
  266.  
  267.         $this->renderer->initialize(
  268.             $family,
  269.             $depth,
  270.             $type,
  271.             $value,
  272.             $keyLen
  273.         );
  274.                 
  275.         return $this->renderer->toString();
  276.  
  277.     }
  278.     // }}}
  279.  
  280.     // {{{ singleton()
  281.     /**
  282.      * Attempt to return a concrete singleton Var_Dump instance.
  283.          * The singleton approach must be used in relationship with the
  284.          * displayInit() and display() methods.
  285.      * See Var_Dump/Renderer/*.php for the complete list of options
  286.      * @see Var_Dump::display(), Var_Dump::displayInit()
  287.      * @return object Var_Dump instance
  288.      * @access public
  289.      */
  290.     function singleton()
  291.     {
  292.         static $instance;
  293.         if (!isset($instance)) {
  294.             $instance = new Var_Dump(array()array());
  295.         }
  296.         return $instance;
  297.     }
  298.     // }}}
  299.  
  300.     // {{{ displayInit()
  301.     /**
  302.      * Initialise the singleton object used by the display() method.
  303.      * @see Var_Dump::singleton(), Var_Dump::display()
  304.      * @param array $options         Global parameters.
  305.      * @param array $rendererOptions Parameters for the rendering.
  306.      * @access public
  307.      */
  308.     function displayInit($options = array()$rendererOptions = array())
  309.     {
  310.         $displayInit Var_Dump::singleton();
  311.                 $displayInit->Var_Dump($options$rendererOptions);
  312.     }
  313.     // }}}
  314.  
  315.     // {{{ display()
  316.     /**
  317.      * Outputs or returns a string representation of a variable.
  318.      * @see Var_Dump::singleton(), Var_Dump::displayInit()
  319.      * @param mixed $expression The variable to parse.
  320.      * @param bool  $return     Whether the variable should be echoed or returned.
  321.      * @return string If returned, the string representation of the variable.
  322.      * @access public
  323.      */
  324.     function display($expression$return=FALSE)
  325.     {
  326.         $display Var_Dump::singleton();
  327.         if ($return{
  328.             return $display->toString($expression);
  329.         else {
  330.             echo $display->toString($expression);
  331.         }
  332.     }
  333.     // }}}
  334.  
  335. }
  336.  
  337. ?>

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