1: <?php
2: /**
3: * Copyright 2007-2008 Maintainable Software, LLC
4: * Copyright 2006-2012 Horde LLC (http://www.horde.org/)
5: *
6: * @author Mike Naberezny <mike@maintainable.com>
7: * @author Derek DeVries <derek@maintainable.com>
8: * @author Chuck Hagenbuch <chuck@horde.org>
9: * @license http://www.horde.org/licenses/bsd
10: * @category Horde
11: * @package View
12: * @subpackage Helper
13: */
14:
15: /**
16: * Dumps a variable for inspection.
17: *
18: * Portions borrowed from Paul M. Jones' Solar_Debug
19: *
20: * @author Mike Naberezny <mike@maintainable.com>
21: * @author Derek DeVries <derek@maintainable.com>
22: * @author Chuck Hagenbuch <chuck@horde.org>
23: * @license http://www.horde.org/licenses/bsd
24: * @category Horde
25: * @package View
26: * @subpackage Helper
27: */
28: class Horde_View_Helper_Debug extends Horde_View_Helper_Base
29: {
30: /**
31: * Dumps a variable for inspection.
32: *
33: * @param mixed $var A variable.
34: *
35: * @return string Debug output of the variable.
36: */
37: public function debug($var)
38: {
39: return '<pre class="debug_dump">'
40: . htmlspecialchars($this->_fetch($var))
41: . '</pre>';
42: }
43:
44: /**
45: * Pretty exception dumper.
46: *
47: * Inspired by:
48: * http://www.sitepoint.com/blogs/2006/04/04/pretty-blue-screen/ and
49: * http://www.sitepoint.com/blogs/2006/08/12/pimpin-harrys-pretty-bluescreen/.
50: *
51: * Also see for future ideas:
52: * http://mikenaberezny.com/archives/55
53: *
54: * @param Exception $e An exception to dump.
55: *
56: * @return string Debug output of the exception.
57: */
58: public function dump(Exception $e)
59: {
60: $input = array(
61: 'type' => get_class($e),
62: 'code' => $e->getCode(),
63: 'message' => $e->getMessage(),
64: 'line' => $e->getLine(),
65: 'file' => $e->getFile(),
66: 'trace' => $e->getTrace(),
67: );
68:
69: // Store previous output.
70: $previous_output = ob_get_contents();
71:
72: $desc = $input['type'] . ' making ' . $_SERVER['REQUEST_METHOD'] . ' request to ' . $_SERVER['REQUEST_URI'];
73: return $this->render('_dump.html.php');
74: }
75:
76: /**
77: * Returns formatted output from var_dump().
78: *
79: * Buffers the var_dump() output for a variable and applies some simple
80: * formatting for readability.
81: *
82: * @param mixed $var Variable to dump.
83: *
84: * @return string Formatted results of var_dump().
85: */
86: protected function _fetch($var)
87: {
88: ob_start();
89: var_dump($var);
90: return preg_replace('/\]\=\>\n(\s+)/m', '] => ', ob_get_clean());
91: }
92:
93: protected function _sub($f)
94: {
95: $loc = '';
96: if (isset($f['class'])) {
97: $loc .= $f['class'] . $f['type'];
98: }
99: if (isset($f['function'])) {
100: $loc .= $f['function'];
101: }
102: if (!empty($loc)) {
103: $loc = htmlspecialchars($loc);
104: $loc = "<strong>$loc</strong>";
105: }
106: return $loc;
107: }
108:
109: protected function _clean($line)
110: {
111: $l = trim(strip_tags($line));
112: return $l ? $l : ' ';
113: }
114:
115: protected function _parms($f)
116: {
117: if (isset($f['function'])) {
118: try {
119: if (isset($f['class'])) {
120: $r = new ReflectionMethod($f['class'] . '::' . $f['function']);
121: } else {
122: $r = new ReflectionFunction($f['function']);
123: }
124: return $r->getParameters();
125: } catch(Exception $e) {
126: }
127: }
128: return array();
129: }
130:
131: protected function _src2lines($file)
132: {
133: $src = nl2br(highlight_file($file, true));
134: return explode('<br />', $src);
135: }
136: }
137: