2006-07-22 19:01:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Debugging tools.
|
|
|
|
*
|
|
|
|
* This file gives a developer a set of tools useful for performing code
|
|
|
|
* consistency checks. This includes scoping code blocks to ensure that
|
|
|
|
* only the interesting iteration of a loop gets outputted, a paint()
|
|
|
|
* function that acts like var_dump() with pre tags, and conditional
|
|
|
|
* printing.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
TODO
|
|
|
|
* Integrate into SimpleTest so it tells us whether or not there were any
|
|
|
|
not cleaned up debug calls.
|
|
|
|
* Custom var_dump() that ignores blacklisted properties
|
2007-11-25 02:24:39 +00:00
|
|
|
* DEPRECATE AND REMOVE ALL CALLS!
|
2006-07-22 19:01:59 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**#@+
|
|
|
|
* Convenience global functions. Corresponds to method on Debugger.
|
|
|
|
*/
|
|
|
|
function paint($mixed) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->paint($mixed);
|
|
|
|
}
|
|
|
|
function paintIf($mixed, $conditional) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->paintIf($mixed, $conditional);
|
|
|
|
}
|
|
|
|
function paintWhen($mixed, $scopes = array()) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->paintWhen($mixed, $scopes);
|
|
|
|
}
|
|
|
|
function paintIfWhen($mixed, $conditional, $scopes = array()) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->paintIfWhen($mixed, $conditional, $scopes);
|
|
|
|
}
|
|
|
|
function addScope($id = false) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->addScope($id);
|
|
|
|
}
|
|
|
|
function removeScope($id) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->removeScope($id);
|
|
|
|
}
|
|
|
|
function resetScopes() {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->resetScopes();
|
|
|
|
}
|
|
|
|
function isInScopes($array = array()) {
|
|
|
|
$Debugger =& Debugger::instance();
|
|
|
|
return $Debugger->isInScopes($array);
|
|
|
|
}
|
|
|
|
/**#@-*/
|
|
|
|
|
2007-06-24 03:34:33 +00:00
|
|
|
|
2006-07-22 19:01:59 +00:00
|
|
|
/**
|
|
|
|
* The debugging singleton. Most interesting stuff happens here.
|
|
|
|
*/
|
|
|
|
class Debugger
|
|
|
|
{
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public $shouldPaint = false;
|
|
|
|
public $paints = 0;
|
|
|
|
public $current_scopes = array();
|
|
|
|
public $scope_nextID = 1;
|
|
|
|
public $add_pre = true;
|
2006-07-22 19:01:59 +00:00
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function Debugger() {
|
2006-07-22 19:01:59 +00:00
|
|
|
$this->add_pre = !extension_loaded('xdebug');
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public static function &instance() {
|
2006-07-22 19:01:59 +00:00
|
|
|
static $soleInstance = false;
|
|
|
|
if (!$soleInstance) $soleInstance = new Debugger();
|
|
|
|
return $soleInstance;
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function paintIf($mixed, $conditional) {
|
2006-07-22 19:01:59 +00:00
|
|
|
if (!$conditional) return;
|
|
|
|
$this->paint($mixed);
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function paintWhen($mixed, $scopes = array()) {
|
2006-07-22 19:01:59 +00:00
|
|
|
if (!$this->isInScopes($scopes)) return;
|
|
|
|
$this->paint($mixed);
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function paintIfWhen($mixed, $conditional, $scopes = array()) {
|
2006-07-22 19:01:59 +00:00
|
|
|
if (!$conditional) return;
|
|
|
|
if (!$this->isInScopes($scopes)) return;
|
|
|
|
$this->paint($mixed);
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function paint($mixed) {
|
2006-07-22 19:01:59 +00:00
|
|
|
$this->paints++;
|
|
|
|
if($this->add_pre) echo '<pre>';
|
|
|
|
var_dump($mixed);
|
|
|
|
if($this->add_pre) echo '</pre>';
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function addScope($id = false) {
|
2006-07-22 19:01:59 +00:00
|
|
|
if ($id == false) {
|
|
|
|
$id = $this->scope_nextID++;
|
|
|
|
}
|
|
|
|
$this->current_scopes[$id] = true;
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function removeScope($id) {
|
2006-07-22 19:01:59 +00:00
|
|
|
if (isset($this->current_scopes[$id])) unset($this->current_scopes[$id]);
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function resetScopes() {
|
2006-07-22 19:01:59 +00:00
|
|
|
$this->current_scopes = array();
|
|
|
|
$this->scope_nextID = 1;
|
|
|
|
}
|
|
|
|
|
2007-11-25 02:24:39 +00:00
|
|
|
public function isInScopes($scopes = array()) {
|
2006-07-22 19:01:59 +00:00
|
|
|
if (empty($this->current_scopes)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!is_array($scopes)) {
|
|
|
|
$scopes = array($scopes);
|
|
|
|
}
|
|
|
|
foreach ($scopes as $scope_id) {
|
|
|
|
if (empty($this->current_scopes[$scope_id])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (empty($scopes)) {
|
|
|
|
if ($this->scope_nextID == 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for($i = 1; $i < $this->scope_nextID; $i++) {
|
|
|
|
if (empty($this->current_scopes[$i])) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|