| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161 |
- <?php
- class DBG {
- public static function isActive($mode = null) {
- return true == V::get('_DBG_ON', false, $_SESSION);
- }
- public static function activate($mode = null) {// @used in User::auth()
- $_SESSION['_DBG_ON'] = true;
- }
- public static function deactivate($mode = 'all') {
- $_SESSION['_DBG_ON'] = false;
- }
- /**
- * @param $reqKqy - key in $_REQUEST array (if true then always show)
- * @param $reqValueExpr - expression to compare req value
- * true - always visible - only for fast DBG without $reqKey in REQUEST
- * '>*', '>=*', '<*', '<=*' - compare $reqValue
- * examples:
- * - show when $_REQUEST['DBG_SCH'] == '1'
- * DBG::_('DBG_SCH', '1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- * - show when $_REQUEST['DBG_SCH'] > '1'
- * DBG::_('DBG_SCH', '>1', "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- * - show when any value: strlen($_REQUEST['DBG_SCH']) > 0
- * DBG::_('DBG_SCH', true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- * - always show
- * DBG::_(true, true, "fieldsConfig({$idTable})", $fieldsConfig, __CLASS__, __FUNCTION__, __LINE__);
- */
- public static function _($reqKey, $reqValueExpr, $label, $variable, $className, $functionName, $lineNumber, $borderColor = 'red') {
- if (DBG::isActive()) return;
- $showDBG = false;
- $reqValue = (true === $reqKey || !$reqKey)? '' : V::get($reqKey, '', $_GET);
- if (true === $reqKey) {
- $showDBG = true;
- }
- else if (strlen($reqValue) == 0) {
- return;
- }
- else if ($reqValue == $reqValueExpr) {
- $showDBG = true;
- }
- else if (true === $reqValueExpr) {
- $showDBG = true;
- }
- else {
- if ('>=' == substr($reqValueExpr, 0, 2)) {
- if ($reqValue >= substr($reqValueExpr, 2)) {
- $showDBG = true;
- }
- }
- else if ('>' == substr($reqValueExpr, 0, 1)) {
- if ($reqValue > substr($reqValueExpr, 1)) {
- $showDBG = true;
- }
- }
- else if ('<=' == substr($reqValueExpr, 0, 2)) {
- if ($reqValue <= substr($reqValueExpr, 2)) {
- $showDBG = true;
- }
- }
- else if ('<' == substr($reqValueExpr, 0, 1)) {
- if ($reqValue < substr($reqValueExpr, 1)) {
- $showDBG = true;
- }
- }
- else {
- if ($reqValue = $reqValueExpr) {
- $showDBG = true;
- }
- }
- }
- if ($showDBG) {
- ?>
- <pre style="max-height:200px;max-width:800px;overflow:auto;border:1px solid <?php echo $borderColor; ?>;text-align:left;"
- ><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):\n"; ?>
- <?php print_r($variable);?>
- </pre>
- <?php
- }
- }
- public static function table($label, $table, $className, $functionName, $lineNumber) {
- $cols = array();
- if (is_array($table) && !empty($table)) {
- $firstRow = array();
- foreach ($table as $row) {
- $firstRow = $row;
- break;
- }
- if (is_array($firstRow)) {
- $cols = array_keys($firstRow);
- }
- else if (is_object($firstRow)) {
- $cols = array_keys((array)$firstRow);
- }
- else {
- return;// bad table item type
- }
- }
- ?>
- <table class="table table-bordered table-hover">
- <caption><?php echo "{$label} ({$className}::{$functionName}:{$lineNumber}):"; ?></caption>
- <thead>
- <tr>
- <th style="padding:2px;">Lp.</th>
- <?php foreach ($cols as $colName) : ?>
- <th style="padding:2px;"><?php echo $colName; ?></th>
- <?php endforeach; ?>
- </tr>
- </thead>
- <tbody>
- <?php $i = 0; foreach ($table as $row) : $i++; ?>
- <tr>
- <td style="padding:2px;"><?php echo $i; ?></td>
- <?php foreach ($cols as $colName) : ?>
- <td style="padding:2px;"><?php echo V::get($colName, '', $row); ?></td>
- <?php endforeach; ?>
- </tr>
- <?php endforeach; ?>
- </tbody>
- </table>
- <?php
- }
- public static function nicePrint($variable, $varName) {
- $cnt = '';
- ob_start();
- print_r($variable);
- $cnt = ob_get_contents();
- ob_end_clean();
- $outLines = array();
- $lines = explode("\n", $cnt);
- if ($varName) $lines[0] = '<b style="color:#ff5252">' . $varName . "</b> => {$lines[0]}";
- foreach ($lines as $line) {
- if ('(' == trim($line)) continue;
- if (')' == trim($line)) continue;
- if ('' == trim($line)) continue;
- if ('Array' == substr($line, -5) || 'stdClass' == substr($line, -5)) {
- $line = str_replace('Array', '<span style="color:#41a541">Array</span>', $line);
- $line = str_replace('stdClass', '<span style="color:#41a541">stdClass</span>', $line);
- $line .= ':';
- }
- if (($firstBracket = strpos($line, '[')) > 0) {
- $line = str_replace("\t", ' ', $line);
- $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 4 + 2 : 2;
- $line = substr($line, $firstBracket - $splitPos);
- $line = preg_replace('/\[(\w+)\]/', '[<span style="color:#5a5aff">\1</span>]', $line);
- $line = preg_replace('/\] \=\> (\w+)$/', '] => <span style="color:#e88501">\1</span>', $line);
- }
- $outLines[] = $line;
- }
- $outLines = implode("\n", $outLines);
- echo $outLines;
- }
- }
|