/** * @param $traceData * * @return string */ public static function decorateTrace($traceData) { foreach ($traceData as &$frame) { if (isset($frame['args'])) { KintParser::reset(); $frame['args'] = self::_unparse(KintParser::factory($frame['args'])); } if (isset($frame['object'])) { KintParser::reset(); $frame['object'] = self::_unparse(KintParser::factory($frame['object'])); } } return 'kintDump.push(' . json_encode($traceData) . ');' . 'console.log(kintDump[kintDump.length-1]);'; }
/** * @param array $traceData * * @return string * @throws \Exception */ public static function decorateTrace(array $traceData = array()) { $output = '<dl class="kint-trace">'; foreach ($traceData as $i => $step) { $class = ' kint-parent '; if (Kint::$expandedByDefault) { $class .= ' kint-show '; } $output .= ' <dt class="' . $class . '"> <b>' . ($i + 1) . '</b> <nav></nav> <var> '; if (isset($step['file'])) { $output .= self::_ideLink($step['file'], $step['line']); } else { $output .= 'PHP internal call'; } $output .= '</var>'; $output .= $step['function']; if (isset($step['args'])) { $output .= '(' . implode(', ', array_keys($step['args'])) . ')'; } $output .= '</dt><dd>'; $firstTab = ' class="kint-active-tab" '; $output .= '<ul class="kint-tabs">'; if (!empty($step['source'])) { $output .= "<li{$firstTab}>Source</li>"; $firstTab = ''; } if (!empty($step['args'])) { $output .= "<li{$firstTab}>Arguments</li>"; $firstTab = ''; } if (!empty($step['object'])) { KintParser::reset(); $calleeDump = KintParser::factory($step['object']); $output .= "<li{$firstTab}>Callee object [{$calleeDump->type}]</li>"; } $output .= '</ul><ul>'; if (!empty($step['source'])) { $output .= "<li><pre class=\"kint-source\">{$step['source']}</pre></li>"; } if (!empty($step['args'])) { $output .= '<li>'; $j = 0; foreach ($step['args'] as $k => $arg) { KintParser::reset(); if (isset($step['index']) && $step['index'] !== null) { $output .= self::decorate(KintParser::factory($arg, $k), 0, 'debug_backtrace()[' . $step['index'] . "]['args']", 'array', $j); } else { $output .= self::decorate(KintParser::factory($arg, $k)); } $j++; } $output .= '</li>'; } if (isset($calleeDump) && !empty($step['object'])) { $output .= '<li>'; if (isset($step['index']) && $step['index'] !== null) { $output .= self::decorate($calleeDump, 'debug_backtrace(true)[' . $step['index'] . ']', 'array', 'object'); } else { $output .= self::decorate($calleeDump); } $output .= '</li>'; } $output .= '</ul></dd>'; } $output .= '</dl>'; return $output; }
/** * Dump information about variables, accepts any number of parameters, supports modifiers: * * clean up any output before kint and place the dump at the top of page: * - Kint::dump() * ***** * expand all nodes on display: * ! Kint::dump() * ***** * dump variables disregarding their depth: * + Kint::dump() * ***** * return output instead of displaying it: * @ Kint::dump() * ***** * force output as plain text * ~ Kint::dump() * * Modifiers are supported by all dump wrapper functions, including Kint::trace(). Space is optional. * * * You can also use the following shorthand to display debug_backtrace(): * Kint::dump( 1 ); * * Passing the result from debug_backtrace() to kint::dump() as a single parameter will display it as trace too: * $trace = debug_backtrace( true ); * Kint::dump( $trace ); * Or simply: * Kint::dump( debug_backtrace() ); * * * @param mixed $data * * @return void|string */ public static function dump($data = null) { if (!self::enabled()) { return ''; } $stash = self::settings(); list($names, $modifiers, $callee, $previousCaller, $miniTrace) = self::_getCalleeInfo(defined('DEBUG_BACKTRACE_IGNORE_ARGS') ? debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS) : debug_backtrace()); # set mode for current run $mode = self::enabled(); if ($mode === true) { $mode = PHP_SAPI === 'cli' && self::$cliDetection === true ? self::MODE_CLI : self::MODE_RICH; } self::enabled($mode); if (strpos($modifiers, '~') !== false) { self::enabled(self::MODE_WHITESPACE); } switch (self::enabled()) { case self::MODE_RICH: $decorator = 'kint\\decorators\\Kint_Decorators_Rich'; break; case self::MODE_JS: $decorator = 'kint\\decorators\\Kint_Decorators_JS'; break; default: case self::MODE_PLAIN: $decorator = 'kint\\decorators\\Kint_Decorators_Plain'; break; } /* @var Kint_Decorators $decorator */ $firstRunOldValue = $decorator::$firstRun; # process modifiers: @, +, ! and - if (strpos($modifiers, '-') !== false) { $decorator::$firstRun = true; while (ob_get_level()) { ob_end_clean(); } } if (strpos($modifiers, '!') !== false) { self::$expandedByDefault = true; } if (strpos($modifiers, '+') !== false) { self::$maxLevels = false; } if (strpos($modifiers, '@') !== false) { self::$returnOutput = true; $decorator::$firstRun = true; } $output = ''; if ($decorator::$firstRun) { $output .= call_user_func(array($decorator, 'init')); } $trace = false; $tmpFuncNumArgs = func_num_args(); if ($data === 1 && $tmpFuncNumArgs === 1 && $names === array(null)) { # Kint::dump(1) shorthand $trace = debug_backtrace(true); } elseif ($tmpFuncNumArgs === 1 && is_array($data)) { $trace = $data; # test if the single parameter is result of debug_backtrace() } $trace and $trace = self::_parseTrace($trace); $output .= call_user_func(array($decorator, 'wrapStart')); if ($trace) { $output .= call_user_func(array($decorator, 'decorateTrace'), $trace); } else { $data = $tmpFuncNumArgs === 0 ? array('[[no arguments passed]]') : func_get_args(); foreach ($data as $k => $argument) { KintParser::reset(); # when the dump arguments take long to generate output, user might have changed the file and # Kint might not parse the arguments correctly, so check if names are set and while the # displayed names might be wrong, at least don't throw an error $output .= call_user_func(array($decorator, 'decorate'), KintParser::factory($argument, isset($names[$k]) ? $names[$k] : '')); } } $output .= call_user_func(array($decorator, 'wrapEnd'), $callee, $miniTrace, $previousCaller); $decorator::$firstRun = false; if (strpos($modifiers, '@') !== false) { $decorator::$firstRun = $firstRunOldValue; } if (self::$returnOutput) { self::settings($stash); return $output; } if (self::$delayedMode) { self::settings($stash); register_shutdown_function('printf', '%s', $output); return ''; } self::settings($stash); echo $output; return ''; }