Return to Snippet

Revision: 39540
at January 18, 2011 06:02 by kendsnyder


Initial Code
<?php

$GLOBALS['ppr_mode'] = 'on';

function ppr_set_mode($mode) {
	// mode = html | off | log
	$GLOBALS['ppr_mode'] = $mode;
}

function ppr() {
	if ($GLOBALS['ppr_mode'] == 'off') {
		return;
	}
	$bt = debug_backtrace();
	$file = str_replace($_SERVER['DOCUMENT_ROOT'].'/','',$bt[0]['file']);
	$line = $bt[0]['line'];
	$context = "ppr from $file($line)\n";

	$args = _ppr_format_args(func_get_args());
	$html = _ppr_get_html($args);
	if ($GLOBALS['ppr_mode'] == 'log') {
		$html = $context . $html;
		error_log(_ppr_html_to_plain($html));
	}
	else {
		echo _ppr_get_style();
		echo '<pre class="ppr">';
		echo $context;
		echo $html;
		echo '</pre>';
	}
}

function ppr_table($rows) {
	if ($GLOBALS['ppr_mode'] != 'html') {
		return;
	}
	echo _ppr_get_style();
	if (count($rows) == 0) {
		echo '<pre class="ppr">[Empty Table]</pre>';
		return;
	}
	echo '<pre class="ppr"><table class="ppr-rs" border="1"><tr>';
	foreach ($rows[0] as $th => $v) {
		echo "<th>$th</th>";
	}
	echo '</tr>';
	foreach ($rows as $row) {
		echo '<tr>';
		foreach ($row as $value) {
			echo '<td>' . _ppr_get_html($value, 1) . '</td>';
		}
		echo '</tr>';
	}
	echo '</table></pre>';
}

function _ppr_get_object_vars($obj, $dump = '') {
	if (method_exists($obj, 'pprGetObjectVars')) {
		return $obj->pprGetObjectVars($dump);
	}
	$props = get_object_vars($obj);
	return $props;
}

function _ppr_format_args($args) {
	if (count($args) == 1) {
		$args = $args[0];
	}
	else {
		$newArgs = array();
		foreach ($args as $i => $a) {
			$newArgs["@arg$i"] = $a;
		}
		$args = $newArgs;
	}
	return $args;
}

function _ppr_get_html($val, $_maxDepth=16, $_level=0, $_isInObject=false, &$_objIdCache=array()) {
	$out = '';
	if ($val === false || $val === true) {
		$out .= _ppr_get_html_bool($val);
	}
	elseif ($val === null) {
		$out .= _ppr_get_html_null();
	}
	elseif (is_resource($val)) {
		$out .= _ppr_get_html_resource($val);
	}
	elseif (is_array($val)) {
		$out .= _ppr_get_html_array($val, $_maxDepth, $_level, $_isInObject, $_objIdCache);
	}
	elseif (is_object($val)) {
		$out .= _ppr_get_html_object($val, $_maxDepth, $_level, $_isInObject, $_objIdCache);
	}
	elseif (is_float($val)) {
		$out .= _ppr_get_html_float($val);
	}
	elseif (is_int($val)) {
		$out .= _ppr_get_html_int($val);
	}
	else {
		$out .= _ppr_get_html_string($val);
	}
	if (!$_isInObject) {
		$out .= "<br />";
	}
	return $out;
}

function _ppr_get_html_bool($val) {
	return '<span class="ppr-bool">' . ($val ? 'true' : 'false') . '</span>';
}

function _ppr_get_html_null() {
	return '<span class="ppr-null">null</span>';
}

function _ppr_get_html_resource($val) {
	ob_start();
	var_dump($val);
	$dump = ob_get_clean();
	preg_match('/^.+\((\d+)\) of type \(([\w_ ]+)\)$/',$dump,$match);
	return '<span class="ppr-resource">Resource #' . $match[1] . ' <span class="ppr-resource-type">'.$match[2].'</span></span>';
}

function _ppr_get_html_array($val, $_maxDepth=16, $_level=0, $_isInObject=false, &$_objIdCache=array()) {
	$indent = str_repeat('&nbsp;&nbsp;&nbsp;',$_level);
	$indentNext = str_repeat('&nbsp;&nbsp;&nbsp;',$_level+1);

	$out = '';
	if ($_maxDepth == 0) {
		$out .= '<span class="ppr-array">Array <span class="ppr-parens">(</span> <span class="ppr-max">Max Depth</span> <span class="ppr-parens">)</span></span>';
	}
	else {
		if (!$_isInObject) {
			$out .= '<span class="ppr-array">Array <span class="ppr-parens">(</span></span><br />';
		}
		foreach ($val as $k => $v) {
			$out .= $indentNext . '<span class="ppr-bracket">[<span class="ppr-array-key">' . htmlentities((string) $k,ENT_QUOTES,'utf-8') . '</span>]</span>';
			$out .= ' <span class="ppr-arrow">=&gt;</span> ' .  _ppr_get_html($v, $_maxDepth-1, $_level+1, false, $_objIdCache);
		}
		if (!$_isInObject) {
			$out .= $indent . '<span class="ppr-parens">)</span>';
		}
	}
	return $out;
}

function _ppr_get_html_object($val, $_maxDepth=16, $_level=0, $_isInObject=false, &$_objIdCache=array()) {
	$indent = str_repeat('&nbsp;&nbsp;&nbsp;',$_level);

	$out = '';
	ob_start();
	var_dump($val);
	$dump = ob_get_clean();
	preg_match('/^\w+\(([\w_]+)\)#(\d+)/',$dump,$match);
	unset($dump);
	$class = $match[1];
	$id = $match[2];
	if ($_maxDepth <= 1 || isset($_objIdCache[$id])) {
		$text = (isset($_objIdCache[$id]) ? 'Recursion' : 'Max Depth');
		$out .= '<span class="ppr-object">Object #' . $id .' <span class="ppr-class">'.$class.'</span> <span class="ppr-brace">{</span> <span class="ppr-max">'.$text.'</span> <span class="ppr-brace">}</span></span>';
	}
	else {
		$_objIdCache[$id] = 1;

		$props = _ppr_get_object_vars($val, $dump);
		$out .= '<span class="ppr-object">Object #' . $id .' <span class="ppr-class">'.$class.'</span> <span class="ppr-brace">{</span></span>';
		if (count($props)) {
			$out .= "<br />";
			$out .= _ppr_get_html($props,$_maxDepth-1,$_level,true,$_objIdCache);
			$out .= $indent;
		}
		$out .= '<span class="ppr-brace">}</span>';
	}
	return $out;
}

function _ppr_get_html_float($val) {
	return '<span class="ppr-float">' . $val . '</span>';
}

function _ppr_get_html_int($val) {
	return '<span class="ppr-int">' . $val . '</span>';
}

function _ppr_get_html_string($val) {
	return '<span class="ppr-string"><span class="ppr-lquot">&ldquo;</span>' . htmlentities((string) $val,ENT_QUOTES) . '<span class="ppr-rquot">&rdquo;</span></span>';
}

function _ppr_get_style() {
	static $style_printed;
	if ($style_printed) {
		return '';
	}
	$style_printed = true;
	return '
<style type="text/css" media="screen">
pre.ppr {
	font-family: "Bitstream Vera Sans Mono", Consolas, "Courier New", monospace;
	font-size: 12px;
	line-height: 130%;
	word-wrap: break-word; /* IE 5.5 - 7 */
	white-space: pre-wrap; /* IE8, EOMB */
	background-color: rgb(255,255,255);
	background-color: rgba(255,255,255,0.92);
	border: 1px solid #888;
	padding: 5px;
	text-align: left;
	clear: both;
}
.ppr-array,
.ppr-object,
.ppr-resource {
	font-style: italic;
}
.ppr-brace,
.ppr-parens {
	font-style: normal;
}
.ppr-string {
	color: #008800;
	font-weight: normal;
}
.ppr-lquot {
	color: #777;
	font-family: "Times New Roman", Times, serif;
	font-weight: bold;
	font-size: 10px;
	margin-right: 2px;
}
.ppr-rquot {
	color: #777;
	font-family: "Times New Roman", Times, serif;
	font-weight: bold;
	font-size: 10px;
	margin-left: 2px;
}
.ppr-arrow {
	color: #777;
	font-weight: bold;
}
.ppr-array-key {
	color: #303030;
	font-weight: normal;
}
.ppr-class,
.ppr-resource-type {
	color: #0055ee;
	font-weight: bold;
	font-style: normal;
}
.ppr-null {
	color: #0000ee;
	font-style: italic;
}
.ppr-bool {
	color: #dd7700;
	font-style: italic;
}
.ppr-bracket {
	color: #888;
}
.ppr-float {
	color: #2200ee;
	font-weight: bold;
}
.ppr-int {
	color: #cc0000;
	font-weight: bold;
}
.ppr-max {
	text-transform: uppercase;
	font-style: italic;
	color: #bb6666;
	font-weight: bold;
}
</style>';
}

function _ppr_html_to_plain($html) {
	$html = str_replace('&ldquo;','"', $html);
	$html = str_replace('&rdquo;','"', $html);
	$html = str_replace('&nbsp;'," ", $html);
	$html = str_replace('<br />',"\n", $html);

	$str = strip_tags($html);
	$str = html_entity_decode($str);
	$str = trim($str);
	return $str;
}

Initial URL

                                

Initial Description

                                

Initial Title
Pretty print_r()

Initial Tags
debug

Initial Language
PHP