DBG.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. <?php
  2. class DBG {
  3. public static function isActive($mode = null) {
  4. return true == V::get('_DBG_ON', false, $_SESSION);
  5. }
  6. public static function activate($mode = null) {// @used in User::auth()
  7. $_SESSION['_DBG_ON'] = true;
  8. }
  9. public static function deactivate($mode = 'all') {
  10. $_SESSION['_DBG_ON'] = false;
  11. }
  12. /**
  13. * @param $reqKqy - key in $_REQUEST array (if true then always show)
  14. * @param $reqValueExpr - expression to compare req value
  15. * true - always visible - only for fast DBG without $reqKey in REQUEST
  16. * '>*', '>=*', '<*', '<=*' - compare $reqValue
  17. * examples:
  18. * - show when $_REQUEST['DBG_SCH'] == '1'
  19. * DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  20. * - show when $_REQUEST['DBG_SCH'] > '1'
  21. * DBG::_('DBG_SCH', '>1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  22. * - show when any value: strlen($_REQUEST['DBG_SCH']) > 0
  23. * DBG::_('DBG_SCH', true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  24. * - always show
  25. * DBG::_(true, true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
  26. */
  27. public static function _($reqKey, $reqValueExpr, $label, $variable, $className, $functionName, $lineNumber, $borderColor = 'red') {
  28. if (DBG::isActive()) return;
  29. $showDBG = false;
  30. $reqValue = (true === $reqKey || !$reqKey)? '' : V::get($reqKey, '', $_GET);
  31. if (true === $reqKey) {
  32. $showDBG = true;
  33. }
  34. else if (strlen($reqValue) == 0) {
  35. return;
  36. }
  37. else if ($reqValue == $reqValueExpr) {
  38. $showDBG = true;
  39. }
  40. else if (true === $reqValueExpr) {
  41. $showDBG = true;
  42. }
  43. else {
  44. if ('>=' == substr($reqValueExpr, 0, 2)) {
  45. if ($reqValue >= substr($reqValueExpr, 2)) {
  46. $showDBG = true;
  47. }
  48. }
  49. else if ('>' == substr($reqValueExpr, 0, 1)) {
  50. if ($reqValue > substr($reqValueExpr, 1)) {
  51. $showDBG = true;
  52. }
  53. }
  54. else if ('<=' == substr($reqValueExpr, 0, 2)) {
  55. if ($reqValue <= substr($reqValueExpr, 2)) {
  56. $showDBG = true;
  57. }
  58. }
  59. else if ('<' == substr($reqValueExpr, 0, 1)) {
  60. if ($reqValue < substr($reqValueExpr, 1)) {
  61. $showDBG = true;
  62. }
  63. }
  64. else {
  65. if ($reqValue = $reqValueExpr) {
  66. $showDBG = true;
  67. }
  68. }
  69. }
  70. if ($showDBG) {
  71. ?>
  72. <pre style="max-height:200px;max-width:800px;overflow:auto;border:1px solid <?php echo $borderColor; ?>;text-align:left;"
  73. ><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):\n"; ?>
  74. <?php print_r($variable);?>
  75. </pre>
  76. <?php
  77. }
  78. }
  79. public static function table($label, $table, $className, $functionName, $lineNumber) {
  80. $cols = array();
  81. if (is_array($table) && !empty($table)) {
  82. $firstRow = array();
  83. foreach ($table as $row) {
  84. $firstRow = $row;
  85. break;
  86. }
  87. if (is_array($firstRow)) {
  88. $cols = array_keys($firstRow);
  89. }
  90. else if (is_object($firstRow)) {
  91. $cols = array_keys((array)$firstRow);
  92. }
  93. else {
  94. return;// bad table item type
  95. }
  96. }
  97. ?>
  98. <table class="table table-bordered table-hover">
  99. <caption><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):"; ?></caption>
  100. <thead>
  101. <tr>
  102. <th style="padding:2px;">Lp.</th>
  103. <?php foreach ($cols as $colName) : ?>
  104. <th style="padding:2px;"><?php echo $colName; ?></th>
  105. <?php endforeach; ?>
  106. </tr>
  107. </thead>
  108. <tbody>
  109. <?php $i = 0; foreach ($table as $row) : $i++; ?>
  110. <tr>
  111. <td style="padding:2px;"><?php echo $i; ?></td>
  112. <?php foreach ($cols as $colName) : ?>
  113. <td style="padding:2px;"><?php echo V::get($colName, '', $row); ?></td>
  114. <?php endforeach; ?>
  115. </tr>
  116. <?php endforeach; ?>
  117. </tbody>
  118. </table>
  119. <?php
  120. }
  121. public static function nicePrint($variable, $varName) {
  122. $cnt = '';
  123. ob_start();
  124. print_r($variable);
  125. $cnt = ob_get_contents();
  126. ob_end_clean();
  127. $outLines = array();
  128. $lines = explode("\n", $cnt);
  129. if ($varName) $lines[0] = '<b style="color:#ff5252">' . $varName . "</b> => {$lines[0]}";
  130. foreach ($lines as $line) {
  131. if ('(' == trim($line)) continue;
  132. if (')' == trim($line)) continue;
  133. if ('' == trim($line)) continue;
  134. if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
  135. $line = str_replace('Array', '<span style="color:#41a541">Array</span>', $line);
  136. $line = str_replace('stdClass', '<span style="color:#41a541">stdClass</span>', $line);
  137. $line .= ':';
  138. }
  139. if (($firstBracket = strpos($line, '[')) > 0) {
  140. $line = str_replace("\t", ' ', $line);
  141. $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 4 + 2 : 2;
  142. $line = substr($line, $firstBracket - $splitPos);
  143. $line = preg_replace('/\[(\w+)\]/', '[<span style="color:#5a5aff">\1</span>]', $line);
  144. $line = preg_replace('/\] \=\> (\w+)$/', '] => <span style="color:#e88501">\1</span>', $line);
  145. }
  146. $outLines[] = $line;
  147. }
  148. $outLines = implode("\n", $outLines);
  149. echo $outLines;
  150. }
  151. }