Fix newline issues in tests.
[htmlpurifier/darkodev.git] / tests / Debugger.php
blob91e2161709f7a83caca48b2a5eecf59050340d4f
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 /**#@-*/
59 /**
60 * The debugging singleton. Most interesting stuff happens here.
62 class Debugger
65 public $shouldPaint = false;
66 public $paints = 0;
67 public $current_scopes = array();
68 public $scope_nextID = 1;
69 public $add_pre = true;
71 public function Debugger() {
72 $this->add_pre = !extension_loaded('xdebug');
75 public static function &instance() {
76 static $soleInstance = false;
77 if (!$soleInstance) $soleInstance = new Debugger();
78 return $soleInstance;
81 public function paintIf($mixed, $conditional) {
82 if (!$conditional) return;
83 $this->paint($mixed);
86 public function paintWhen($mixed, $scopes = array()) {
87 if (!$this->isInScopes($scopes)) return;
88 $this->paint($mixed);
91 public function paintIfWhen($mixed, $conditional, $scopes = array()) {
92 if (!$conditional) return;
93 if (!$this->isInScopes($scopes)) return;
94 $this->paint($mixed);
97 public function paint($mixed) {
98 $this->paints++;
99 if($this->add_pre) echo '<pre>';
100 var_dump($mixed);
101 if($this->add_pre) echo '</pre>';
104 public function addScope($id = false) {
105 if ($id == false) {
106 $id = $this->scope_nextID++;
108 $this->current_scopes[$id] = true;
111 public function removeScope($id) {
112 if (isset($this->current_scopes[$id])) unset($this->current_scopes[$id]);
115 public function resetScopes() {
116 $this->current_scopes = array();
117 $this->scope_nextID = 1;
120 public function isInScopes($scopes = array()) {
121 if (empty($this->current_scopes)) {
122 return false;
124 if (!is_array($scopes)) {
125 $scopes = array($scopes);
127 foreach ($scopes as $scope_id) {
128 if (empty($this->current_scopes[$scope_id])) {
129 return false;
132 if (empty($scopes)) {
133 if ($this->scope_nextID == 1) {
134 return false;
136 for($i = 1; $i < $this->scope_nextID; $i++) {
137 if (empty($this->current_scopes[$i])) {
138 return false;
142 return true;