1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| class Debug {
/** Var_dump personnalisé tiré de www.developpez.com
* @param obj Objet ou tableau à afficher
* @param prefix Affiche le préfixe ou pas (??????)
* @param stack [rec] Pile de stockage
* @param depth [rec] Profondeur actuelle
* @return Ancienmment $str
*/
// virrer la référence sur le $depth;
static public function dumpFormat( $obj, $prefix = false, &$stack = array(), $depth = 0 ) {
$str = '';
$struct = array();
// --- Format de dump ----------------------------------------------------
$format = array (
' <b><span style="color:#66B">%1$s</span></b>
=> <i><span style="color:#6B6">(%2$s)</span></i> %3$s<br />', // Variable simple
'<div style="border-left:3px solid #66B;margin-left:20px;margin-top:10px"><b>
<span style="color:#66B;background-color:#DDD"> %1$s </span></b>
=> <b>{</b> <span style="color:#6B6">(%2$s)</span><br />', // Début de container
' <b>}</b><span style="color:#66B">(%1$s)</span></div>', // Fin de container
'', // Indentation
'key' // repère de balisage
);
// --- On fait le dump ---------------------------------------------------
if ( is_null( $obj ) ) {
$str .= str_repeat( $format[3], $depth ).sprintf($format[0], '[NULL]', 'NULL', 'NULL', $depth );
}
if ( is_object( $obj ) ) {
$struct = get_object_vars( $obj );
}
else {
$struct = $obj; ///// AJOUT
}
if ( is_scalar( $struct ) ) {
$str .= str_repeat( $format[3], $depth ).sprintf( $format[0], '[unknown]', gettype( $struct ), $struct, $depth );
}
else {
//var_dump( $struct );
foreach ( $struct as $key => $val ) {
if ( $prefix ) {
if ( preg_match( '/^\d/', $key ) ) {
$key = $prefix.$key;
}
else { /* Ne rien faire */ }
}
else { /* Ne rien faire */ }
$type = gettype( $val );
if ( is_object( $val ) ) {
array_push( $stack, ${$format[4]} );
$str .= str_repeat( $format[3], $depth ).sprintf( $format[1], $key, $type, $val, $depth );
$str .= Debug::dumpFormat( get_object_vars($val), $prefix, $stack, $depth + 1); // AJOUT +1
}
elseif ( is_array( $val ) ) {
array_push( $stack, ${$format[4]} );
$str .= str_repeat( $format[3], $depth ).sprintf( $format[1], $key, $type, $val, $depth);
$str .= Debug::dumpFormat( $val, $prefix, $stack, $depth + 1); // AJOUT +1
}
elseif ( is_scalar( $val ) ) {
$str .= str_repeat( $format[3], $depth ).sprintf( $format[0], $key, $type, $val, $depth );
}
}
}
$depth--;
if ( $depth != -1 ) {
${$format[4]} = array_pop( $stack );
return ($str .= str_repeat( $format[3], $depth ).sprintf( $format[2], $key, $type, $val, $depth ));
}
else {
return $str;
}
}
}
$a = new StdClass;
$a->dd = 'AA';
$b = new StdClass;
$b->da = $a;
$b->ar = array("str", 33);
$c = new StdClass;
$c->cc = $b;
echo Debug::dumpFormat($c); |
Partager