[1.1.1] Error out if PEAR is not enabled on the system, include the test-settings...
[htmlpurifier.git] / tests / Debugger.php
blob3213af3cac526680be2507c9cbf19028024c1232
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 /**
58 * The debugging singleton. Most interesting stuff happens here.
60 class Debugger
63 var $shouldPaint = false;
64 var $paints = 0;
65 var $current_scopes = array();
66 var $scope_nextID = 1;
67 var $add_pre = true;
69 function Debugger() {
70 $this->add_pre = !extension_loaded('xdebug');
73 function &instance() {
74 static $soleInstance = false;
75 if (!$soleInstance) $soleInstance = new Debugger();
76 return $soleInstance;
79 function paintIf($mixed, $conditional) {
80 if (!$conditional) return;
81 $this->paint($mixed);
84 function paintWhen($mixed, $scopes = array()) {
85 if (!$this->isInScopes($scopes)) return;
86 $this->paint($mixed);
89 function paintIfWhen($mixed, $conditional, $scopes = array()) {
90 if (!$conditional) return;
91 if (!$this->isInScopes($scopes)) return;
92 $this->paint($mixed);
95 function paint($mixed) {
96 $this->paints++;
97 if($this->add_pre) echo '<pre>';
98 var_dump($mixed);
99 if($this->add_pre) echo '</pre>';
102 function addScope($id = false) {
103 if ($id == false) {
104 $id = $this->scope_nextID++;
106 $this->current_scopes[$id] = true;
109 function removeScope($id) {
110 if (isset($this->current_scopes[$id])) unset($this->current_scopes[$id]);
113 function resetScopes() {
114 $this->current_scopes = array();
115 $this->scope_nextID = 1;
118 function isInScopes($scopes = array()) {
119 if (empty($this->current_scopes)) {
120 return false;
122 if (!is_array($scopes)) {
123 $scopes = array($scopes);
125 foreach ($scopes as $scope_id) {
126 if (empty($this->current_scopes[$scope_id])) {
127 return false;
130 if (empty($scopes)) {
131 if ($this->scope_nextID == 1) {
132 return false;
134 for($i = 1; $i < $this->scope_nextID; $i++) {
135 if (empty($this->current_scopes[$i])) {
136 return false;
140 return true;