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
18 * DEPRECATE AND REMOVE ALL CALLS!
22 * Convenience global functions. Corresponds to method on Debugger.
24 function paint($mixed) {
25 $Debugger =& Debugger
::instance();
26 return $Debugger->paint($mixed);
28 function paintIf($mixed, $conditional) {
29 $Debugger =& Debugger
::instance();
30 return $Debugger->paintIf($mixed, $conditional);
32 function paintWhen($mixed, $scopes = array()) {
33 $Debugger =& Debugger
::instance();
34 return $Debugger->paintWhen($mixed, $scopes);
36 function paintIfWhen($mixed, $conditional, $scopes = array()) {
37 $Debugger =& Debugger
::instance();
38 return $Debugger->paintIfWhen($mixed, $conditional, $scopes);
40 function addScope($id = false) {
41 $Debugger =& Debugger
::instance();
42 return $Debugger->addScope($id);
44 function removeScope($id) {
45 $Debugger =& Debugger
::instance();
46 return $Debugger->removeScope($id);
48 function resetScopes() {
49 $Debugger =& Debugger
::instance();
50 return $Debugger->resetScopes();
52 function isInScopes($array = array()) {
53 $Debugger =& Debugger
::instance();
54 return $Debugger->isInScopes($array);
58 function printTokens($tokens, $index = null) {
60 $generator = new HTMLPurifier_Generator();
61 foreach ($tokens as $i => $token) {
62 if ($index === $i) $string .= '[<strong>';
63 $string .= "<sup>$i</sup>";
64 $string .= $generator->escape($generator->generateFromToken($token));
65 if ($index === $i) $string .= '</strong>]';
72 * The debugging singleton. Most interesting stuff happens here.
77 public $shouldPaint = false;
79 public $current_scopes = array();
80 public $scope_nextID = 1;
81 public $add_pre = true;
83 public function Debugger() {
84 $this->add_pre
= !extension_loaded('xdebug');
87 public static function &instance() {
88 static $soleInstance = false;
89 if (!$soleInstance) $soleInstance = new Debugger();
93 public function paintIf($mixed, $conditional) {
94 if (!$conditional) return;
98 public function paintWhen($mixed, $scopes = array()) {
99 if (!$this->isInScopes($scopes)) return;
100 $this->paint($mixed);
103 public function paintIfWhen($mixed, $conditional, $scopes = array()) {
104 if (!$conditional) return;
105 if (!$this->isInScopes($scopes)) return;
106 $this->paint($mixed);
109 public function paint($mixed) {
111 if($this->add_pre
) echo '<pre>';
113 if($this->add_pre
) echo '</pre>';
116 public function addScope($id = false) {
118 $id = $this->scope_nextID++
;
120 $this->current_scopes
[$id] = true;
123 public function removeScope($id) {
124 if (isset($this->current_scopes
[$id])) unset($this->current_scopes
[$id]);
127 public function resetScopes() {
128 $this->current_scopes
= array();
129 $this->scope_nextID
= 1;
132 public function isInScopes($scopes = array()) {
133 if (empty($this->current_scopes
)) {
136 if (!is_array($scopes)) {
137 $scopes = array($scopes);
139 foreach ($scopes as $scope_id) {
140 if (empty($this->current_scopes
[$scope_id])) {
144 if (empty($scopes)) {
145 if ($this->scope_nextID
== 1) {
148 for($i = 1; $i < $this->scope_nextID
; $i++
) {
149 if (empty($this->current_scopes
[$i])) {