您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

165 行
3.9KB

  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. {
  23. $Debugger =& Debugger::instance();
  24. return $Debugger->paint($mixed);
  25. }
  26. function paintIf($mixed, $conditional)
  27. {
  28. $Debugger =& Debugger::instance();
  29. return $Debugger->paintIf($mixed, $conditional);
  30. }
  31. function paintWhen($mixed, $scopes = array())
  32. {
  33. $Debugger =& Debugger::instance();
  34. return $Debugger->paintWhen($mixed, $scopes);
  35. }
  36. function paintIfWhen($mixed, $conditional, $scopes = array())
  37. {
  38. $Debugger =& Debugger::instance();
  39. return $Debugger->paintIfWhen($mixed, $conditional, $scopes);
  40. }
  41. function addScope($id = false)
  42. {
  43. $Debugger =& Debugger::instance();
  44. return $Debugger->addScope($id);
  45. }
  46. function removeScope($id)
  47. {
  48. $Debugger =& Debugger::instance();
  49. return $Debugger->removeScope($id);
  50. }
  51. function resetScopes()
  52. {
  53. $Debugger =& Debugger::instance();
  54. return $Debugger->resetScopes();
  55. }
  56. function isInScopes($array = array())
  57. {
  58. $Debugger =& Debugger::instance();
  59. return $Debugger->isInScopes($array);
  60. }
  61. /**#@-*/
  62. /**
  63. * The debugging singleton. Most interesting stuff happens here.
  64. */
  65. class Debugger
  66. {
  67. public $shouldPaint = false;
  68. public $paints = 0;
  69. public $current_scopes = array();
  70. public $scope_nextID = 1;
  71. public $add_pre = true;
  72. public function Debugger()
  73. {
  74. $this->add_pre = !extension_loaded('xdebug');
  75. }
  76. public static function &instance() {
  77. static $soleInstance = false;
  78. if (!$soleInstance) $soleInstance = new Debugger();
  79. return $soleInstance;
  80. }
  81. public function paintIf($mixed, $conditional)
  82. {
  83. if (!$conditional) return;
  84. $this->paint($mixed);
  85. }
  86. public function paintWhen($mixed, $scopes = array())
  87. {
  88. if (!$this->isInScopes($scopes)) return;
  89. $this->paint($mixed);
  90. }
  91. public function paintIfWhen($mixed, $conditional, $scopes = array())
  92. {
  93. if (!$conditional) return;
  94. if (!$this->isInScopes($scopes)) return;
  95. $this->paint($mixed);
  96. }
  97. public function paint($mixed)
  98. {
  99. $this->paints++;
  100. if($this->add_pre) echo '<pre>';
  101. var_dump($mixed);
  102. if($this->add_pre) echo '</pre>';
  103. }
  104. public function addScope($id = false)
  105. {
  106. if ($id == false) {
  107. $id = $this->scope_nextID++;
  108. }
  109. $this->current_scopes[$id] = true;
  110. }
  111. public function removeScope($id)
  112. {
  113. if (isset($this->current_scopes[$id])) unset($this->current_scopes[$id]);
  114. }
  115. public function resetScopes()
  116. {
  117. $this->current_scopes = array();
  118. $this->scope_nextID = 1;
  119. }
  120. public function isInScopes($scopes = array())
  121. {
  122. if (empty($this->current_scopes)) {
  123. return false;
  124. }
  125. if (!is_array($scopes)) {
  126. $scopes = array($scopes);
  127. }
  128. foreach ($scopes as $scope_id) {
  129. if (empty($this->current_scopes[$scope_id])) {
  130. return false;
  131. }
  132. }
  133. if (empty($scopes)) {
  134. if ($this->scope_nextID == 1) {
  135. return false;
  136. }
  137. for($i = 1; $i < $this->scope_nextID; $i++) {
  138. if (empty($this->current_scopes[$i])) {
  139. return false;
  140. }
  141. }
  142. }
  143. return true;
  144. }
  145. }
  146. // vim: et sw=4 sts=4