Debugger.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. <?php
  2. /**
  3. * Debugging tools.
  4. *
  5. * This file gives a developer a set of tools useful for performing code
  6. * consistency checks. This includes scoping code blocks to ensure that
  7. * only the interesting iteration of a loop gets outputted, a paint()
  8. * function that acts like var_dump() with pre tags, and conditional
  9. * printing.
  10. */
  11. /*
  12. TODO
  13. * Integrate into SimpleTest so it tells us whether or not there were any
  14. not cleaned up debug calls.
  15. * Custom var_dump() that ignores blacklisted properties
  16. * DEPRECATE AND REMOVE ALL CALLS!
  17. */
  18. /**#@+
  19. * Convenience global functions. Corresponds to method on Debugger.
  20. */
  21. function paint($mixed) {
  22. $Debugger =& Debugger::instance();
  23. return $Debugger->paint($mixed);
  24. }
  25. function paintIf($mixed, $conditional) {
  26. $Debugger =& Debugger::instance();
  27. return $Debugger->paintIf($mixed, $conditional);
  28. }
  29. function paintWhen($mixed, $scopes = array()) {
  30. $Debugger =& Debugger::instance();
  31. return $Debugger->paintWhen($mixed, $scopes);
  32. }
  33. function paintIfWhen($mixed, $conditional, $scopes = array()) {
  34. $Debugger =& Debugger::instance();
  35. return $Debugger->paintIfWhen($mixed, $conditional, $scopes);
  36. }
  37. function addScope($id = false) {
  38. $Debugger =& Debugger::instance();
  39. return $Debugger->addScope($id);
  40. }
  41. function removeScope($id) {
  42. $Debugger =& Debugger::instance();
  43. return $Debugger->removeScope($id);
  44. }
  45. function resetScopes() {
  46. $Debugger =& Debugger::instance();
  47. return $Debugger->resetScopes();
  48. }
  49. function isInScopes($array = array()) {
  50. $Debugger =& Debugger::instance();
  51. return $Debugger->isInScopes($array);
  52. }
  53. /**#@-*/
  54. /**
  55. * The debugging singleton. Most interesting stuff happens here.
  56. */
  57. class Debugger
  58. {
  59. public $shouldPaint = false;
  60. public $paints = 0;
  61. public $current_scopes = array();
  62. public $scope_nextID = 1;
  63. public $add_pre = true;
  64. public function Debugger() {
  65. $this->add_pre = !extension_loaded('xdebug');
  66. }
  67. public static function &instance() {
  68. static $soleInstance = false;
  69. if (!$soleInstance) $soleInstance = new Debugger();
  70. return $soleInstance;
  71. }
  72. public function paintIf($mixed, $conditional) {
  73. if (!$conditional) return;
  74. $this->paint($mixed);
  75. }
  76. public function paintWhen($mixed, $scopes = array()) {
  77. if (!$this->isInScopes($scopes)) return;
  78. $this->paint($mixed);
  79. }
  80. public function paintIfWhen($mixed, $conditional, $scopes = array()) {
  81. if (!$conditional) return;
  82. if (!$this->isInScopes($scopes)) return;
  83. $this->paint($mixed);
  84. }
  85. public function paint($mixed) {
  86. $this->paints++;
  87. if($this->add_pre) echo '<pre>';
  88. var_dump($mixed);
  89. if($this->add_pre) echo '</pre>';
  90. }
  91. public function addScope($id = false) {
  92. if ($id == false) {
  93. $id = $this->scope_nextID++;
  94. }
  95. $this->current_scopes[$id] = true;
  96. }
  97. public function removeScope($id) {
  98. if (isset($this->current_scopes[$id])) unset($this->current_scopes[$id]);
  99. }
  100. public function resetScopes() {
  101. $this->current_scopes = array();
  102. $this->scope_nextID = 1;
  103. }
  104. public function isInScopes($scopes = array()) {
  105. if (empty($this->current_scopes)) {
  106. return false;
  107. }
  108. if (!is_array($scopes)) {
  109. $scopes = array($scopes);
  110. }
  111. foreach ($scopes as $scope_id) {
  112. if (empty($this->current_scopes[$scope_id])) {
  113. return false;
  114. }
  115. }
  116. if (empty($scopes)) {
  117. if ($this->scope_nextID == 1) {
  118. return false;
  119. }
  120. for($i = 1; $i < $this->scope_nextID; $i++) {
  121. if (empty($this->current_scopes[$i])) {
  122. return false;
  123. }
  124. }
  125. }
  126. return true;
  127. }
  128. }
  129. // vim: et sw=4 sts=4