DBG.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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, $rows, $className, $functionName, $lineNumber) {
  80. $params = array();
  81. $params['caption'] = "{$label} ({$className}::{$functionName}:{$lineNumber}):";
  82. $params['rows'] = $rows;
  83. UI::table($params);
  84. }
  85. public static function nicePrint($variable, $varName) {
  86. $cnt = '';
  87. ob_start();
  88. print_r($variable);
  89. $cnt = ob_get_contents();
  90. ob_end_clean();
  91. $outLines = array();
  92. $lines = explode("\n", $cnt);
  93. if ($varName) $lines[0] = '<b style="color:#ff5252">' . $varName . "</b> => {$lines[0]}";
  94. foreach ($lines as $line) {
  95. if ('(' == trim($line)) continue;
  96. if (')' == trim($line)) continue;
  97. if ('' == trim($line)) continue;
  98. if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
  99. $line = str_replace('Array', '<span style="color:#41a541">Array</span>', $line);
  100. $line = str_replace('stdClass', '<span style="color:#41a541">stdClass</span>', $line);
  101. $line .= ':';
  102. }
  103. if (($firstBracket = strpos($line, '[')) > 0) {
  104. $line = str_replace("\t", ' ', $line);
  105. $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 4 + 2 : 2;
  106. $line = substr($line, $firstBracket - $splitPos);
  107. $line = preg_replace('/\[(\w+)\]/', '[<span style="color:#5a5aff">\1</span>]', $line);
  108. $line = preg_replace('/\] \=\> (\w+)$/', '] => <span style="color:#e88501">\1</span>', $line);
  109. }
  110. $outLines[] = $line;
  111. }
  112. $outLines = implode("\n", $outLines);
  113. echo $outLines;
  114. }
  115. }