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