6 * This file gives a developer a set of tools useful for performing code
7 * consistency checks. This includes scoping code blocks to ensure that
8 * only the interesting iteration of a loop gets outputted, a paint()
9 * function that acts like var_dump() with pre tags, and conditional
15 * Integrate into SimpleTest so it tells us whether or not there were any
16 not cleaned up debug calls.
17 * Custom var_dump() that ignores blacklisted properties
21 * Convenience global functions. Corresponds to method on Debugger.
23 function paint($mixed) {
24 $Debugger =& Debugger
::instance();
25 return $Debugger->paint($mixed);
27 function paintIf($mixed, $conditional) {
28 $Debugger =& Debugger
::instance();
29 return $Debugger->paintIf($mixed, $conditional);
31 function paintWhen($mixed, $scopes = array()) {
32 $Debugger =& Debugger
::instance();
33 return $Debugger->paintWhen($mixed, $scopes);
35 function paintIfWhen($mixed, $conditional, $scopes = array()) {
36 $Debugger =& Debugger
::instance();
37 return $Debugger->paintIfWhen($mixed, $conditional, $scopes);
39 function addScope($id = false) {
40 $Debugger =& Debugger
::instance();
41 return $Debugger->addScope($id);
43 function removeScope($id) {
44 $Debugger =& Debugger
::instance();
45 return $Debugger->removeScope($id);
47 function resetScopes() {
48 $Debugger =& Debugger
::instance();
49 return $Debugger->resetScopes();
51 function isInScopes($array = array()) {
52 $Debugger =& Debugger
::instance();
53 return $Debugger->isInScopes($array);
57 function printTokens($tokens, $index) {
59 $generator = new HTMLPurifier_Generator();
60 foreach ($tokens as $i => $token) {
61 if ($index == $i) $string .= '[<strong>';
62 $string .= "<sup>$i</sup>";
63 $string .= $generator->escape($generator->generateFromToken($token));
64 if ($index == $i) $string .= '</strong>]';
71 * The debugging singleton. Most interesting stuff happens here.
76 var $shouldPaint = false;
78 var $current_scopes = array();
79 var $scope_nextID = 1;
83 $this->add_pre
= !extension_loaded('xdebug');
89 static function &instance() {
90 static $soleInstance = false;
91 if (!$soleInstance) $soleInstance = new Debugger();
95 function paintIf($mixed, $conditional) {
96 if (!$conditional) return;
100 function paintWhen($mixed, $scopes = array()) {
101 if (!$this->isInScopes($scopes)) return;
102 $this->paint($mixed);
105 function paintIfWhen($mixed, $conditional, $scopes = array()) {
106 if (!$conditional) return;
107 if (!$this->isInScopes($scopes)) return;
108 $this->paint($mixed);
111 function paint($mixed) {
113 if($this->add_pre
) echo '<pre>';
115 if($this->add_pre
) echo '</pre>';
118 function addScope($id = false) {
120 $id = $this->scope_nextID++
;
122 $this->current_scopes
[$id] = true;
125 function removeScope($id) {
126 if (isset($this->current_scopes
[$id])) unset($this->current_scopes
[$id]);
129 function resetScopes() {
130 $this->current_scopes
= array();
131 $this->scope_nextID
= 1;
134 function isInScopes($scopes = array()) {
135 if (empty($this->current_scopes
)) {
138 if (!is_array($scopes)) {
139 $scopes = array($scopes);
141 foreach ($scopes as $scope_id) {
142 if (empty($this->current_scopes
[$scope_id])) {
146 if (empty($scopes)) {
147 if ($this->scope_nextID
== 1) {
150 for($i = 1; $i < $this->scope_nextID
; $i++
) {
151 if (empty($this->current_scopes
[$i])) {