DBG.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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">
  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. }