| 1 |
<?php |
| 2 |
class Kint_Decorators_Rich extends Kint |
| 3 |
{ |
| 4 |
// make calls to Kint::dump() from different places in source coloured differently. |
| 5 |
private static $_usedColors = array(); |
| 6 |
|
| 7 |
public static function decorate( kintVariableData $kintVar ) |
| 8 |
{ |
| 9 |
$output = '<dl>'; |
| 10 |
|
| 11 |
$extendedPresent = $kintVar->extendedValue !== null || $kintVar->alternatives !== null; |
| 12 |
|
| 13 |
if ( $extendedPresent ) { |
| 14 |
$class = 'kint-parent'; |
| 15 |
if ( Kint::$expandedByDefault ) { |
| 16 |
$class .= ' kint-show'; |
| 17 |
} |
| 18 |
$output .= '<dt class="' . $class . '"><nav></nav>'; |
| 19 |
} else { |
| 20 |
$output .= '<dt>'; |
| 21 |
} |
| 22 |
|
| 23 |
$output .= self::_drawHeader( $kintVar ) . $kintVar->value . '</dt>'; |
| 24 |
|
| 25 |
|
| 26 |
if ( $extendedPresent ) { |
| 27 |
$output .= '<dd>'; |
| 28 |
} |
| 29 |
|
| 30 |
if ( isset( $kintVar->extendedValue ) ) { |
| 31 |
|
| 32 |
if ( is_array( $kintVar->extendedValue ) ) { |
| 33 |
foreach ( $kintVar->extendedValue as $v ) { |
| 34 |
$output .= self::decorate( $v ); |
| 35 |
} |
| 36 |
} elseif ( is_string( $kintVar->extendedValue ) ) { |
| 37 |
$output .= '<pre>' . $kintVar->extendedValue . '</pre>'; |
| 38 |
} else { |
| 39 |
$output .= self::decorate( $kintVar->extendedValue ); //it's kint's container |
| 40 |
} |
| 41 |
|
| 42 |
} elseif ( isset( $kintVar->alternatives ) ) { |
| 43 |
$output .= "<ul class=\"kint-tabs\">"; |
| 44 |
|
| 45 |
foreach ( $kintVar->alternatives as $k => $var ) { |
| 46 |
$active = $k === 0 ? ' class="kint-active-tab"' : ''; |
| 47 |
$output .= "<li{$active}>" . self::_drawHeader( $var, false ) . '</li>'; |
| 48 |
} |
| 49 |
|
| 50 |
$output .= "</ul><ul>"; |
| 51 |
|
| 52 |
foreach ( $kintVar->alternatives as $var ) { |
| 53 |
$output .= "<li>"; |
| 54 |
|
| 55 |
$var = $var->value; |
| 56 |
|
| 57 |
if ( is_array( $var ) ) { |
| 58 |
foreach ( $var as $v ) { |
| 59 |
$output .= self::decorate( $v ); |
| 60 |
} |
| 61 |
} elseif ( is_string( $var ) ) { |
| 62 |
$output .= '<pre>' . $var . '</pre>'; |
| 63 |
} elseif ( isset( $var ) ) { |
| 64 |
throw new Exception( |
| 65 |
'Kint has encountered an error, ' |
| 66 |
. 'please paste this report to https://github.com/raveren/kint/issues<br>' |
| 67 |
. 'Error encountered at ' . basename( __FILE__ ) . ':' . __LINE__ . '<br>' |
| 68 |
. ' variables: ' |
| 69 |
. htmlspecialchars( var_export( $kintVar->alternatives, true ), ENT_QUOTES ) |
| 70 |
); |
| 71 |
} |
| 72 |
|
| 73 |
$output .= "</li>"; |
| 74 |
} |
| 75 |
|
| 76 |
$output .= "</ul>"; |
| 77 |
} |
| 78 |
if ( $extendedPresent ) { |
| 79 |
$output .= '</dd>'; |
| 80 |
} |
| 81 |
|
| 82 |
$output .= '</dl>'; |
| 83 |
|
| 84 |
return $output; |
| 85 |
} |
| 86 |
|
| 87 |
private static function _drawHeader( kintVariableData $kintVar, $verbose = true ) |
| 88 |
{ |
| 89 |
$output = ''; |
| 90 |
if ( $verbose ) { |
| 91 |
if ( $kintVar->access !== null ) { |
| 92 |
$output .= "<var>" . $kintVar->access . "</var> "; |
| 93 |
} |
| 94 |
|
| 95 |
if ( $kintVar->name !== null && $kintVar->name !== '' ) { |
| 96 |
$output .= "<dfn>" . $kintVar->name . "</dfn> "; |
| 97 |
} |
| 98 |
|
| 99 |
if ( $kintVar->operator !== null ) { |
| 100 |
$output .= $kintVar->operator . " "; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
if ( $kintVar->type !== null ) { |
| 105 |
$output .= "<var>" . $kintVar->type; |
| 106 |
if ( $kintVar->subtype !== null ) { |
| 107 |
$output .= " " . $kintVar->subtype; |
| 108 |
} |
| 109 |
$output .= "</var> "; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
if ( $kintVar->size !== null ) { |
| 114 |
$output .= "(" . $kintVar->size . ") "; |
| 115 |
} |
| 116 |
|
| 117 |
return $output; |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
/** |
| 122 |
* produces css and js required for display. May be called multiple times, will only produce output once per |
| 123 |
* pageload or until `-` or `@` modifier is used |
| 124 |
* |
| 125 |
* @return string |
| 126 |
*/ |
| 127 |
protected static function _css() |
| 128 |
{ |
| 129 |
if ( !self::$_firstRun ) return ''; |
| 130 |
self::$_firstRun = false; |
| 131 |
|
| 132 |
$baseDir = KINT_DIR . 'view/inc/'; |
| 133 |
|
| 134 |
if ( !is_readable( $cssFile = $baseDir . self::$theme . '.css' ) ) { |
| 135 |
$cssFile = $baseDir . 'original.css'; |
| 136 |
} |
| 137 |
|
| 138 |
return '<script>' . file_get_contents( $baseDir . 'kint.js' ) . '</script>' |
| 139 |
. '<style>' . file_get_contents( $cssFile ) . "</style>\n"; |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
/** |
| 144 |
* called for each dump, opens the html tag |
| 145 |
* |
| 146 |
* @param array $callee caller information taken from debug backtrace |
| 147 |
* |
| 148 |
* @return string |
| 149 |
*/ |
| 150 |
protected static function _wrapStart( $callee ) |
| 151 |
{ |
| 152 |
// colors looping outputs the same (i.e. if same line in code dumps variables multiple time, |
| 153 |
// we assume it's in a loop) |
| 154 |
|
| 155 |
$uid = isset( $callee['file'] ) ? crc32( $callee['file'] . $callee['line'] ) : 'no-file'; |
| 156 |
|
| 157 |
if ( isset( self::$_usedColors[$uid] ) ) { |
| 158 |
$class = self::$_usedColors[$uid]; |
| 159 |
} else { |
| 160 |
$class = sizeof( self::$_usedColors ); |
| 161 |
self::$_usedColors[$uid] = $class; |
| 162 |
} |
| 163 |
|
| 164 |
$class = "kint_{$class}"; |
| 165 |
|
| 166 |
|
| 167 |
return "<div class=\"kint {$class}\">"; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/** |
| 172 |
* closes Kint::_wrapStart() started html tags and displays callee information |
| 173 |
* |
| 174 |
* @param array $callee caller information taken from debug backtrace |
| 175 |
* @param array $prevCaller previous caller information taken from debug backtrace |
| 176 |
* |
| 177 |
* @return string |
| 178 |
*/ |
| 179 |
protected static function _wrapEnd( $callee, $prevCaller ) |
| 180 |
{ |
| 181 |
if ( !Kint::$displayCalledFrom ) { |
| 182 |
return '</div>'; |
| 183 |
} |
| 184 |
|
| 185 |
$callingFunction = ''; |
| 186 |
if ( isset( $prevCaller['class'] ) ) { |
| 187 |
$callingFunction = $prevCaller['class']; |
| 188 |
} |
| 189 |
if ( isset( $prevCaller['type'] ) ) { |
| 190 |
$callingFunction .= $prevCaller['type']; |
| 191 |
} |
| 192 |
if ( isset( $prevCaller['function'] ) && !in_array( $prevCaller['function'], Kint::$_statements ) ) { |
| 193 |
$callingFunction .= $prevCaller['function'] . '()'; |
| 194 |
} |
| 195 |
$callingFunction and $callingFunction = " in ({$callingFunction})"; |
| 196 |
|
| 197 |
|
| 198 |
$calleeInfo = isset( $callee['file'] ) |
| 199 |
? 'Called from ' . self::shortenPath( $callee['file'], $callee['line'] ) |
| 200 |
: ''; |
| 201 |
|
| 202 |
|
| 203 |
return $calleeInfo || $callingFunction |
| 204 |
? "<footer>{$calleeInfo}{$callingFunction}</footer></div>" |
| 205 |
: "</div>"; |
| 206 |
} |
| 207 |
|
| 208 |
} |