Release 2.0.1, merged in 1181 to HEAD.
[htmlpurifier.git] / tests / Debugger.php
blob0fccd041be08d140a5e4988524e9b8cb9d61fbe2
1 <?php
3 /**
4 * Debugging tools.
5 *
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
10 * printing.
14 TODO
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
20 /**#@+
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);
55 /**#@-*/
57 function printTokens($tokens, $index) {
58 $string = '<pre>';
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>]';
66 $string .= '</pre>';
67 echo $string;
70 /**
71 * The debugging singleton. Most interesting stuff happens here.
73 class Debugger
76 var $shouldPaint = false;
77 var $paints = 0;
78 var $current_scopes = array();
79 var $scope_nextID = 1;
80 var $add_pre = true;
82 function Debugger() {
83 $this->add_pre = !extension_loaded('xdebug');
86 /**
87 * @static
89 static function &instance() {
90 static $soleInstance = false;
91 if (!$soleInstance) $soleInstance = new Debugger();
92 return $soleInstance;
95 function paintIf($mixed, $conditional) {
96 if (!$conditional) return;
97 $this->paint($mixed);
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) {
112 $this->paints++;
113 if($this->add_pre) echo '<pre>';
114 var_dump($mixed);
115 if($this->add_pre) echo '</pre>';
118 function addScope($id = false) {
119 if ($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)) {
136 return false;
138 if (!is_array($scopes)) {
139 $scopes = array($scopes);
141 foreach ($scopes as $scope_id) {
142 if (empty($this->current_scopes[$scope_id])) {
143 return false;
146 if (empty($scopes)) {
147 if ($this->scope_nextID == 1) {
148 return false;
150 for($i = 1; $i < $this->scope_nextID; $i++) {
151 if (empty($this->current_scopes[$i])) {
152 return false;
156 return true;