DBG.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. $col = ['green'=>'#96c178', 'red'=>'#de6b74', 'blue'=>'#55b5c1', 'bg-dark'=>'#282c34', 'white'=>'#abb2bf', 'orange'=>'#d19a66', 'violet'=>'#c476db'];
  87. $cnt = '';
  88. ob_start();
  89. print_r($variable);
  90. $cnt = ob_get_contents();
  91. ob_end_clean();
  92. $outLines = array();
  93. $lines = explode("\n", $cnt);
  94. if ($varName) $lines[0] = '<b style="color:'.$col['red'].'">' . $varName . "</b> => {$lines[0]}";
  95. foreach ($lines as $line) {
  96. if ('(' == trim($line)) continue;
  97. if (')' == trim($line)) continue;
  98. if ('' == trim($line)) continue;
  99. if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
  100. $line = str_replace('Array', '<span style="color:'.$col['blue'].'">Array</span>', $line);
  101. $line = str_replace('stdClass', '<span style="color:'.$col['blue'].'">stdClass</span>', $line);
  102. $line .= ':';
  103. }
  104. if (($firstBracket = strpos($line, '[')) > 0) {
  105. $line = str_replace("\t", ' ', $line);
  106. $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 2 + 4 : 4;
  107. $line = substr($line, $firstBracket - $splitPos);
  108. $line = preg_replace('/\[(\w+)\]/', '[<span style="color:'.$col['green'].'">\1</span>]', $line);
  109. $line = preg_replace('/\] \=\> (.+)$/', '] => <span style="color:'.$col['orange'].'">\1</span>', $line);
  110. }
  111. $outLines[] = $line;
  112. }
  113. $outLines = implode("\n", $outLines);
  114. echo '<pre style="background-color:'.$col['bg-dark'].'; color:'.$col['white'].'; font-size:x-small">' . $outLines . '</pre>';
  115. // echo '<pre>'; print_r($variable);echo'</pre>';
  116. }
  117. }