DBG.php 4.5 KB

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