PageRenderTime 58ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/school/605.441.Databases/project/htmlpurifier/tests/Debugger.php

https://github.com/hank/life
PHP | 147 lines | 105 code | 18 blank | 24 comment | 18 complexity | d21aabe52bb60cbc757359342ff6da1e MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, Apache-2.0
  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