[3.0.0] Upgraded test scripts and other goodies. Also removed some PHP4 cruft.
[htmlpurifier/darkodev.git] / tests / Debugger.php
blob6bc44554419c7316143553a93fb74ac0c3bbed52
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
18 * DEPRECATE AND REMOVE ALL CALLS!
21 /**#@+
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);
56 /**#@-*/
58 function printTokens($tokens, $index = null) {
59 $string = '<pre>';
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>]';
67 $string .= '</pre>';
68 echo $string;
71 /**
72 * The debugging singleton. Most interesting stuff happens here.
74 class Debugger
77 public $shouldPaint = false;
78 public $paints = 0;
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();
90 return $soleInstance;
93 public function paintIf($mixed, $conditional) {
94 if (!$conditional) return;
95 $this->paint($mixed);
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) {
110 $this->paints++;
111 if($this->add_pre) echo '<pre>';
112 var_dump($mixed);
113 if($this->add_pre) echo '</pre>';
116 public function addScope($id = false) {
117 if ($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)) {
134 return false;
136 if (!is_array($scopes)) {
137 $scopes = array($scopes);
139 foreach ($scopes as $scope_id) {
140 if (empty($this->current_scopes[$scope_id])) {
141 return false;
144 if (empty($scopes)) {
145 if ($this->scope_nextID == 1) {
146 return false;
148 for($i = 1; $i < $this->scope_nextID; $i++) {
149 if (empty($this->current_scopes[$i])) {
150 return false;
154 return true;