3 * Krumo: Structured information display solution
5 * Krumo is a debugging tool (PHP4/PHP5), which displays structured information
6 * about any PHP variable. It is a nice replacement for print_r() or var_dump()
7 * which are used by a lot of PHP developers.
9 * @author Kaloyan K. Tsvetkov <kaloyan@kaloyan.info>
10 * @license http://opensource.org/licenses/lgpl-license.php GNU Lesser General Public License Version 2.1
13 * @version $Id: class.krumo.php,v 1.1.2.4 2009/02/11 04:01:01 weitzman Exp $
16 //////////////////////////////////////////////////////////////////////////////
19 * backward compatibility: the DIR_SEP constant isn't used anymore
21 if(!defined('DIR_SEP')) {
22 define('DIR_SEP', DIRECTORY_SEPARATOR
);
25 * backward compatibility: the PATH_SEPARATOR constant is availble since 4.3.0RC2
27 if (!defined('PATH_SEPARATOR')) {
28 define('PATH_SEPARATOR', OS_WINDOWS ?
';' : ':');
31 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
34 * Set the KRUMO_DIR constant up with the absolute path to Krumo files. If it is
35 * not defined, include_path will be used. Set KRUMO_DIR only if any other module
36 * or application has not already set it up.
38 if (!defined('KRUMO_DIR')) {
39 define('KRUMO_DIR', dirname(__FILE__
) . DIRECTORY_SEPARATOR
);
43 * This constant sets the maximum strings of strings that will be shown
44 * as they are. Longer strings will be truncated with this length, and
45 * their `full form` will be shown in a child node.
47 if (!defined('KRUMO_TRUNCATE_LENGTH')) {
48 define('KRUMO_TRUNCATE_LENGTH', 50);
51 //////////////////////////////////////////////////////////////////////////////
56 * This class stores the Krumo API for rendering and
57 * displaying the structured information it is reporting
64 * Return Krumo version
74 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
77 * Prints a debug backtrace
82 Function backtrace() {
86 if (!krumo
::_debug()) {
92 return krumo
::dump(debug_backtrace());
96 * Prints a list of all currently declared classes.
105 if (!krumo
::_debug()) {
112 <div
class="krumo-title">
113 This is a
list of all currently declared classes
.
116 return krumo
::dump(get_declared_classes());
120 * Prints a list of all currently declared interfaces (PHP5 only).
125 Function interfaces() {
129 if (!krumo
::_debug()) {
136 <div
class="krumo-title">
137 This is a
list of all currently declared interfaces
.
140 return krumo
::dump(get_declared_interfaces());
144 * Prints a list of all currently included (or required) files.
149 Function includes() {
153 if (!krumo
::_debug()) {
160 <div
class="krumo-title">
161 This is a
list of all currently
included (or required
) files
.
164 return krumo
::dump(get_included_files());
168 * Prints a list of all currently declared functions.
173 Function functions() {
177 if (!krumo
::_debug()) {
184 <div
class="krumo-title">
185 This is a
list of all currently declared functions
.
188 return krumo
::dump(get_defined_functions());
192 * Prints a list of all currently declared constants.
201 if (!krumo
::_debug()) {
208 <div
class="krumo-title">
209 This is a
list of all currently declared
constants (defines
).
212 return krumo
::dump(get_defined_constants());
216 * Prints a list of all currently loaded PHP extensions.
221 Function extensions() {
225 if (!krumo
::_debug()) {
232 <div
class="krumo-title">
233 This is a
list of all currently loaded PHP extensions
.
236 return krumo
::dump(get_loaded_extensions());
240 * Prints a list of all HTTP request headers.
249 if (!krumo
::_debug()) {
256 <div
class="krumo-title">
257 This is a
list of all HTTP request headers
.
260 return krumo
::dump(getAllHeaders());
264 * Prints a list of the configuration settings read from <i>php.ini</i>
273 if (!krumo
::_debug()) {
280 <div
class="krumo-title">
281 This is a
list of the configuration settings read from
<code
><b
><?php
echo get_cfg_var('cfg_file_path');?
></b
></code
>.
284 return krumo
::dump(parse_ini_file(get_cfg_var('cfg_file_path'), true));
288 * Prints a list of all your configuration settings.
297 if (!krumo
::_debug()) {
304 <div
class="krumo-title">
305 This is a
list of all your configuration settings
.
308 return krumo
::dump(ini_get_all());
312 * Prints a list of the specified directories under your <i>include_path</i> option.
321 if (!krumo
::_debug()) {
328 <div
class="krumo-title">
329 This is a
list of the specified directories under your
<code
><b
>include_path
</b
></code
> option
.
332 return krumo
::dump(explode(PATH_SEPARATOR
, ini_get('include_path')));
336 * Prints a list of all the values from the <i>$_REQUEST</i> array.
345 if (!krumo
::_debug()) {
352 <div
class="krumo-title">
353 This is a
list of all the values from the
<code
><b
>$_REQUEST</b
></code
> array.
356 return krumo
::dump($_REQUEST);
360 * Prints a list of all the values from the <i>$_GET</i> array.
369 if (!krumo
::_debug()) {
376 <div
class="krumo-title">
377 This is a
list of all the values from the
<code
><b
>$_GET</b
></code
> array.
380 return krumo
::dump($_GET);
384 * Prints a list of all the values from the <i>$_POST</i> array.
393 if (!krumo
::_debug()) {
400 <div
class="krumo-title">
401 This is a
list of all the values from the
<code
><b
>$_POST</b
></code
> array.
404 return krumo
::dump($_POST);
408 * Prints a list of all the values from the <i>$_SERVER</i> array.
417 if (!krumo
::_debug()) {
424 <div
class="krumo-title">
425 This is a
list of all the values from the
<code
><b
>$_SERVER</b
></code
> array.
428 return krumo
::dump($_SERVER);
432 * Prints a list of all the values from the <i>$_COOKIE</i> array.
441 if (!krumo
::_debug()) {
448 <div
class="krumo-title">
449 This is a
list of all the values from the
<code
><b
>$_COOKIE</b
></code
> array.
452 return krumo
::dump($_COOKIE);
456 * Prints a list of all the values from the <i>$_ENV</i> array.
465 if (!krumo
::_debug()) {
472 <div
class="krumo-title">
473 This is a
list of all the values from the
<code
><b
>$_ENV</b
></code
> array.
476 return krumo
::dump($_ENV);
480 * Prints a list of all the values from the <i>$_SESSION</i> array.
489 if (!krumo
::_debug()) {
496 <div
class="krumo-title">
497 This is a
list of all the values from the
<code
><b
>$_SESSION</b
></code
> array.
500 return krumo
::dump($_SESSION);
504 * Prints a list of all the values from an INI file.
506 * @param string $ini_file
511 Function ini($ini_file) {
515 if (!krumo
::_debug()) {
521 if (!$_ = @parse_ini_file
($ini_file, 1)) {
528 <div
class="krumo-title">
529 This is a
list of all the values from the
<code
><b
><?php
echo realpath($ini_file) ?
realpath($ini_file) : $ini_file;?
></b
></code
> INI file
.
532 return krumo
::dump($_);
535 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
538 * Dump information about a variable
540 * @param mixed $data,...
544 Function dump($data) {
548 if (!krumo
::_debug()) {
554 if (func_num_args() > 1) {
555 $_ = func_get_args();
568 // DEVEL: we added array_reverse() so the proper file+line number is found.
569 $_ = array_reverse(debug_backtrace());
570 while($d = array_pop($_)) {
571 // DEVEL: changed if() condition below
572 if ((strpos(@$d['file'], 'devel') === FALSE) && (strpos(@$d['file'], 'krumo') === FALSE) && @$d['class'] != 'krumo') {
580 <div
class="krumo-root" dir
="ltr">
581 <ul
class="krumo-node krumo-first">
582 <?php
echo krumo
::_dump($data);?
>
583 <li
class="krumo-footnote">
584 <div
class="krumo-version" style
="white-space:nowrap;">
585 <h6
>Krumo version
<?php
echo krumo
::version();?
></h6
> |
<a
586 href
="http://krumo.sourceforge.net"
587 target
="_blank">http
://krumo.sourceforge.net</a>
590 <?php
if (@$d['file']) { ?
>
591 <span
class="krumo-call" style
="white-space:nowrap;">
592 Called from
<code
><?php
echo $d['file']?
></code
>,
593 line
<code
><?php
echo $d['line']?
></code
></span
>
602 $_recursion_marker = krumo
::_marker();
603 if ($hive =& krumo
::_hive($dummy)) {
604 foreach($hive as $i=>$bee){
605 if (is_object($bee)) {
606 unset($hive[$i]->$_recursion_marker);
608 unset($hive[$i][$_recursion_marker]);
613 // PHP 4.x.x array reference bug...
615 if (is_array($data) && version_compare(PHP_VERSION
, "5", "<")) {
616 unset($GLOBALS[krumo
::_marker()]);
620 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
623 * Print the skin (CSS)
631 static $_css = false;
641 // DEVEL: changed for Drupal variables system
642 $skin = variable_get('devel_krumo_skin', 'orange');
644 // custom selected skin ?
646 $_ = KRUMO_DIR
. "skins/{$skin}/skin.css";
647 if ($fp = @fopen
($_, 'r', 1)) {
648 $css = fread($fp, filesize($_));
654 if (!$css && ($skin != 'default')) {
656 $_ = KRUMO_DIR
. "skins/default/skin.css";
657 $css = join('', @file
($_));
662 if ($_css = $css != '') {
667 // DEVEL: changed for Drupal path system.
668 $css_url = url(drupal_get_path('module', 'devel')) . "/krumo/skins/{$skin}/";
670 $css = preg_replace('~%url%~Uis', $css_url, $css);
675 <!-- Using Krumo Skin
: <?php
echo preg_replace('~^' . preg_quote(realpath(KRUMO_DIR
) . DIRECTORY_SEPARATOR
) . '~Uis', '', realpath($_));?
> -->
676 <style type
="text/css">
686 <script type
="text/javascript">
688 <?php
echo join(file(KRUMO_DIR
. "krumo.js"));?
>
698 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
708 return true === krumo
::_debug(true);
719 return false === krumo
::_debug(false);
723 * Get\Set Krumo state: whether it is enabled or disabled
725 * @param boolean $state
730 Function _debug($state=null) {
737 $_ = (boolean
) $state;
745 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
748 * Dump information about a variable
751 * @param string $name
755 Function _dump(&$data, $name='...') {
759 if (is_object($data)) {
760 return krumo
::_object($data, $name);
765 if (is_array($data)) {
767 // PHP 4.x.x array reference bug...
769 if (version_compare(PHP_VERSION
, "5", "<")) {
771 // prepare the GLOBAL reference list...
773 if (!isset($GLOBALS[krumo
::_marker()])) {
774 $GLOBALS[krumo
::_marker()] = array();
776 if (!is_array($GLOBALS[krumo
::_marker()])) {
777 $GLOBALS[krumo
::_marker()] = (array) $GLOBALS[krumo
::_marker()];
782 if (!empty($GLOBALS[krumo
::_marker()])) {
783 $d = array_shift($GLOBALS[krumo
::_marker()]);
790 return krumo
::_array($data, $name);
795 if (is_resource($data)) {
796 return krumo
::_resource($data, $name);
801 if (is_string($data)) {
802 return krumo
::_string($data, $name);
805 if (is_float($data)) {
806 return krumo
::_float($data, $name);
809 if (is_integer($data)) {
810 return krumo
::_integer($data, $name);
813 if (is_bool($data)) {
814 return krumo
::_boolean($data, $name);
819 if (is_null($data)) {
820 return krumo
::_null($name);
824 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
827 * Render a dump for a NULL value
829 * @param string $name
834 Function _null($name) {
836 <li
class="krumo-child">
837 <div
class="krumo-element"
838 onMouseOver
="krumo.over(this);"
839 onMouseOut
="krumo.out(this);">
841 <a
class="krumo-name"><?php
echo $name;?
></a
>
842 (<em
class="krumo-type krumo-null">NULL</em
>)
848 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
851 * Return the marked used to stain arrays
852 * and objects in order to detect recursions
860 static $_recursion_marker;
861 if (!isset($_recursion_marker)) {
862 $_recursion_marker = uniqid('krumo');
865 return $_recursion_marker;
868 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
871 * Adds a variable to the hive of arrays and objects which
872 * are tracked for whether they have recursive entries
874 * @param mixed &$bee either array or object, not a scallar vale
875 * @return array all the bees
880 Function &_hive(&$bee) {
886 if (!is_null($bee)) {
890 $_recursion_marker = krumo
::_marker();
892 ?
@($bee->$_recursion_marker++
)
893 : @($bee[$_recursion_marker]++
);
903 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
906 * Render a dump for the properties of an array or objeect
908 * @param mixed &$data
912 Function _vars(&$data) {
914 $_is_object = is_object($data);
916 // test for references in order to
917 // prevent endless recursion loops
919 $_recursion_marker = krumo
::_marker();
921 ?
@$data->$_recursion_marker
922 : @$data[$_recursion_marker] ;
925 // recursion detected
928 return krumo
::_recursion();
938 <div
class="krumo-nest" style
="display:none;">
939 <ul
class="krumo-node">
944 $keys = ($_is_object)
945 ?
array_keys(get_object_vars($data))
950 foreach($keys as $k) {
954 if ($k === $_recursion_marker) {
966 // PHP 4.x.x array reference bug...
968 if (is_array($data) && version_compare(PHP_VERSION
, "5", "<")) {
969 $GLOBALS[krumo
::_marker()][] =& $v;
979 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
982 * Render a block that detected recursion
987 Function _recursion() {
989 <div
class="krumo-nest" style
="display:none;">
990 <ul
class="krumo-node">
991 <li
class="krumo-child">
992 <div
class="krumo-element"
993 onMouseOver
="krumo.over(this);"
994 onMouseOut
="krumo.out(this);">
995 <a
class="krumo-name"><big
>∞</big></a>
996 (<em
class="krumo-type">Recursion
</em
>)
1005 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1008 * Render a dump for an array
1010 * @param mixed $data
1011 * @param string $name
1015 Function _array(&$data, $name) {
1017 <li
class="krumo-child">
1019 <div
class="krumo-element<?php echo count($data) > 0 ? ' krumo-expand' : '';?>"
1020 <?php
if (count($data) > 0) {?
> onClick
="krumo.toggle(this);"<?php
} ?
>
1021 onMouseOver
="krumo.over(this);"
1022 onMouseOut
="krumo.out(this);">
1024 <a
class="krumo-name"><?php
echo $name;?
></a
>
1025 (<em
class="krumo-type">Array, <strong
class="krumo-array-length"><?php
echo
1028 :(count($data)." elements");
1035 if (is_callable($data)) {
1036 $_ = array_values($data);
1038 <span
class="krumo-callback"> |
1039 (<em
class="krumo-type">Callback
</em
>)
1040 <strong
class="krumo-string"><?php
1041 echo htmlSpecialChars($_[0]);?
>::<?php
1042 echo htmlSpecialChars($_[1]);?
>();</strong
></span
>
1049 <?php
if (count($data)) {
1050 krumo
::_vars($data);
1056 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1059 * Render a dump for an object
1061 * @param mixed $data
1062 * @param string $name
1066 Function _object(&$data, $name) {
1068 <li
class="krumo-child">
1070 <div
class="krumo-element<?php echo count($data) > 0 ? ' krumo-expand' : '';?>"
1071 <?php
if (count($data) > 0) {?
> onClick
="krumo.toggle(this);"<?php
} ?
>
1072 onMouseOver
="krumo.over(this);"
1073 onMouseOut
="krumo.out(this);">
1075 <a
class="krumo-name"><?php
echo $name;?
></a
>
1076 (<em
class="krumo-type">Object</em
>)
1077 <strong
class="krumo-class"><?php
echo get_class($data);?
></strong
>
1080 <?php
if (count($data)) {
1081 krumo
::_vars($data);
1087 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1090 * Render a dump for a resource
1092 * @param mixed $data
1093 * @param string $name
1097 Function _resource($data, $name) {
1099 <li
class="krumo-child">
1101 <div
class="krumo-element"
1102 onMouseOver
="krumo.over(this);"
1103 onMouseOut
="krumo.out(this);">
1105 <a
class="krumo-name"><?php
echo $name;?
></a
>
1106 (<em
class="krumo-type">Resource</em
>)
1107 <strong
class="krumo-resource"><?php
echo get_resource_type($data);?
></strong
>
1114 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1117 * Render a dump for a boolean value
1119 * @param mixed $data
1120 * @param string $name
1124 Function _boolean($data, $name) {
1126 <li
class="krumo-child">
1128 <div
class="krumo-element"
1129 onMouseOver
="krumo.over(this);"
1130 onMouseOut
="krumo.out(this);">
1132 <a
class="krumo-name"><?php
echo $name;?
></a
>
1133 (<em
class="krumo-type">Boolean
</em
>)
1134 <strong
class="krumo-boolean"><?php
echo $data?
'TRUE':'FALSE'?
></strong
>
1141 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1144 * Render a dump for a integer value
1146 * @param mixed $data
1147 * @param string $name
1151 Function _integer($data, $name) {
1153 <li
class="krumo-child">
1155 <div
class="krumo-element"
1156 onMouseOver
="krumo.over(this);"
1157 onMouseOut
="krumo.out(this);">
1159 <a
class="krumo-name"><?php
echo $name;?
></a
>
1160 (<em
class="krumo-type">Integer</em
>)
1161 <strong
class="krumo-integer"><?php
echo $data;?
></strong
>
1168 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1171 * Render a dump for a float value
1173 * @param mixed $data
1174 * @param string $name
1178 Function _float($data, $name) {
1180 <li
class="krumo-child">
1182 <div
class="krumo-element"
1183 onMouseOver
="krumo.over(this);"
1184 onMouseOut
="krumo.out(this);">
1186 <a
class="krumo-name"><?php
echo $name;?
></a
>
1187 (<em
class="krumo-type">Float</em
>)
1188 <strong
class="krumo-float"><?php
echo $data;?
></strong
>
1195 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1198 * Render a dump for a string value
1200 * @param mixed $data
1201 * @param string $name
1205 Function _string($data, $name) {
1211 if (strLen($data) > KRUMO_TRUNCATE_LENGTH
) {
1212 $_ = substr($data, 0, KRUMO_TRUNCATE_LENGTH
- 3) . '...';
1216 <li
class="krumo-child">
1218 <div
class="krumo-element<?php echo $_extra ? ' krumo-expand' : '';?>"
1219 <?php
if ($_extra) {?
> onClick
="krumo.toggle(this);"<?php
} ?
>
1220 onMouseOver
="krumo.over(this);"
1221 onMouseOut
="krumo.out(this);">
1223 <a
class="krumo-name"><?php
echo $name;?
></a
>
1224 (<em
class="krumo-type">String,
1225 <strong
class="krumo-string-length"><?php
1226 echo strlen($data) ?
> characters
</strong
> </em
>)
1227 <strong
class="krumo-string"><?php
echo htmlSpecialChars($_);?
></strong
>
1232 if (is_callable($data)) {
1234 <span
class="krumo-callback"> |
1235 (<em
class="krumo-type">Callback
</em
>)
1236 <strong
class="krumo-string"><?php
echo htmlSpecialChars($_);?
>();</strong
></span
>
1243 <?php
if ($_extra) { ?
>
1244 <div
class="krumo-nest" style
="display:none;">
1245 <ul
class="krumo-node">
1247 <li
class="krumo-child">
1248 <div
class="krumo-preview"><?php
echo htmlSpecialChars($data);?
></div
>
1258 // -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
1263 //////////////////////////////////////////////////////////////////////////////
1266 * Alias of {@link krumo::dump()}
1268 * @param mixed $data,...
1270 * @see krumo::dump()
1273 $_ = func_get_args();
1274 return call_user_func_array(
1275 array('krumo', 'dump'), $_
1279 //////////////////////////////////////////////////////////////////////////////