Source for file Debug.php
Documentation is available at Debug.php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
* PHP_Debug : A simple and fast way to debug your PHP code
* The basic purpose of PHP_Debug is to provide assistance in debugging PHP
* code, by 'debug' i don't mean 'step by step debug' but program trace,
* variables display, process time, included files, queries executed, watch
* variables... These informations are gathered through the script execution and
* therefore are displayed at the end of the script (in a nice floating div or a
* html table) so that it can be read and used at any moment. (especially
* usefull during the development phase of a project or in production with a
* Copyright (c) 2007 - Vernet Loïc
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* @author Vernet Loïc <qrf_coil[at]yahoo.fr>
* @copyright 1997-2007 The PHP Group
* @license http://www.opensource.org/licenses/mit-license.php MIT
* @link http://pear.php.net/package/PHP_Debug
* @link http://phpdebug.sourceforge.net
* @link http://www.php-debug.com
* @see Var_Dump, SQL_Parser
* @version CVS: $Id: Debug.php,v 1.6 2009/01/12 21:13:00 c0il Exp $
* Factory class for renderer of Debug class
* @see Debug/Renderer/*.php
require_once 'PHP/DebugLine.php';
require_once 'PHP/Debug/Renderer.php';
* Possible version of class Debug
const VERSION_STANDALONE = 0;
const VERSION_DEFAULT = self ::VERSION_STANDALONE;
const VERSION = self ::VERSION_STANDALONE;
const RELEASE = 'V2.1.5';
const PEAR_RELEASE = 'V1.0.3';
* These are constant for dump() and DumpObj() functions.
* - DUMP_DISP : Tell the function to display the debug info.
* - DUMP_STR : Tell the fonction to return the debug info as a string
* - DUMP_VARNAME : Default name of Array - DBG_ARR_OBJNAME : Default name
const DUMP_VARNAME = 'Variable';
* These are constant for addDebug functions, they set the behaviour where
* the function should add the debug information in first or in last
* These are constants to define Super array environment variables
const GLOBAL_REQUEST = 4;
const GLOBAL_SESSION = 5;
const GLOBAL_GLOBALS = 6;
* Default configuration options
* @since V2.0.0 - 16 apr 2006
protected $defaultOptions = array (
'render_mode' => 'Div', // Renderer mode
'render_type' => 'HTML', // Renderer type
'restrict_access' => false , // Restrict or not the access
'allowed_ip' => array ('127.0.0.1'), // Authorized IP to view the debug when restrict_access is true
'allow_url_access' => false , // Allow to access the debug with a special parameter in the url
'url_key' => 'debug', // Key for url instant access
'url_pass' => 'true', // Password for url instant access
'enable_watch' => false , // Enable the watch function
'replace_errorhandler' => true , // Replace or no the PHP errorhandler
'lang' => 'EN', // Language
* Default static options for static functions
* @since V2.0.0 - 16 apr 2006
protected static $staticOptions = array (
'dump_method' => 'print_r', // print_r or var_dump
'pear_var_dump_method' => 'Var_Dump::display' // Var_Dump display funtion (not used for now)
* Functions from this class that must be excluded in order to have the
* correct backtrace information
* @see PHP_DebugLine::setTraceback()
* @since V2.0.0 - 13 apr 2006
public static $excludedBackTraceFunctions = array (
* Correspondance between super array constant and variable name
* @since V2.0.0 - 18 apr 2006
public static $globalEnvConstantsCorresp = array (
self ::GLOBAL_GET => '_GET',
self ::GLOBAL_POST => '_POST',
self ::GLOBAL_FILES => '_FILES',
self ::GLOBAL_COOKIE => '_COOKIE',
self ::GLOBAL_REQUEST=> '_REQUEST',
self ::GLOBAL_SESSION=> '_SESSION',
self ::GLOBAL_GLOBALS=> 'GLOBALS'
* Default configuration options
* @since V2.0.0 - 13 apr 2006
protected $options = array ();
* This is the array where the debug lines are collected.
* @since V2.0.0 - 11 apr 2006
protected $debugLineBuffer = array ();
* This is the array containing all the required/included files of the
* @since V2.0.0 - 17 apr 2006
* @see render(), PHP_DebugLine::TYPE_TEMPLATES
protected $requiredFiles = array ();
* This is the array containing all the watched variables
* @since V2.0.0 - 16 apr 2006
protected $watches = array ();
* @since V2.0.0 - 11 apr 2006
* @since V2.0.0 - 11 apr 2006
* Number of queries executed during script
* @since V2.0.0 - 19 apr 2006
protected $queryCount = 0;
* PHP_Debug class constructor
* - the execution start time
* - the error and watch call back functions
* @param array $options Array containing options to affect to Debug
* @since V2.0.0 - 11 apr 2006
function __construct ($options = array ())
$this->startTime = PHP_Debug ::getMicroTimeNow ();
$this->options = array_merge($this->defaultOptions, $options);
$this->setWatchCallback ();
$this->setErrorHandler ();
* Add a debug information
* @param string $info The main debug information
* (may be empty for some debug line types)
* @param integer $type Type of the DebugLine
* @since V1.0.0 - 07 Apr 2006
public function addDebug ($info, $type = PHP_DebugLine ::TYPE_STD ,
$position = self ::POSITIONLAST )
if ($position == self ::POSITIONLAST ) {
$this->debugLineBuffer[] = $debugLine;
// Additional process for some types
* Add a debug info before all the existing other debug lines
* It is an alias for addDebug($info, self::POSITIONLAST)
* @since V1.0.0 - 13 Apr 2006
public function addDebugFirst ($info, $type = PHP_DebugLine ::TYPE_STD )
return $this->addDebug ($info, $type, self ::POSITIONFIRST );
* This is an alias for the addDebug function
* @since V2.0.0 - 20 apr 2006
public function add ($info, $type = PHP_DebugLine ::TYPE_STD )
return $this->addDebug ($info, $type);
* This is an alias for the addDebug function when wanting to add a query
* @see addDebug(), PHP_DebugLine::TYPE_QUERY
* @since V2.0.0 - 21 Apr 2006
public function query ($qry)
* This is an alias for the addDebug function when wanting to add a
* database related debug info
* @see addDebug(), PHP_DebugLine::TYPE_QUERYREL
* @since V2.1.0 - 3 apr 2007
public function queryRel ($info)
* This is an alias for the addDebug function when wanting to add an
* @see addDebug(), PHP_DebugLine::TYPE_APPERROR
* @since V2.0.0 - 21 Apr 2006
public function error ($info)
* This is an alias for adding the monitoring of processtime
* @see addDebug(), PHP_DebugLine::TYPE_PROCESSPERF
* @since V2.1.0 - 21 Apr 2006
public function addProcessPerf ()
* This a method to dump the content of any variable and add the result in
* @param mixed $var Variable to dump
* @param string $varname Name of the variable
* @since V2.0.0 - 25 Apr 2006
public function dump ($obj, $varName = '')
* Set the main action of PHP script
* @param string $action Name of the main action of the file
* @since V2.0.0 - 25 Apr 2006
* @see PHP_DebugLine::TYPE_CURRENTFILE
public function setAction ($action)
* Add an application setting
* @param string $action Name of the main action of the file
* @since V2.1.0 - 02 Apr 2007
* @see PHP_DebugLine::TYPE_ENV
public function addSetting ($value, $name)
* Add a group of settings
* @param string $action Name of the main action of the file
* @since V2.1.0 - 2 Apr 2007
* @see PHP_DebugLine::TYPE_ENV
public function addSettings ($values, $name)
* Set the callback fucntion to process the watches, enabled depending of
* the options flag 'enable_watch'
* @since V2.0.0 - 16 apr 2006
* @see options, watches, watchesCallback()
protected function setWatchCallback ()
if ($this->options['enable_watch'] == true ) {
if (count($this->watches) === 0 ) {
$watchMethod = array ($this, 'watchesCallback');
* Set the callback function to process replace the php error handler,
* enabled depending of the options flag 'replace_errorhandler'
* @since V2.0.0 - 16 apr 2006
* @see options, errorHandlerCallback()
protected function setErrorHandler ()
if ($this->options['replace_errorhandler'] == true ) {
* Callback function for php error handling
* Warning : the only PHP error codes that are processed by this user
* handler are : E_WARNING, E_NOTICE, E_USER_ERROR
* For the other error codes the standart php handler will be used
* @since V2.0.0 - 17 apr 2006
* @see options, setErrorHandler()
public function errorHandlerCallback ()
// We already have line & file with setBackTrace function
for ($index = 0; $index < $popNumber; $index++ ) {
if ($details[0 ] != E_STRICT )
* Add a variable to the watchlist. Watched variables must be in a declare
* (ticks=n) block so that every n ticks the watched variables are checked
* for changes. If any changes were made, the new value of the variable is
* @param string $variableName Variable to watch
* @since V2.0.0 - 17 apr 2006
public function watch ($variableName)
if ($this->options['enable_watch'] == true ) {
if (isset ($GLOBALS[$variableName])) {
$this->watches[$variableName] = $GLOBALS[$variableName];
$this->watches[$variableName] = null;
throw new Exception ('The Watch function is disabled please set the option \'enable_watch\' to \'true\' to be able to use this feature, it\'s stable with a Unix server');
* Watch callback function, process watches and add changes to the debug
* @since V2.0.0 - 17 apr 2006
public function watchesCallback ()
// Check if there are variables to watch
if (count($this->watches)) {
foreach ($this->watches as $variableName => $variableValue) {
if ($GLOBALS[$variableName] !== $this->watches[$variableName]) {
$this->watches[$variableName],
$this->watches[$variableName] = $GLOBALS[$variableName];
* Get global process time
* @return float Execution process time of the script
* @since V2.0.0 - 21 Apr 2006
public function getProcessTime ()
return $this->getElapsedTime ($this->startTime, $this->endTime);
* Get database related process time
* @return float Execection process time of the script for all
* database specific tasks
* @see PHP_DebugLine::TYPE_QUERY, PHP_DebugLine::TYPE_QUERYREL
* @since V2.0.0 - 21 Apr 2006
public function getQueryTime ()
foreach($this->debugLineBuffer as $lkey => $lvalue) {
$properties = $lvalue->getProperties ();
if (!empty ($properties['endTime'])) {
$queryTime = $queryTime +
$properties['startTime'],
* PHP_Debug default output function, first we finish the processes and
* then a render object is created and its render method is invoked
* The renderer used is set with the options, all the possible renderer
* are in the directory Debug/Renderer/*.php
* (not the files ending by '_Config.php')
* @since V2.0.0 - 13 apr 2006
$this->endTime = PHP_Debug ::getMicroTimeNow ();
// Render output if we are allowed to
if ($this->isAllowed ()) {
// Create render object and invoke its render function
// Get required files here to have event all Debug classes
return $renderer->render ();
* Alias for the render function
* @since V2.0.0 - 17 apr 2006
public function display ()
* Return the output without displaying it
* @since V2.0.1 - 17 apr 2006
public function getOutput ()
* Restrict access to a list of IP
* @param array $ip Array with IP to allow access
* @since V2.0.0 - 11 Apr 2006
* @see $options, isAllowed()
function restrictAccess ($ip)
$this->options['allowed_ip'] = $ip;
* Test if the client is allowed to access the debug information
* There are several possibilities :
* - 'restrict_access' flag is set to false
* - 'restrict_access' flag is set to true and client IP is the
* allowed ip in the options 'allowed_ip'
* - Access by url is allowed with flag 'allow_url_access' then
* the client must enter the good key and password in the url
* @since V2.0.0 - 20 apr 2006
* @see $options, restrictAcess()
protected function isAllowed ()
if ($this->options['restrict_access'] == true ) {
// Check if client IP is among the allowed ones
$this->options['allowed_ip']
// Check if instant access is allowed and test key and password
elseif ($this->options['allow_url_access'] == true ) {
$key = $this->options['url_key'];
if (!empty ($_GET[$key])) {
if ($_GET[$key] == $this->options['url_pass']) {
// Access is not restricted
* Return microtime from a timestamp
* @param $time Timestamp to retrieve micro time
* @return numeric Microtime of timestamp param
* @since V1.1.0 - 14 Nov 2003
public static function getMicroTime ($time)
list ($usec, $sec) = explode(' ', $time);
return (float) $usec + (float) $sec;
* Alias for getMicroTime(microtime()
* @since V2.0.0 - 19 apr 2006
public static function getMicroTimeNow ()
* Get elapsed time between 2 timestamp
* @param float $timeStart Start time
* @param float $timeEnd End time
* @return float Numeric difference between the two times
* ref in format 00.0000 sec
* @since V1.0.0 - 20 Oct 2003
public static function getElapsedTime ($timeStart, $timeEnd)
return round($timeEnd - $timeStart, 4 );
* Returns Uri prefix, including protocol, hostname and server port.
* @return string Uniform resource identifier prefix
public static function getUriPrefix ()
if (PHP_Debug ::isSecure ()) {
$port = $pathArray['SERVER_PORT'] == $standardPort || !$pathArray['SERVER_PORT'] ? '' : ':'. $pathArray['SERVER_PORT'];
return $proto. '://'. $pathArray['SERVER_NAME']. $port;
* @since V2.1.1 - 23 avr. 2007
public static function isSecure ()
return $_SERVER['SERVER_PORT'] != 80;
* Returns current host name.
* @since V2.1.1 - 23 avr. 2007
public static function getHost ()
return isset ($pathArray['HTTP_X_FORWARDED_HOST']) ? $pathArray['HTTP_X_FORWARDED_HOST'] : (isset ($pathArray['HTTP_HOST']) ? $pathArray['HTTP_HOST'] : '');
* Returns current script name.
* @since V2.1.1 - 23 avr. 2007
public static function getScriptName ()
return isset ($pathArray['SCRIPT_NAME']) ? $pathArray['SCRIPT_NAME'] : (isset ($pathArray['ORIG_SCRIPT_NAME']) ? $pathArray['ORIG_SCRIPT_NAME'] : '');
* Return the query string
* @since 2.1.1 - 23 avr. 2007
public static function getQueryString ()
return $_SERVER['QUERY_STRING'] ? '?'. $_SERVER['QUERY_STRING'] : '';
* @since 2.1.1 - 23 avr. 2007
public static function getUrl ()
return self ::getUriPrefix (). self ::getScriptName (). self ::getQueryString ();
* Set the endtime for a DebugLine in order to monitor the performance
* @see PHP_DebugLine::endTime
* @since V2.0.0 - 19 apr 2006
public function stopTimer ()
$this->debugLineBuffer[count($this->debugLineBuffer)-1 ]->setEndTime ();
* Display the content of any kind of variable
* - Mode PHP_DEBUG_DUMP_ARR_DISP display the array
* - Mode PHP_DEBUG_DUMP_ARR_STR return the infos as a string
* @param mixed $var Variable to dump
* @param string $varname Name of the variable
* @param integer $mode Mode of function
* @param boolean $stopExec Stop the process after display of debug
* @return mixed Nothing or string depending on the mode
* @since V2.0.0 - 25 Apr 2006
public static function dumpVar (
$varName = self ::DUMP_VARNAME ,
$mode = self ::DUMP_DISP ) {
$dumpMethod = self ::$staticOptions['dump_method'];
$dbgBuffer = '<pre><b>dump of \''. $varName. '\'</b> :'.
CR. $dbgBuffer. '</pre>';
case PHP_Debug ::DUMP_STR:
$dieMsg = '<pre><b>Process stopped by PHP_Debug</b>'. CR;
$dieMsg .= $backtrace[0 ]['file'] ? '» file : <b>'.
$backtrace[0 ]['file'] . '</b>'. CR : '';
$dieMsg .= $backtrace[0 ]['line'] ? '» line : <b>'.
$backtrace[0 ]['line'] . '</b>'. CR : '';
$dieMsg .= $backtrace[1 ]['class'] ? '» class : <b>'.
$backtrace[1 ]['class'] . '</b>'. CR : '';
$dieMsg .= $backtrace[1 ]['function'] ? '» function : <b>'.
$backtrace[1 ]['function'] . '</b>'. CR : '';
* @param string $optionsIdx Name of the option to get
* @since V2.0.0 - 13 apr 2006
public function getOption ($optionIdx)
return $this->options[$optionIdx];
* Getter of requiredFiles property
* @return array Array with the included/required files
* @since V2.0.0 - 13 apr 2006
public function getRequiredFiles ()
return $this->requiredFiles;
* Getter of debugString property
* @since V2.0.0 - 13 apr 2006
public function getDebugBuffer ()
return $this->debugLineBuffer;
* Getter of queryCount property
* @since V2.0.0 - 21 Apr 2006
public function getQueryCount ()
return $this->queryCount;
* Debug default output function, simply uses the static dump fonction
* @since V2.0.0 - 11 apr 2006
public function __toString ()
return '<pre>'. PHP_Debug ::dumpVar (
__CLASS__. ' class instance',
Documentation generated on Wed, 14 Jan 2009 16:00:06 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.
|