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

Source for file VarDump.php

Documentation is available at VarDump.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 4                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2002 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.02 of the PHP license,      |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available at through the world-wide-web at                           |
  11. // | http://www.php.net/license/2_02.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:  Alan Knowles <alan@akbkhome.com>                           |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: VarDump.php 304750 2010-10-25 09:47:38Z clockwerx $
  20. //
  21. // GTK var dump tool
  22. //
  23.  
  24. /**
  25. * simple class to do VarDump in GTK
  26. *
  27. @abstract
  28. *  A simple class that does a regedit type interface for viewing
  29. *  data in a tree like format.
  30. *
  31. *  usage:
  32. *  $data = array(1,2,3,4);
  33. *  $x = new GTK_VarDump($data,'test array');
  34. *
  35. *  thats it!!
  36. *
  37. @see http://cvs.php.net/pear/Gtk_VarDump/examples/example.php
  38. *
  39. @version    $Id: VarDump.php 304750 2010-10-25 09:47:38Z clockwerx $
  40. */
  41. class Gtk_VarDump {
  42.     
  43.     /**
  44.     * aliased array of node points
  45.     * (id => point in tree)
  46.     *
  47.     * @var array 
  48.     * @access private
  49.     */
  50.     var $_nodes = array();
  51.     /**
  52.     * aliased array of gtk nodes (id => gtknode)
  53.     *
  54.     * @var array 
  55.     * @access private
  56.     */
  57.  
  58.     var $_gtkNodes = array();
  59.     
  60.     
  61.     /**
  62.     * Constructor
  63.     *
  64.     * This is all you need!!!
  65.     * 
  66.     * 
  67.     * @param   array|object     tree to display
  68.     * @param   string base name of tree
  69.     * 
  70.     * @access   public
  71.     */
  72.   
  73.     
  74.     function Gtk_VarDump(&$tree,$baseName 'BASE'{
  75.         // it thought there was a rountine in pear somewhere to do this..
  76.         
  77.         if (!extension_loaded('php-gtk')) {
  78.              dl('php_gtk.' .PHP_SHLIB_SUFFIX    )
  79.         }
  80.         $this->_loadInterface($baseName);
  81.         $this->_gtkTree->clear();
  82.         $n $this->_buildTree($tree,0,$baseName,true);
  83.         $this->_addChildren($n,2);
  84.         $this->_display($tree);
  85.         gtk::main();
  86.     }
  87.      /**
  88.     * the main glade object
  89.     *
  90.     * @access private
  91.     */
  92.     var $_glade;  
  93.     /**
  94.     * the main gtk window
  95.     *
  96.     * @access private
  97.     */
  98.     var $_gtkWindow;    
  99.     /**
  100.     * the main gtk Tree
  101.     *
  102.     * @access private
  103.     */
  104.     var $_gtkTree;
  105.     /**
  106.     * the main gtk List
  107.     *
  108.     * @access private
  109.     */
  110.  
  111.     var $_gtkList;
  112.  
  113.         
  114.     /**
  115.     * Set up the interface
  116.     * (loads interface from a glade file - VarDump/interface.glade)
  117.     * @access   private
  118.     * @param    string $title of window
  119.     */
  120.   
  121.     function _loadInterface($title{
  122.         $this->_glade = new GladeXML(dirname(__FILE__).'/VarDump/interface.glade');
  123.         
  124.         $this->_gtkTree $this->_glade->get_widget('ctree');
  125.         $this->_gtkTree->connect_object_after('tree-expand',   array(&$this'_expandTree'));
  126.         $this->_gtkTree->connect_object_after(       'tree-select-row'array(&$this'_selectRow'));
  127.         $this->_gtkTree->set_column_width(180);
  128.         $this->_gtkTree->set_column_auto_resize(0,true);
  129.         $this->_gtkList $this->_glade->get_widget('list');
  130.         
  131.         $OK $this->_glade->get_widget('ok');
  132.         $OK->connect('clicked'array(&$this,'_done'));
  133.         
  134.         $this->_gtkWindow $this->_glade->get_widget('window');
  135.         $this->_gtkWindow->connect('destroy'array(&$this,'_done'));
  136.         $this->_gtkWindow->connect('delete_event'array(&$this,'_deleteEvent'));
  137.         $this->_gtkWindow->set_title("Gtk_VarDump :: $title");
  138.         $this->_setfont('text','-*-helvetica-bold-r-normal-*-*-120-*-*-p-*-iso8859-1');
  139.     }
  140.     /**
  141.     * Set up the font for a widget
  142.     * 
  143.     * @access   private
  144.     * @param   string $widgetname name of widget
  145.     * @param   string $fontname  name of font
  146.     */
  147.     
  148.     function _setFont($widgetname,$fontname{
  149.         $font = gdk::font_load($fontname);
  150.         
  151.         $widget $this->_glade->get_widget($widgetname)
  152.         $oldstyle $widget->get_style();
  153.         $newstyle $oldstyle->copy();
  154.         $newstyle->font = $font;
  155.         $widget->set_style($newstyle);
  156.     }
  157.     
  158.     
  159.     /**
  160.     * Delete event callback
  161.     *
  162.     * @access   private
  163.     */
  164.     
  165.     function _deleteEvent({
  166.         return false;
  167.     }
  168.     /**
  169.     * Quit/Done callback
  170.     *
  171.     * @access   private
  172.     */
  173.  
  174.     function _done({
  175.         $this->_gtkWindow->hide();
  176.         $this->_gtkWindow->destroy();
  177.         gtk::main_quit();
  178.         
  179.     }
  180.      /**
  181.     * Counter used for node id creation
  182.     *
  183.     * @var int 
  184.     * @access private
  185.     */
  186.    
  187.     var $n = 0; // node counter
  188.     /**
  189.     * Children of a node (to be deleted on expansion
  190.     *
  191.     * @var array 
  192.     * @access private
  193.     */
  194.     var $_children  = array();
  195.   
  196.     /**
  197.     * build the Tree
  198.     *
  199.     *
  200.     * @param object|arraynode, the data to show
  201.     * @param int id of the parent.
  202.     * @param string name of node (text)
  203.     * @param bool expanded is it expanded -
  204.     * @access   private
  205.     */
  206.  
  207.   
  208.   
  209.   
  210.     function _buildTree(&$node,$parentId$name,$expanded = false{
  211.         
  212.         // make the node for this object or array..
  213.         
  214.         switch (gettype($node)) {
  215.             case 'object';
  216.                 $col_a $name;
  217.                 $col_b get_class($node);
  218.                 break;
  219.             case 'array':
  220.                 $col_b 'array[' count($node']';
  221.                 $col_a $name;
  222.                 break;
  223.             default:
  224.                 return;
  225.         }
  226.         
  227.         
  228.         
  229.         $this->n++;
  230.         $n $this->n;
  231.         $this->_nodes[$n&$node;
  232.        // echo "ADD NODE($n) TO : $parentId\n";
  233.         $this->_gtkNodes[$n$this->_gtkTree->insert_node(
  234.             @$this->_gtkNodes[$parentId],   // parent
  235.             null,   // sibling
  236.             array($col_a' ',$col_b' '),  //  text to display
  237.             0,      // spacing
  238.             null,   // pixmapclosed
  239.             null,   // maskclosed
  240.             null,   // pixmapopen
  241.             null,   // maskopen
  242.             false,  //is_leaf
  243.             $expanded // expanded
  244.             );
  245.      
  246.         $this->_children[$parentId][$n;
  247.         
  248.         $this->_gtkTree->node_set_row_data($this->_gtkNodes[$n]$n);
  249.        
  250.         // then find out all it's children...
  251.         
  252.         return $n;
  253.     }
  254.       
  255.     /**
  256.     * Add children to a tree
  257.     *
  258.     * and removes old children after new ones are added..
  259.     * 
  260.     * 
  261.     * @param   int $n   base to build on
  262.     * @param   int $decay so it only goes down one level
  263.     * 
  264.     * @access   private
  265.     */
  266.     
  267.     function _addChildren($n,$decay{
  268.         //echo "ADDCHILDREN : $n, $decay\n";
  269.         if ($decay < 0{
  270.             return;
  271.         }
  272.         $remove @$this->_children[$n];
  273.         
  274.         $this->_children[$n= array();
  275.         
  276.         $node &$this->_nodes[$n];
  277.         $type gettype($node);
  278.         switch ($type{
  279.             case 'object':
  280.                 $parts array_keys(get_object_vars($node));
  281.                 foreach($parts as $k{
  282.                     if (!isset($node->$k)) {
  283.                         continue;
  284.                     }
  285.                     $nn $this->_buildTree($node->$k,$n,$k);
  286.                     if ($nn{
  287.                         $this->_addChildren($nn,$decay - 1);
  288.                     }
  289.                     
  290.                 }
  291.                 break;
  292.             case 'array':
  293.                 $parts array_keys($node);
  294.                 foreach($parts as $k{
  295.                     $nn $this->_buildTree($node[$k],$n$k);
  296.                     if ($nn{
  297.                         $this->_addChildren($nn,$decay - 1);
  298.                     }
  299.                 }
  300.                 break;
  301.                 
  302.                     
  303.         }   
  304.       
  305.         if (is_array($remove)) {
  306.             foreach($remove as $k{
  307.                  $this->_gtkTree->remove_node($this->_gtkNodes[$k]);
  308.                  
  309.                 unset($this->_gtkNodes[$k]);
  310.                 unset($this->_nodes[$k]);
  311.             }
  312.         }
  313.                         
  314.     }
  315.     
  316.     /**
  317.     * call back for a node being expanded
  318.     * 
  319.     * @param   gtkNode $gtknode  node that was expanded
  320.     * @access   private
  321.     */
  322.     
  323.     function _expandTree($gtkNode{
  324.         $id $this->_gtkTree->node_get_row_data($gtkNode);
  325.         $this->_addChildren($id,1);
  326.       
  327.          
  328.     }
  329.     /**
  330.     * call back for a node selected
  331.     * 
  332.     * @param   gtkNode $gtknode  node that was selected
  333.     * @access   private
  334.     */
  335.     
  336.     function _selectRow($gtkNode{
  337.         $id $this->_gtkTree->node_get_row_data($gtkNode);
  338.         $this->_display($this->_nodes[$id]);
  339.     }
  340.     /**
  341.     * display the data (except objects/arrays) in the right hand side..
  342.     * 
  343.     * @param   object|array$node  data to display
  344.     * @access   private
  345.     */
  346.  
  347.     function _display(&$node{
  348.         $widget $this->_glade->get_widget('text');
  349.         $this->_gtkList->clear();
  350.         $type gettype($node);
  351.         switch ($type{
  352.             case 'object':  
  353.                 $widget->set_text('Object: 'get_class($node));
  354.                 
  355.                 $parts array_keys(get_object_vars($node))
  356.                 foreach ($parts as $k{
  357.                     switch (gettype($node->$k)) {
  358.                         case 'object':
  359.                         case 'array':
  360.                             continue;
  361.                         case 'string':
  362.                             $this->_gtkList->append(array(' '.$k,' string['.strlen($node->$k']',' '.$node->$k));
  363.                             break;
  364.                         default:
  365.                             $this->_gtkList->append(array(' '.$k,gettype($node->$k),' '.$node->$k));
  366.                     }
  367.                 }
  368.                 break;
  369.             case 'array':
  370.                 $widget->set_text('Array');
  371.                 $parts array_keys($node)
  372.                 foreach ($parts as $k{
  373.                     switch (gettype($node[$k])) {
  374.                         case 'object':
  375.                         case 'array':
  376.                             continue;
  377.                         case 'string':
  378.                             $this->_gtkList->append(array(' '.$k,' string['.strlen($node[$k]']',' '.$node[$k]));
  379.                             break;
  380.                         default:
  381.                             $this->_gtkList->append(array(' '.$k,' '.gettype($node[$k]),' '.$node[$k]));
  382.                     }
  383.                 }
  384.         }
  385.     }
  386.      
  387.     
  388.     
  389.     
  390. }
  391. /*
  392. $t = new StdClass;
  393. $t->test = $GLOBALS;
  394. // test code!
  395. new GTK_VarDump($t,'test');
  396. */

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