| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?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, $rows, $className, $functionName, $lineNumber) {
- $params = array();
- $params['caption'] = "{$label} ({$className}::{$functionName}:{$lineNumber}):";
- $params['rows'] = $rows;
- UI::table($params);
- }
- public static function nicePrint($variable, $varName) {
- $col = ['green'=>'#96c178', 'red'=>'#de6b74', 'blue'=>'#55b5c1', 'bg-dark'=>'#282c34', 'white'=>'#abb2bf', 'orange'=>'#d19a66', 'violet'=>'#c476db'];
- $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:'.$col['red'].'">' . $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:'.$col['blue'].'">Array</span>', $line);
- $line = str_replace('stdClass', '<span style="color:'.$col['blue'].'">stdClass</span>', $line);
- $line .= ':';
- }
- if (($firstBracket = strpos($line, '[')) > 0) {
- $line = str_replace("\t", ' ', $line);
- $splitPos = ($firstBracket > 4)? ($firstBracket - 4) / 2 + 4 : 4;
- $line = substr($line, $firstBracket - $splitPos);
- $line = preg_replace('/\[(\w+)\]/', '[<span style="color:'.$col['green'].'">\1</span>]', $line);
- $line = preg_replace('/\] \=\> (.+)$/', '] => <span style="color:'.$col['orange'].'">\1</span>', $line);
- }
- $outLines[] = $line;
- }
- $outLines = implode("\n", $outLines);
- echo '<pre style="background-color:'.$col['bg-dark'].'; color:'.$col['white'].'; font-size:x-small">' . $outLines . '</pre>';
- // echo '<pre>'; print_r($variable);echo'</pre>';
- }
- }
|