Debug.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Debug
  17. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. */
  20. /**
  21. * Concrete class for generating debug dumps related to the output source.
  22. *
  23. * @category Zend
  24. * @package Zend_Debug
  25. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  26. * @license http://framework.zend.com/license/new-bsd New BSD License
  27. */
  28. class Zend_Debug
  29. {
  30. /**
  31. * @var string
  32. */
  33. protected static $_sapi = null;
  34. /**
  35. * Get the current value of the debug output environment.
  36. * This defaults to the value of PHP_SAPI.
  37. *
  38. * @return string;
  39. */
  40. public static function getSapi()
  41. {
  42. if (self::$_sapi === null) {
  43. self::$_sapi = PHP_SAPI;
  44. }
  45. return self::$_sapi;
  46. }
  47. /**
  48. * Set the debug ouput environment.
  49. * Setting a value of null causes Zend_Debug to use PHP_SAPI.
  50. *
  51. * @param string $sapi
  52. * @return void;
  53. */
  54. public static function setSapi($sapi)
  55. {
  56. self::$_sapi = $sapi;
  57. }
  58. /**
  59. * Debug helper function. This is a wrapper for var_dump() that adds
  60. * the <pre /> tags, cleans up newlines and indents, and runs
  61. * htmlentities() before output.
  62. *
  63. * @param mixed $var The variable to dump.
  64. * @param string $label OPTIONAL Label to prepend to output.
  65. * @param bool $echo OPTIONAL Echo output if true.
  66. * @return string
  67. */
  68. public static function dump($var, $label=null, $echo=true)
  69. {
  70. // format the label
  71. $label = ($label===null) ? '' : rtrim($label) . ' ';
  72. // var_dump the variable into a buffer and keep the output
  73. ob_start();
  74. var_dump($var);
  75. $output = ob_get_clean();
  76. // neaten the newlines and indents
  77. $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  78. if (self::getSapi() == 'cli') {
  79. $output = PHP_EOL . $label
  80. . PHP_EOL . $output
  81. . PHP_EOL;
  82. } else {
  83. if(!extension_loaded('xdebug')) {
  84. $output = htmlspecialchars($output, ENT_QUOTES);
  85. }
  86. $output = '<pre>'
  87. . $label
  88. . $output
  89. . '</pre>';
  90. }
  91. if ($echo) {
  92. echo($output);
  93. }
  94. return $output;
  95. }
  96. }