= 0 ); require KINT_DIR . 'config.default.php'; require KINT_DIR . 'inc/kintVariableData.class.php'; require KINT_DIR . 'inc/kintParser.class.php'; require KINT_DIR . 'inc/kintObject.class.php'; require KINT_DIR . 'decorators/rich.php'; require KINT_DIR . 'decorators/plain.php'; if ( is_readable( KINT_DIR . 'config.php' ) ) { require KINT_DIR . 'config.php'; } # init settings if ( !empty( $GLOBALS['_kint_settings'] ) ) { Kint::enabled( $GLOBALS['_kint_settings']['enabled'] ); foreach ( $GLOBALS['_kint_settings'] as $key => $val ) { property_exists( 'Kint', $key ) and Kint::$$key = $val; } unset( $GLOBALS['_kint_settings'], $key, $val ); } class Kint { // these are all public and 1:1 config array keys so you can switch them easily private static $_enabledMode; # stores mode and active statuses public static $returnOutput; public static $fileLinkFormat; public static $displayCalledFrom; public static $charEncodings; public static $maxStrLength; public static $appRootDirs; public static $maxLevels; public static $theme; public static $expandedByDefault; public static $cliDetection; public static $cliColors; const MODE_RICH = 'r'; const MODE_WHITESPACE = 'w'; const MODE_CLI = 'c'; const MODE_PLAIN = 'p'; public static $aliases = array( 'methods' => array( array( 'kint', 'dump' ), array( 'kint', 'trace' ), ), 'functions' => array( 'd', 'dd', 'ddd', 's', 'sd', ) ); private static $_firstRun = true; /** * Enables or disables Kint, can globally enforce the rendering mode. If called without parameters, returns the * current mode. * * @param mixed $forceMode * null or void - return current mode * false - disable (no output) * true - enable and detect cli automatically * Kint::MODE_* - enable and force selected mode disregarding detection and function * shorthand (s()/d()), note that you can still override this * with the "~" modifier * * @return mixed previously set value if a new one is passed */ public static function enabled( $forceMode = null ) { # act both as a setter... if ( isset( $forceMode ) ) { $before = self::$_enabledMode; self::$_enabledMode = $forceMode; return $before; } # ...and a getter return self::$_enabledMode; } /** * Prints a debug backtrace, same as Kint::dump(1) * * @param array $trace [OPTIONAL] you can pass your own trace, otherwise, `debug_backtrace` will be called * * @return mixed */ public static function trace( $trace = null ) { if ( !self::enabled() ) return ''; return self::dump( isset( $trace ) ? $trace : debug_backtrace( true ) ); } /** * 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 ''; list( $names, $modifiers, $callee, $previousCaller, $miniTrace ) = self::_getCalleeInfo( defined( 'DEBUG_BACKTRACE_IGNORE_ARGS' ) ? debug_backtrace( DEBUG_BACKTRACE_IGNORE_ARGS ) : debug_backtrace() ); $modeOldValue = self::enabled(); $firstRunOldValue = self::$_firstRun; # process modifiers: @, +, !, ~ and - if ( strpos( $modifiers, '-' ) !== false ) { self::$_firstRun = true; while ( ob_get_level() ) { ob_end_clean(); } } if ( strpos( $modifiers, '!' ) !== false ) { $expandedByDefaultOldValue = self::$expandedByDefault; self::$expandedByDefault = true; } if ( strpos( $modifiers, '+' ) !== false ) { $maxLevelsOldValue = self::$maxLevels; self::$maxLevels = false; } if ( strpos( $modifiers, '@' ) !== false ) { $returnOldValue = self::$returnOutput; self::$returnOutput = true; self::$_firstRun = true; } if ( strpos( $modifiers, '~' ) !== false ) { self::enabled( self::MODE_WHITESPACE ); } # set mode for current run $mode = self::enabled(); if ( $mode === true ) { $mode = PHP_SAPI === 'cli' ? self::MODE_CLI : self::MODE_RICH; } self::enabled( $mode ); $decorator = self::enabled() === self::MODE_RICH ? 'Kint_Decorators_Rich' : 'Kint_Decorators_Plain'; $output = ''; if ( self::$_firstRun ) { $output .= call_user_func( array( $decorator, 'init' ) ); } $trace = false; if ( $names === array( null ) && func_num_args() === 1 && $data === 1 ) { # Kint::dump(1) shorthand $trace = KINT_PHP53 ? debug_backtrace( true ) : debug_backtrace(); } elseif ( func_num_args() === 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 = func_num_args() === 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 ); self::enabled( $modeOldValue ); self::$_firstRun = false; if ( strpos( $modifiers, '~' ) !== false ) { self::$_firstRun = $firstRunOldValue; } else { self::enabled( $modeOldValue ); } if ( strpos( $modifiers, '!' ) !== false ) { self::$expandedByDefault = $expandedByDefaultOldValue; } if ( strpos( $modifiers, '+' ) !== false ) { self::$maxLevels = $maxLevelsOldValue; } if ( strpos( $modifiers, '@' ) !== false ) { self::$returnOutput = $returnOldValue; self::$_firstRun = $firstRunOldValue; return $output; } if ( self::$returnOutput ) return $output; echo $output; return ''; } /** * generic path display callback, can be configured in the settings; purpose is to show relevant path info and hide * as much of the path as possible. * * @param string $file * * @return string */ public static function shortenPath( $file ) { $file = str_replace( '\\', '/', $file ); $shortenedName = $file; $replaced = false; if ( is_array( self::$appRootDirs ) ) foreach ( self::$appRootDirs as $path => $replaceString ) { if ( empty( $path ) ) continue; $path = str_replace( '\\', '/', $path ); if ( strpos( $file, $path ) === 0 ) { $shortenedName = $replaceString . substr( $file, strlen( $path ) ); $replaced = true; break; } } # fallback to find common path with Kint dir if ( !$replaced ) { $pathParts = explode( '/', str_replace( '\\', '/', KINT_DIR ) ); $fileParts = explode( '/', $file ); $i = 0; foreach ( $fileParts as $i => $filePart ) { if ( !isset( $pathParts[ $i ] ) || $pathParts[ $i ] !== $filePart ) break; } $shortenedName = ( $i ? '.../' : '' ) . implode( '/', array_slice( $fileParts, $i ) ); } return $shortenedName; } public static function getIdeLink( $file, $line ) { return str_replace( array( '%f', '%l' ), array( $file, $line ), self::$fileLinkFormat ); } /** * trace helper, shows the place in code inline * * @param string $file full path to file * @param int $lineNumber the line to display * @param int $padding surrounding lines to show besides the main one * * @return bool|string */ private static function _showSource( $file, $lineNumber, $padding = 7 ) { if ( !$file OR !is_readable( $file ) ) { # continuing will cause errors return false; } # open the file and set the line position $file = fopen( $file, 'r' ); $line = 0; # Set the reading range $range = array( 'start' => $lineNumber - $padding, 'end' => $lineNumber + $padding ); # set the zero-padding amount for line numbers $format = '% ' . strlen( $range['end'] ) . 'd'; $source = ''; while ( ( $row = fgets( $file ) ) !== false ) { # increment the line number if ( ++$line > $range['end'] ) { break; } if ( $line >= $range['start'] ) { # make the row safe for output $row = htmlspecialchars( $row, ENT_NOQUOTES, 'UTF-8' ); # trim whitespace and sanitize the row $row = '' . sprintf( $format, $line ) . ' ' . $row; if ( $line === $lineNumber ) { # apply highlighting to this row $row = '
Kint: dd() is being deprecated, please use ddd() instead\n"; $_ = func_get_args(); call_user_func_array( array( 'Kint', 'dump' ), $_ ); die; } } if ( !function_exists( 'ddd' ) ) { /** * Alias of Kint::dump() * [!!!] IMPORTANT: execution will halt after call to this function * * @return string */ function ddd() { if ( !Kint::enabled() ) return ''; $_ = func_get_args(); call_user_func_array( array( 'Kint', 'dump' ), $_ ); die; } } if ( !function_exists( 's' ) ) { /** * Alias of Kint::dump(), however the output is in plain htmlescaped text and some minor visibility enhancements * added. If run in CLI mode, output is pure whitespace. * * To force rendering mode without autodetecting anything: * * Kint::enabled( Kint::MODE_PLAIN ); * Kint::dump( $variable ); * * [!!!] IMPORTANT: execution will halt after call to this function * * @return string */ function s() { $enabled = Kint::enabled(); if ( !$enabled ) return ''; if ( $enabled === Kint::MODE_WHITESPACE ) { # if already in whitespace, don't elevate to plain $restoreMode = Kint::MODE_WHITESPACE; } else { $restoreMode = Kint::enabled( # remove cli colors in cli mode; remove rich interface in HTML mode PHP_SAPI === 'cli' ? Kint::MODE_WHITESPACE : Kint::MODE_PLAIN ); } $params = func_get_args(); $dump = call_user_func_array( array( 'Kint', 'dump' ), $params ); Kint::enabled( $restoreMode ); return $dump; } } if ( !function_exists( 'sd' ) ) { /** * @see s() * * [!!!] IMPORTANT: execution will halt after call to this function * * @return string */ function sd() { $enabled = Kint::enabled(); if ( !$enabled ) return ''; if ( $enabled !== Kint::MODE_WHITESPACE ) { Kint::enabled( PHP_SAPI === 'cli' ? Kint::MODE_WHITESPACE : Kint::MODE_PLAIN ); } $params = func_get_args(); call_user_func_array( array( 'Kint', 'dump' ), $params ); die; } }