<?php
    
/**
    * o------------------------------------------------------------------------------o
    * | This package is dual licensed as GPL and a commercial license.               |
    * | If you use the code commercially (or if you don't want to be restricted by   |
    * | the GPL license), you will need the commercial license. It's only £49 (GBP - |
    * | roughly $98 depending on the exchange rate) and helps me out a lot. Thanks.  |
    * o------------------------------------------------------------------------------o
    *
    * © Copyright Richard Heyes
    */

    /**
    * Timer class for debugging
    */
    
class Timer
    
{
        
/**
        * Array of timing information
        * @var array
        */
        
var $times;
    
        
/**
        * Constructor Sets the start time
        */
        
function __construct()
        {
            
$time = explode(' ', microtime());
            
$this->times['Start'] = $time[0] + $time[1];
        }
        
        function
Timer()
        {
            
$this->__construct();
        }
        
        
/**
        * Sets an interval time
        *
        * @param string $label Label for this checkpoint
        */
        
function Interval($label)
        {
            
$time = explode(' ', microtime());
            
$this->times[$label] = $time[0] + $time[1];
        }
        
        
/**
        * Alias for the above
        *
        * @param string $label Label for this checkpoint
        */
        
function CheckPoint($label)
        {
            
$this->interval($label);
        }
        
        
/**
        * Dumps the times in HTML format (by default)
        *
        * @param string $format Format that output is desired in
        */
        
function Dump($format = 'text')
        {
            
$time = explode(' ', microtime());
            
$this->times['End'] = $time[0] + $time[1];
    
            
/**
            * Build the output
            */
            
require_once('Console/Table.php');
            
$table = new Console_Table();
            
$table->setHeaders(array('Checkpoint', 'Time', 'Difference'));
            
            
$lastCheckpoint = 'Start';
            
$lastTime       = 0;
            foreach (
$this->times as $k => $v) {
                
$time = str_pad(sprintf('%0.04f', $v - $this->times[$lastCheckpoint]), 6, '0', STR_PAD_RIGHT);
                
$table->addRow(array($k, $time, str_pad($time - $lastTime, 6, '0', STR_PAD_RIGHT)));
                
$lastTime = $time;
            }
            
            echo
'<pre>' . $table->getTable() . '</pre>';
        }
        
        
/**
        * Aliases for the above
        */
        
function End()    { $this->Dump();}
        function
Finish() { $this->Dump();}
        function
Stop()   { $this->Dump();}
    }
?>