PageRenderTime 34ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Debug.php

https://bitbucket.org/dbaltas/zend-framework-1.x-on-git
PHP | 113 lines | 45 code | 11 blank | 57 comment | 7 complexity | ad01cf73c7ad487f1d6b9f7a2046225f MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT
  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-2012 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: Debug.php 25094 2012-11-07 20:10:40Z rob $
  20. */
  21. /**
  22. * Concrete class for generating debug dumps related to the output source.
  23. *
  24. * @category Zend
  25. * @package Zend_Debug
  26. * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class Zend_Debug
  30. {
  31. /**
  32. * @var string
  33. */
  34. protected static $_sapi = null;
  35. /**
  36. * Get the current value of the debug output environment.
  37. * This defaults to the value of PHP_SAPI.
  38. *
  39. * @return string;
  40. */
  41. public static function getSapi()
  42. {
  43. if (self::$_sapi === null) {
  44. self::$_sapi = PHP_SAPI;
  45. }
  46. return self::$_sapi;
  47. }
  48. /**
  49. * Set the debug ouput environment.
  50. * Setting a value of null causes Zend_Debug to use PHP_SAPI.
  51. *
  52. * @param string $sapi
  53. * @return void;
  54. */
  55. public static function setSapi($sapi)
  56. {
  57. self::$_sapi = $sapi;
  58. }
  59. /**
  60. * Debug helper function. This is a wrapper for var_dump() that adds
  61. * the <pre /> tags, cleans up newlines and indents, and runs
  62. * htmlentities() before output.
  63. *
  64. * @param mixed $var The variable to dump.
  65. * @param string $label OPTIONAL Label to prepend to output.
  66. * @param bool $echo OPTIONAL Echo output if true.
  67. * @return string
  68. */
  69. public static function dump($var, $label=null, $echo=true)
  70. {
  71. // format the label
  72. $label = ($label===null) ? '' : rtrim($label) . ' ';
  73. // var_dump the variable into a buffer and keep the output
  74. ob_start();
  75. var_dump($var);
  76. $output = ob_get_clean();
  77. // neaten the newlines and indents
  78. $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
  79. if (self::getSapi() == 'cli') {
  80. $output = PHP_EOL . $label
  81. . PHP_EOL . $output
  82. . PHP_EOL;
  83. } else {
  84. if(!extension_loaded('xdebug')) {
  85. $flags = ENT_QUOTES;
  86. // PHP 5.4.0+
  87. if (defined('ENT_SUBSTITUTE')) {
  88. $flags = ENT_QUOTES | ENT_SUBSTITUTE;
  89. }
  90. $output = htmlspecialchars($output, $flags);
  91. }
  92. $output = '<pre>'
  93. . $label
  94. . $output
  95. . '</pre>';
  96. }
  97. if ($echo) {
  98. echo($output);
  99. }
  100. return $output;
  101. }
  102. }