<?php
/**
 * A class for debugging.
 *
 * This class provides a variable dumper and timer.
 */
class d
{
    /**
     * @var array The timer flags.
     */
    protected static $flag = array();

    ////////////////////////////////////////////////////////////////////////////
    // Construct and destruct functions.

    ////////////////////////////////////////////////////////////////////////////
    // Overload functions.

    ////////////////////////////////////////////////////////////////////////////
    // Interface functions.

    /**
     * Starts the timer.
     */
    public static function start()
    {
        self::clear();
        self::flag('start');
    }

    /**
     * Sets a flag to the timer.
     * 
     * @param string $caption The flag name.
     */
    public static function flag($caption)
    {
        self::$flag[] = array('caption' => $caption, 'time' => microtime(true), 'memory' => memory_get_usage());
    }

    /**
     * Stops and outputs the timer.
     */
    public static function stop()
    {
        self::flag('stop');
        self::timer();
        self::clear();
    }
    
    /**
     * Clears all the flags.
     */
    public static function clear()
    {
        self::$flag = array();
    }

    /**
     * Dumps a variable.
     * 
     * @param mixed $variable The variable.
     */
    public static function dump($variable)
    {
        FirePHP::getInstance(true)->log($variable);
    }
    
    /**
     * Dumps the super global variables.
     */
    public static function request()
    {
        foreach (array('_GET', '_POST', '_FILES', '_COOKIE', '_SERVER') as $k)
        {
            FirePHP::getInstance(true)->log($GLOBALS[$k], '$' . $k);
        }
        
        if (isset($_SESSION))
        {
            FirePHP::getInstance(true)->log($_SESSION, '$_SESSION');
        }
    }

    ////////////////////////////////////////////////////////////////////////////
    // Internal functions.

    /**
     * Outputs the timer.
     */
    protected static function timer()
    {
        FirePHP::getInstance(true)->log('Timer');
        FirePHP::getInstance(true)->log('');
        $profile = array();
        
        if (self::$flag)
        {
            $previous_time = self::$flag[0]['time'];
            
            foreach (self::$flag as $f)
            {
                $time_difference = $f['time'] - $previous_time;
                $profile[] = array('caption' => $f['caption'], 'time' => $f['time'], 'difference' => $time_difference, 'memory' => $f['memory']);
                $previous_time = $f['time'];
            }
        }
        
        foreach ($profile as $e)
        {
            FirePHP::getInstance(true)->log(str_pad($e['caption'], 30, '.', STR_PAD_RIGHT) . number_format($e['difference'], 6) . ' (' . ($e['memory'] / 1000) . ' Kb)');
        }
        
        self::$firephp->log('');
        
        if (count($profile) >= 2)
        {
            FirePHP::getInstance(true)->log(str_pad('Total', 30, '.', STR_PAD_RIGHT) . number_format($profile[(count($profile) - 1)]['time'] - $profile[0]['time'], 6) . ' (' . (memory_get_peak_usage() / 1000) . ' Kb)');
        }
        else
        {
            FirePHP::getInstance(true)->log((memory_get_peak_usage() / 1000) . ' Kb', 'Peak memory usage');
        }
    }
}
