Skip to content

Commit b51fc22

Browse files
committed
Merge pull request php-debugbar#122 from barryvdh/kintlite
Remove Kint dependency
2 parents f6da2ec + 511c7a1 commit b51fc22

File tree

2 files changed

+136
-3
lines changed

2 files changed

+136
-3
lines changed

composer.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
}],
1313
"require": {
1414
"php": ">=5.3.0",
15-
"psr/log": "~1.0",
16-
"raveren/kint": "0.9"
15+
"psr/log": "~1.0"
1716
},
1817
"require-dev": {
1918
"php": ">=5.3.0"

src/DebugBar/DataFormatter/DataFormatter.php

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class DataFormatter implements DataFormatterInterface
1414
{
1515
public function formatVar($data)
1616
{
17-
return \kintLite($data);
17+
return $this->kintLite($data);
1818
}
1919

2020
public function formatDuration($seconds)
@@ -36,4 +36,138 @@ public function formatBytes($size, $precision = 2)
3636
$suffixes = array('B', 'KB', 'MB', 'GB', 'TB');
3737
return round(pow(1024, $base - floor($base)), $precision) . $suffixes[floor($base)];
3838
}
39+
40+
/**
41+
* lightweight version of Kint::dump(). Uses whitespace for formatting instead of html
42+
* sadly not DRY yet
43+
*
44+
* Extracted from Kint.class.php in raveren/kint, https://github.com/raveren/kint
45+
* Copyright (c) 2013 Rokas Šleinius ([email protected])
46+
*
47+
* @param mixed $var
48+
* @param int $level
49+
*
50+
* @return string
51+
*/
52+
protected function kintLite(&$var, $level = 0)
53+
{
54+
// initialize function names into variables for prettier string output (html and implode are also DRY)
55+
$html = "htmlspecialchars";
56+
$implode = "implode";
57+
$strlen = "strlen";
58+
$count = "count";
59+
$getClass = "get_class";
60+
61+
62+
if ( $var === null ) {
63+
return 'NULL';
64+
} elseif ( is_bool( $var ) ) {
65+
return 'bool ' . ( $var ? 'TRUE' : 'FALSE' );
66+
} elseif ( is_float( $var ) ) {
67+
return 'float ' . $var;
68+
} elseif ( is_int( $var ) ) {
69+
return 'integer ' . $var;
70+
} elseif ( is_resource( $var ) ) {
71+
if ( ( $type = get_resource_type( $var ) ) === 'stream' AND $meta = stream_get_meta_data( $var ) ) {
72+
73+
if ( isset( $meta['uri'] ) ) {
74+
$file = $meta['uri'];
75+
76+
return "resource ({$type}) {$html( $file, 0 )}";
77+
} else {
78+
return "resource ({$type})";
79+
}
80+
} else {
81+
return "resource ({$type})";
82+
}
83+
} elseif ( is_string( $var ) ) {
84+
return "string ({$strlen( $var )}) \"{$html( $var )}\"";
85+
} elseif ( is_array( $var ) ) {
86+
$output = array();
87+
$space = str_repeat( $s = ' ', $level );
88+
89+
static $marker;
90+
91+
if ( $marker === null ) {
92+
// Make a unique marker
93+
$marker = uniqid( "\x00" );
94+
}
95+
96+
if ( empty( $var ) ) {
97+
return "array()";
98+
} elseif ( isset( $var[$marker] ) ) {
99+
$output[] = "[\n$space$s*RECURSION*\n$space]";
100+
} elseif ( $level < 7 ) {
101+
$isSeq = array_keys( $var ) === range( 0, count( $var ) - 1 );
102+
103+
$output[] = "[";
104+
105+
$var[$marker] = true;
106+
107+
108+
foreach ( $var as $key => &$val ) {
109+
if ( $key === $marker ) continue;
110+
111+
$key = $space . $s . ( $isSeq ? "" : "'{$html( $key, 0 )}' => " );
112+
113+
$dump = $this->kintLite( $val, $level + 1 );
114+
$output[] = "{$key}{$dump}";
115+
}
116+
117+
unset( $var[$marker] );
118+
$output[] = "$space]";
119+
120+
} else {
121+
$output[] = "[\n$space$s*depth too great*\n$space]";
122+
}
123+
return "array({$count( $var )}) {$implode( "\n", $output )}";
124+
} elseif ( is_object( $var ) ) {
125+
if ( $var instanceof SplFileInfo ) {
126+
return "object SplFileInfo " . $var->getRealPath();
127+
}
128+
129+
// Copy the object as an array
130+
$array = (array)$var;
131+
132+
$output = array();
133+
$space = str_repeat( $s = ' ', $level );
134+
135+
$hash = spl_object_hash( $var );
136+
137+
// Objects that are being dumped
138+
static $objects = array();
139+
140+
if ( empty( $array ) ) {
141+
return "object {$getClass( $var )} {}";
142+
} elseif ( isset( $objects[$hash] ) ) {
143+
$output[] = "{\n$space$s*RECURSION*\n$space}";
144+
} elseif ( $level < 7 ) {
145+
$output[] = "{";
146+
$objects[$hash] = true;
147+
148+
foreach ( $array as $key => & $val ) {
149+
if ( $key[0] === "\x00" ) {
150+
151+
$access = $key[1] === "*" ? "protected" : "private";
152+
153+
// Remove the access level from the variable name
154+
$key = substr( $key, strrpos( $key, "\x00" ) + 1 );
155+
} else {
156+
$access = "public";
157+
}
158+
159+
$output[] = "$space$s$access $key -> " . $this->kintLite( $val, $level + 1 );
160+
}
161+
unset( $objects[$hash] );
162+
$output[] = "$space}";
163+
164+
} else {
165+
$output[] = "{\n$space$s*depth too great*\n$space}";
166+
}
167+
168+
return "object {$getClass( $var )} ({$count( $array )}) {$implode( "\n", $output )}";
169+
} else {
170+
return gettype( $var ) . htmlspecialchars( var_export( $var, true ), ENT_NOQUOTES );
171+
}
172+
}
39173
}

0 commit comments

Comments
 (0)