From 511c7a100dcc506890dbcabf451275e1999eada6 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Wed, 23 Apr 2014 10:13:42 +0200 Subject: [PATCH] Remove Kint dependency Fixes #121 --- composer.json | 3 +- src/DebugBar/DataFormatter/DataFormatter.php | 136 ++++++++++++++++++- 2 files changed, 136 insertions(+), 3 deletions(-) diff --git a/composer.json b/composer.json index 760f4db..212b83c 100644 --- a/composer.json +++ b/composer.json @@ -12,8 +12,7 @@ }], "require": { "php": ">=5.3.0", - "psr/log": "~1.0", - "raveren/kint": "0.9" + "psr/log": "~1.0" }, "require-dev": { "php": ">=5.3.0" diff --git a/src/DebugBar/DataFormatter/DataFormatter.php b/src/DebugBar/DataFormatter/DataFormatter.php index 1d98b9f..4de4649 100644 --- a/src/DebugBar/DataFormatter/DataFormatter.php +++ b/src/DebugBar/DataFormatter/DataFormatter.php @@ -14,7 +14,7 @@ class DataFormatter implements DataFormatterInterface { public function formatVar($data) { - return \kintLite($data); + return $this->kintLite($data); } public function formatDuration($seconds) @@ -36,4 +36,138 @@ class DataFormatter implements DataFormatterInterface $suffixes = array('B', 'KB', 'MB', 'GB', 'TB'); return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)]; } + + /** + * lightweight version of Kint::dump(). Uses whitespace for formatting instead of html + * sadly not DRY yet + * + * Extracted from Kint.class.php in raveren/kint, https://github.com/raveren/kint + * Copyright (c) 2013 Rokas Šleinius (raveren@gmail.com) + * + * @param mixed $var + * @param int $level + * + * @return string + */ + protected function kintLite(&$var, $level = 0) + { + // initialize function names into variables for prettier string output (html and implode are also DRY) + $html = "htmlspecialchars"; + $implode = "implode"; + $strlen = "strlen"; + $count = "count"; + $getClass = "get_class"; + + + if ( $var === null ) { + return 'NULL'; + } elseif ( is_bool( $var ) ) { + return 'bool ' . ( $var ? 'TRUE' : 'FALSE' ); + } elseif ( is_float( $var ) ) { + return 'float ' . $var; + } elseif ( is_int( $var ) ) { + return 'integer ' . $var; + } elseif ( is_resource( $var ) ) { + if ( ( $type = get_resource_type( $var ) ) === 'stream' AND $meta = stream_get_meta_data( $var ) ) { + + if ( isset( $meta['uri'] ) ) { + $file = $meta['uri']; + + return "resource ({$type}) {$html( $file, 0 )}"; + } else { + return "resource ({$type})"; + } + } else { + return "resource ({$type})"; + } + } elseif ( is_string( $var ) ) { + return "string ({$strlen( $var )}) \"{$html( $var )}\""; + } elseif ( is_array( $var ) ) { + $output = array(); + $space = str_repeat( $s = ' ', $level ); + + static $marker; + + if ( $marker === null ) { + // Make a unique marker + $marker = uniqid( "\x00" ); + } + + if ( empty( $var ) ) { + return "array()"; + } elseif ( isset( $var[$marker] ) ) { + $output[] = "[\n$space$s*RECURSION*\n$space]"; + } elseif ( $level < 7 ) { + $isSeq = array_keys( $var ) === range( 0, count( $var ) - 1 ); + + $output[] = "["; + + $var[$marker] = true; + + + foreach ( $var as $key => &$val ) { + if ( $key === $marker ) continue; + + $key = $space . $s . ( $isSeq ? "" : "'{$html( $key, 0 )}' => " ); + + $dump = $this->kintLite( $val, $level + 1 ); + $output[] = "{$key}{$dump}"; + } + + unset( $var[$marker] ); + $output[] = "$space]"; + + } else { + $output[] = "[\n$space$s*depth too great*\n$space]"; + } + return "array({$count( $var )}) {$implode( "\n", $output )}"; + } elseif ( is_object( $var ) ) { + if ( $var instanceof SplFileInfo ) { + return "object SplFileInfo " . $var->getRealPath(); + } + + // Copy the object as an array + $array = (array)$var; + + $output = array(); + $space = str_repeat( $s = ' ', $level ); + + $hash = spl_object_hash( $var ); + + // Objects that are being dumped + static $objects = array(); + + if ( empty( $array ) ) { + return "object {$getClass( $var )} {}"; + } elseif ( isset( $objects[$hash] ) ) { + $output[] = "{\n$space$s*RECURSION*\n$space}"; + } elseif ( $level < 7 ) { + $output[] = "{"; + $objects[$hash] = true; + + foreach ( $array as $key => & $val ) { + if ( $key[0] === "\x00" ) { + + $access = $key[1] === "*" ? "protected" : "private"; + + // Remove the access level from the variable name + $key = substr( $key, strrpos( $key, "\x00" ) + 1 ); + } else { + $access = "public"; + } + + $output[] = "$space$s$access $key -> " . $this->kintLite( $val, $level + 1 ); + } + unset( $objects[$hash] ); + $output[] = "$space}"; + + } else { + $output[] = "{\n$space$s*depth too great*\n$space}"; + } + + return "object {$getClass( $var )} ({$count( $array )}) {$implode( "\n", $output )}"; + } else { + return gettype( $var ) . htmlspecialchars( var_export( $var, true ), ENT_NOQUOTES ); + } + } } \ No newline at end of file