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

Source for file Debug.php

Documentation is available at Debug.php

  1. <?php
  2. /**
  3. * DotBoost Technologies Inc.
  4. * DotKernel Application Framework
  5. *
  6. @category   DotKernel
  7. @package    DotLibrary
  8. @copyright  Copyright (c) 2009 DotBoost  Technologies (http://www.dotboost.com)
  9. @license    http://opensource.org/licenses/osl-3.0.php  Open Software License (OSL 3.0)
  10. @version    $Id: Debug.php 152 2010-06-18 07:39:40Z teo $
  11. */
  12.  
  13. /**
  14. * Few useful debugging functions . Planned to be replaced by ZfDebug version
  15. @category   DotKernel
  16. @package    DotLibrary
  17. @author     DotKernel Team <team@dotkernel.com>
  18. */
  19.  
  20. class Dot_Debug
  21. {    
  22.     /**
  23.      * Display debugger for db
  24.      * @access public
  25.      * @var bool 
  26.      */    
  27.     public $db_debug = true;
  28.     /**
  29.      * Display details for db debugger on 1st load - if false,
  30.      * the user will need to click on the div to see details
  31.      * @access public
  32.      * @var bool 
  33.      */    
  34.     public $db_details = false;
  35.     /**
  36.      * Allow display for db details
  37.      * @access public
  38.      * @var bool 
  39.      */    
  40.     public $allow_db_details = true;
  41.     /**
  42.      * Show or not total time box
  43.      * @access public
  44.      * @var bool 
  45.      */
  46.     public $total_time = true;
  47.     /**
  48.      * Show or not memory usage box
  49.      * @access public
  50.      * @var bool 
  51.      */    
  52.     public $memory_usage = true;
  53.     /**
  54.      * Constructor
  55.      * @access public
  56.      * @param Zend_Db $db 
  57.      * @param Dot_Template $tpl 
  58.      * @return Dot_Debug 
  59.      */
  60.     public function __construct($db$tpl)
  61.     {
  62.         $this->db $db;
  63.         $this->tpl $tpl;        
  64.     }    
  65.     /**
  66.      * Set magic method
  67.      * @access public
  68.      * @param string $propriety 
  69.      * @param object $value 
  70.      * @return void 
  71.      */
  72.     public function __set($propriety$value)
  73.     {
  74.         $this->$propriety $value;
  75.     }
  76.     /**
  77.      * Count the end time
  78.      * @access public
  79.      * @return string 
  80.      */
  81.     private function endTimer()
  82.     {
  83.         // format start time 
  84.         $stime explode (' '$this->startTimer);
  85.         $startTime $stime[1$stime[0];
  86.         //format end time
  87.         $mtime microtime ();
  88.         $mtime explode (' '$mtime);
  89.         $endTime $mtime[1$mtime[0];
  90.         $totalTime 1000 round (($endTime $startTime)3);
  91.         return $totalTime;
  92.     }
  93.     /**
  94.      * Format the number, show miliseconds
  95.      * @access public
  96.      * @param int $number 
  97.      * @param bool $miliseconds [optional]
  98.      * @return string 
  99.      */
  100.     private function numberFormat ($number$miliseconds false)
  101.     {
  102.         // show miliseconds
  103.         if ($miliseconds)
  104.         {
  105.             return number_format(1000 $number2'.'' ');
  106.         }
  107.         return number_format($number6'.'' ');
  108.     }
  109.     /**
  110.      * Display the debug variables
  111.      * @access public
  112.      * @return void 
  113.      */
  114.     public function show ()
  115.     {
  116.         $this->tpl->setFile('tpl_debugger''../debugger.tpl');
  117.         $this->tpl->setBlock('tpl_debugger''zf_version''zf_version_block');
  118.         $this->tpl->setBlock('tpl_debugger''php_version''php_version_block');
  119.         $this->tpl->setBlock('tpl_debugger''dot_version''dot_version_block');
  120.         $this->tpl->setBlock('tpl_debugger''total_time''total_time_block');
  121.         $this->tpl->setBlock('tpl_debugger''memory_usage''memory_usage_block');
  122.         $this->tpl->setBlock('tpl_debugger''details_db_debug''details_db_debug_block');
  123.         $this->tpl->setBlock('tpl_debugger''db_debug''db_debug_block');
  124.         $this->tpl->setBlock('tpl_debugger''if_params''if_params_block');
  125.         $this->tpl->setBlock('tpl_debugger''no_params''no_params_block');
  126.         $this->tpl->setBlock('tpl_debugger''params''params_block');
  127.         $this->tpl->setBlock('tpl_debugger''queries''queries_block');
  128.         $this->tpl->setBlock('tpl_debugger''if_show_debug''if_show_debug_block');
  129.         
  130.         $this->showZFVersion();
  131.         $this->showPHPVersion();
  132.         $this->showDotVersion();
  133.         
  134.         // if we need db debuger
  135.         if ($this->db_debug)
  136.         {
  137.             $this->showDbDebug();
  138.         }
  139.         if ($this->memory_usage)
  140.         {
  141.             $this->showMemoryUsage();
  142.         }
  143.         // if we need to show total time - put this last so it counts the debug of queries, memory limit, etc
  144.         if ($this->total_time)
  145.         {
  146.             $this->showTotalTime();
  147.         }
  148.         $this->tpl->parse('DEBUGGER''tpl_debugger');        
  149.     }
  150.     /**
  151.      * Display total time
  152.      * @access public
  153.      * @return void 
  154.      */
  155.     private function showTotalTime ()
  156.     {
  157.         $this->tpl->setVar('TOTAL_GENERAL_TIME'$this->endTimer());
  158.         $this->tpl->parse('total_time_block''total_time'true);
  159.     }
  160.     /**
  161.      * Display DB querys for debug
  162.      * @access private
  163.      * @return void 
  164.      */
  165.     private function showDbDebug ()
  166.     {
  167.         $profiler $this->db->getProfiler();
  168.         // lets see if we have the profiler active
  169.         if ($profiler->getEnabled(=== true)
  170.         {
  171.             $this->tpl->setVar('INITIAL_DISPLAY''none');
  172.             // initial status
  173.             if ($this->db_details)
  174.             {
  175.                 $this->tpl->setVar('INITIAL_DISPLAY''block');
  176.             }
  177.  
  178.             $longestTime 0;
  179.             $longestQuery '';
  180.  
  181.             // show queries
  182.             $count 0;
  183.             $profiler_queries $profiler->getQueryProfiles();
  184.             if (is_array($profiler_queries&& count($profiler_queries0)
  185.             {
  186.                 foreach ($profiler_queries as $query)
  187.                 {
  188.                     $bc ($count 21;
  189.                     $this->tpl->setVar('DEBUG_CLASS''debugger_'.$bc);
  190.                     $this->tpl->setVar('QUERY_COUNT'$count++);
  191.                     $this->tpl->setVar('QUERY_TIME'$this->numberFormat($query->getElapsedSecs()true));
  192.                     $this->tpl->setVar('QUERY_TEXT'$query->getQuery());
  193.                     if ($query->getElapsedSecs($longestTime)
  194.                     {
  195.                         $longestTime $query->getElapsedSecs();
  196.                         $longestQuery $query->getQuery();
  197.                     }
  198.  
  199.                     // show query params
  200.                     $queryParams $query->getQueryParams();
  201.                     if (count($queryParams0)
  202.                     {
  203.                         foreach ($queryParams as $key => $val)
  204.                         {
  205.                             $this->tpl->setVar('QUERY_PARAMS'$val);
  206.                             $this->tpl->parse('params_block''params'true);
  207.                         }
  208.                         $this->tpl->parse('if_params_block''if_params'true);
  209.                     }
  210.                     else 
  211.                     {
  212.                         $this->tpl->parse('no_params_block''no_params'true);
  213.                     }
  214.                     $this->tpl->parse('queries_block''queries'true);
  215.                     $this->tpl->parse('if_params_block''');
  216.                     $this->tpl->parse('no_params_block''');
  217.                     $this->tpl->parse('params_block''');
  218.                 }
  219.             }
  220.             $totalTime $profiler->getTotalElapsedSecs();
  221.             $queryCount $profiler->getTotalnumQueries();
  222.             // show aditional information
  223.             $this->tpl->setVar('TOTAL_QUERIES'$queryCount);
  224.             $this->tpl->setVar('TOTAL_TIME'$this->numberFormat($totalTimetrue));
  225.             $this->tpl->setVar('AVERAGE_QUERY_TIME'$this->numberFormat($totalTime $queryCounttrue));
  226.             $this->tpl->setVar('QUERIES_PER_SECOND'ceil($queryCount $totalTime));
  227.             $this->tpl->setVar('LONGEST_QUERY'$longestQuery);
  228.             $this->tpl->setVar('LONGEST_QUERY_TIME'$this->numberFormat($longestTimetrue));
  229.             // parse final blocks
  230.             if ($this->allow_db_details)
  231.             {
  232.                 $this->tpl->parse('details_db_debug_block''details_db_debug'true);
  233.             }
  234.             else 
  235.             {
  236.                 $this->tpl->parse('db_debug_block''db_debug'true);
  237.             }
  238.             $this->tpl->parse('if_show_debug_block''if_show_debug'true);
  239.         }
  240.     }
  241.     /**
  242.      * Display memory usage
  243.      * @access public
  244.      * @return void 
  245.      */
  246.     public function showMemoryUsage ()
  247.     {
  248.         $memory_limit round((memory_get_usage(TRUEpow(106))2);
  249.         $this->tpl->setVar('MEMORY_USAGE'$memory_limit);
  250.         $this->tpl->parse('memory_usage_block''memory_usage'true);
  251.     }
  252.     /**
  253.      * Display ZF Version
  254.      * @access public
  255.      * @return void 
  256.      */
  257.     public function showZFVersion ()
  258.     {
  259.         $this->tpl->setVar('ZF_VERSION'Zend_Version::VERSION);
  260.         $this->tpl->parse('zf_version_block''zf_version'true);
  261.     }
  262.     /**
  263.      * Display PHP version
  264.      * @access public
  265.      * @return void 
  266.      */
  267.     public function showPHPVersion ()
  268.     {
  269.         $this->tpl->setVar('PHP_VERSION'phpversion());
  270.         $this->tpl->parse('php_version_block''php_version'true);
  271.     }
  272.     /**
  273.      * Display DotKernel version
  274.      * @access public
  275.      * @return void 
  276.      */
  277.     public function showDotVersion ()
  278.     {
  279.         $this->tpl->setVar('DOT_VERSION'Dot_Kernel::VERSION);
  280.         $this->tpl->parse('dot_version_block''dot_version'true);
  281.     }
  282. }

Documentation generated on Wed, 21 Jul 2010 07:34:39 +0000 by phpDocumentor 1.4.3