Source for file class.mpdebug.php
Documentation is available at class.mpdebug.php
* Contains a debug class used as a helper during development.
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
defined('CON_FRAMEWORK') or die('Illegal call');
* Debug class. Provides a possibility to debug variables without destroying the page
* If debugging is enabled, the output will be a small collapsable mpWebDebug bar
* at the top left corner of the page.
* Inspired by the Web Debug Toolbar of symfony Framework whith a simple way to
* provide debug informations during development.
* See example1.php and example2.php in delivered package.
* // include the class file
* require_once('class.mpdebug.php');
* // get instance of the mpdebug class
* $mpDebug = mpDebug::getInstance();
* 'ressource_urls' => array('/path_to_logs/error.txt'), // this is a not working exaqmple ;-)
* 'dump_super_globals' => array('$_GET', '$_POST', '$_COOKIE', '$_SESSION'),
* 'ignore_empty_superglobals' => true,
* $mpDebug->setConfig($options);
* $mpDebug->addDebug($foo, 'Content of foo');
* $bar = array('win', 'nix', 'apple');
* $mpDebug->addDebug($bar, 'win, nix and apple', __FILE__, __LINE__);
* // and at the end of the page (before closing body-Tag)...
* echo $mpDebug->getResults();
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
* Array to store dumps of variables, as follows:
* - $_dumpCache[pos]['name']
* - $_dumpCache[pos]['value']
* - $_dumpCache[pos]['source']
* - $_dumpCache[pos]['line']
* Flag to trim document root from paths
* Counter 4 added debug items
* Debug links absolute from document-root, e. g. to log files or other docs.
* List of superglobals names to dump automatically.
* Ignore dumping of empty superglobals.
$this->_docRoot =
$_SERVER['DOCUMENT_ROOT'];
if (DIRECTORY_SEPARATOR ==
"\\") {
* Returns a instance of mpDebug (singleton implementation)
if (self::$_instance ==
null) {
self::$_instance =
new mpDebug();
* @param array $options Options array as follows:
* // true or false to enable/disable debugging
* $options['enable'] = true;
* // Array of ressource files which will be linked at the end of debug output. You can add e. g.
* // the HTML path to existing logfiles.
* $options['ressource_urls'] = array('/contenido/logs/errorlog.txt', '/cms/logs/my_own_log.txt');
* // Array superglobals to dump automatically, add each superglobal, but not $GLOBALS
* $options['dump_super_globals'] = array('$_GET', '$_POST', '$_COOKIE', '$_SESSION');
* // Flag to ignore dumpoutput of empty superglobals
* $options['ignore_empty_superglobals'] = true;
* // Magic word to use, if you want to overwrite $options['enable'] option. You can force debugging
* // by using this option. In this case, you can enable it adding the parameter
* // magic_word={my_magic_word} to the URL, e. g.
* // http://domain.tld/mypage.php?magic_word={my_magic_word}
* // After then debugging will be enabled for the next 1 hour (set by cookie)
* $options['magic_word'] = 'foobar';
* // Second way to overwrite option $options['enable']. Here you can define a own function, which
* // should check, if debugging is to enable or not. The function should return a boolean value,
* // true to enable it, or false to disable.
* $options['user_func'] = 'myFunctionName';
//enable/disable debugging
if (isset
($options['enable'])) {
$this->_enable = (bool)
$options['enable'];
if (isset
($options['magic_word'])){
// debug enable check with magic_word send by request
$cookieVal =
md5($options['magic_word']);
if (isset
($_GET['magic_word']) ==
$options['magic_word']) {
@setcookie('mpDebug_mw', $cookieVal, time() +
3600); // enable debugging for 1 hour
} elseif (isset
($_COOKIE['mpDebug_mw']) &&
$_COOKIE['mpDebug_mw'] ==
$cookieVal) {
} elseif (isset
($options['user_func']) &&
is_function($options['user_func'])) {
// debug enable check with userdefined function
if (isset
($options['ressource_urls']) &&
is_array($options['ressource_urls'])) {
$this->_resUrls =
$options['ressource_urls'];
// auto dump of globals or superglobals
if (isset
($options['dump_super_globals']) &&
is_array($options['dump_super_globals'])) {
// ignore dumping of empty superglobals
if (isset
($options['ignore_empty_superglobals'])) {
* Wrapper 4 var_dump function. Dumps content of passed variable.
* @param mixed $var Variable 2 dump content
* @param string $source Name of source
* @param bool $print Flag to print out dump result
* @param bool $decorate Flag to decorate the dump result with pre-Tag
* @return mixed Content of var, if its not to print
public function vdump($var, $source=
'', $print=
true, $decorate=
false) {
if ($source !==
'') { $source .=
":\n"; }
$dump =
'<pre>' .
$dump .
'</pre>';
* @param mixed $var Variable 2 dump
* @param string $name Additional info like name or whatever you wan't do describe the passed variable
* @param string $source Filename (e. g. __FILE__) to specify the location of caller
* @param string $line Line (e. g. __LINE__) to specifiy the line number
public function addVdump($var, $name, $source=
null, $line=
null) {
* Adds a varible dump, does the same as addVdump().
* @param mixed $var Variable 2 dump
* @param string $name Additional info like name or whatever you wan't do describe the passed variable
* @param string $source Filename (e. g. __FILE__) to specify the location of caller
* @param string $line Line (e. g. __LINE__) to specifiy the line number
public function addDebug($var, $name, $source=
null, $line=
null){
* Adds passed variable to the debug cache.
* @param mixed $var Variable 2 dump
* @param string $name Additional info like name or whatever you wan't do describe the passed variable
* @param string $source Filename (e. g. __FILE__) to specify the location of caller
* @param string $line Line (e. g. __LINE__) to specifiy the line number
if (DIRECTORY_SEPARATOR ==
"\\") {
$source =
str_replace(DIRECTORY_SEPARATOR, '/', $source);
$arr['source'] =
$source;
* Main method to get the mpWebDebug Bar.
* Returns or prints the mpWebDebug Bar depending on state of $print
* @param bool $print Flag to print the mpWebDebug Bar
* @return mixed The mpWebDebug Bar if $print is set to false or nothing
if ((is_array($SG) &&
count($SG) ==
0) ||
!isset
($SG) ||
empty($SG)) {
if ($v['source'] !==
null) {
$info .=
'source: ' .
$v['source'] .
"\n";
if ($v['line'] !==
null) {
$info .=
'line: ' .
$v['line'] .
"\n";
<span class="info"><br />
NOTE: This debug output should be visible only on dev environment<br />
© Murat Purc 2008 • <a href="http://www.purc.de/">www.purc.de</a>
* Decorates given data with a html comment and returns it back or prints it out.
* @param string $content Data to decorate with comment
* @param bool $print Flag to print the result
* @return mixed The composed result or nothing if is to print
public function comment($content, $print=
true){
$comment =
"\n<!-- " .
$content .
" -->\n";
* Returns the code (CSS-/ and JavaScript part) for the mpDebugBar, which can be placed inside
* Prevents multiple delivering of the code using a static variable. Only the first call will
* @return string CSS-/JS-Code of mpWebDebug bar
static $alreadyDelivered;
if (isset
($alreadyDelivered)) {
// code for head tag is to deliver once, and it was already delivered, return empty string
$alreadyDelivered =
true;
<style type="text/css"><!--
#mpWebDebug {position:absolute;z-index:1000;width:100%;right:0;top:0;font-size:12px;font-family:arial;text-align:left;}
#mpWebDebug #webDebugAnchorBox {padding:0.2em;background:transparent;}
#mpWebDebug .anchor {font-weight:bold;font-size:0.8em;color:#52bd22;margin:0;padding:0.3em;background:#575555;}
#mpWebDebug .anchor a {font-weight:bold;font-family:arial;color:#52bd22;}
#mpWebDebug #webDebugBox {margin-top:1.3em;padding:5px;position:absolute;left:0;top:0;background-color:#dadada;border:1px black solid;width:99%;overflow:auto;}
#mpWebDebug .outputItemName {margin:0.3em 0; background:#cacaca;}
#mpWebDebug .outputItemName a, #mpWebDebug .outputItemName a:hover {display:block;font-size:9pt;font-family:arial;font-weight:bold;color:black; background:transparent;}
#mpWebDebug .outputItemInfo {text-decoration:underline;}
#mpWebDebug .outputItemValue {padding-left:0.5em;color:#000;}
#mpWebDebug .notVisible {display:none;}
#mpWebDebug .info {color:#007f46;font-size:11px;}
#mpWebDebug pre {margin:0;color:#000;}
<script type="text/javascript"><!-- // <![CDATA[
if (typeof(this.aToggle[id]) == "undefined") {
displayVal = (this.aToggle[id] == "block") ? "none" : "block";
document.getElementById(id).style.display = displayVal;
this.aToggle[id] = displayVal;
} catch(e) {/*alert(e);*/}
* Returns the mpWebDebug bar header.
* @return string Code of the mpWebDebug bar, either with or without CSS/JavaScript code block.
// get head code (css/js)
<div id="webDebugAnchorBox"><span class="anchor">
<a href="javascript:void(0);" onclick="mpWebDebugger.toggle(\'webDebugBox\');return false;">DEBUG</a>
<div id="webDebugBox" class="notVisible">
<div id="jsDebugBox" style="display:none;">
<div class="outputItemName">
<a href="javascript:void(0);" onclick="mpWebDebugger.toggle(\'jsDebug\');return false;">♦ JavaScript</a>
<div id="jsDebug" class="notVisible"></div>
* Prints the content of passed debug item, depending on its type (array, object, string)
* @param string $name Name of the variable
* @param mixed $var The variable itself
* @param string $info Info about the variable
* @return string Container with debug info about the variable
$info_item =
'<div class="outputItemInfo">' .
str_replace("\n", "<br />\n", $info) .
'</div>';
<div class="outputItemName">
<a href="javascript:void(0);" onclick="mpWebDebugger.toggle(\'' .
$id .
'\');return false;">♦ '.
$name.
'</a>
<div id="'.
$id.
'" class="outputItemValue notVisible">
$out .=
$name .
' = is_null';
$out .=
$name .
' = empty';
} elseif (!isset
($var)) {
$out .=
$name .
' = !isset';
$out .=
$name .
' = ' .
$var;
* Creates list of linked ressource files. The result will be added to mpWebDebug.
foreach ($this->_resUrls as $p =>
$url) {
<li><a href="' .
$url .
'" onclick="window.open(this.href);return false;">' .
$url .
'</a></li>
<div class="outputItemName">
<a href="javascript:void(0);" onclick="mpWebDebugger.toggle(\'' .
$id.
'\');return false;">♦ Ressource Links</a>
<div id="' .
$id .
'" class="notVisible">
* Returns the footer of mpWebDebug
* @param mixed $var Variable to dump content
* Prepares data dump for output. Clears some white space characters and line endings to get an
* compact an more readable result.
* @param string $dumpOutput Output of var_dump()
$dumpOutput =
preg_replace("/(Array\n)([\s{1,*}]*)(\()/", 'Array (', $dumpOutput);
$dumpOutput =
preg_replace("/(Array\(\n)([\s{1,*}]*)(\))/", 'Array ()', $dumpOutput);
$dumpOutput =
preg_replace("/(Object\n)([\s{1,*}]*)(\()/", 'Object (', $dumpOutput);
$dumpOutput =
preg_replace("/(Object\(\n)([\s{1,*}]*)(\))/", 'Object ()', $dumpOutput);
* Returns the Superglobal variable by name. Provides a precheck of superglobal
* size to prevend debugging variables having several MB. e. g. $_POST.
* Does not support the return of $GLOBALS. This variable contains since
* PHP 5 several cross references, therefore a dump will result in crossing
* memory limit or script timeout.
* @param string $name Name of superglobal
* @return mixed Content of superglobal or a message or null
// simple check of post size
// content of _POST is > 512 KB
return 'Content of POST seems to be big, approximate calculated size is ' .
$sizeKB .
' KB';
// simple check of session size
// content of _SESSION is > 512 KB
return 'Content of SESSION seems to be big, approximate calculated size is ' .
$sizeKB .
' KB';
* Returns approximate size of superglobal in kb if size is > 512 kb or false
* @return mixed Size in kb or false
// simple check of variable size
* Creates a unique id used as id-Attribute for HTML elements using timer and internal counter.
* @return string Generated id
* Contenido debug class, extends mpDebug and adds logging feature by using a wrapper for
* DebuggerFactory::getDebugger('file')->show($var, $msg);
* @author Murat Purc <murat@purc.de>
* @copyright © Murat Purc 2008
* @license http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
* Returns a instance of Contenido_mpDebug (singleton implementation)
* @return Contenido_mpDebug
if (parent::$_instance ==
null) {
cInclude('classes', 'Debug/DebuggerFactory.class.php');
parent::$_instance =
new Contenido_mpDebug();
return parent::$_instance;
* Wrapper for logging a debug message.
* @param mixed $var Variable to log content
* @param string $msg Message to describe the variable
public function log($var, $msg=
'') {
if ($this->_enable ==
false) {
DebuggerFactory::getDebugger('file')->show($var, $msg);
Documentation generated on Sun, 08 Feb 2009 22:00:45 +0100 by phpDocumentor 1.4.1