1 | <?php |
---|
2 | |
---|
3 | /** |
---|
4 | * Dumps values of the given variable, or the entire data if nothing provided |
---|
5 | * <pre> |
---|
6 | * * var : the variable to display |
---|
7 | * * show_methods : if set to true, the public methods of any object encountered are also displayed |
---|
8 | * </pre> |
---|
9 | * This software is provided 'as-is', without any express or implied warranty. |
---|
10 | * In no event will the authors be held liable for any damages arising from the use of this software. |
---|
11 | * |
---|
12 | * @author Jordi Boggiano <j.boggiano@seld.be> |
---|
13 | * @copyright Copyright (c) 2008, Jordi Boggiano |
---|
14 | * @license http://dwoo.org/LICENSE Modified BSD License |
---|
15 | * @link http://dwoo.org/ |
---|
16 | * @version 1.0.0 |
---|
17 | * @date 2008-10-23 |
---|
18 | * @package Dwoo |
---|
19 | */ |
---|
20 | class Dwoo_Plugin_dump extends Dwoo_Plugin |
---|
21 | { |
---|
22 | protected $outputObjects; |
---|
23 | protected $outputMethods; |
---|
24 | |
---|
25 | public function process($var = '$', $show_methods = false) |
---|
26 | { |
---|
27 | $this->outputMethods = $show_methods; |
---|
28 | if ($var === '$') { |
---|
29 | $var = $this->dwoo->getData(); |
---|
30 | $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">data'; |
---|
31 | } else { |
---|
32 | $out = '<div style="background:#aaa; padding:5px; margin:5px; color:#000;">dump'; |
---|
33 | } |
---|
34 | |
---|
35 | $this->outputObjects = array(); |
---|
36 | |
---|
37 | if (!is_array($var)) { |
---|
38 | if (is_object($var)) { |
---|
39 | return $this->exportObj('', $var); |
---|
40 | } else { |
---|
41 | return $this->exportVar('', $var); |
---|
42 | } |
---|
43 | } |
---|
44 | |
---|
45 | $scope = $this->dwoo->getScope(); |
---|
46 | |
---|
47 | if ($var === $scope) { |
---|
48 | $out .= ' (current scope): <div style="background:#ccc;">'; |
---|
49 | } else { |
---|
50 | $out .= ':<div style="padding-left:20px;">'; |
---|
51 | } |
---|
52 | |
---|
53 | $out .= $this->export($var, $scope); |
---|
54 | |
---|
55 | return $out .'</div></div>'; |
---|
56 | } |
---|
57 | |
---|
58 | protected function export($var, $scope) |
---|
59 | { |
---|
60 | $out = ''; |
---|
61 | foreach ($var as $i=>$v) { |
---|
62 | if (is_array($v) || (is_object($v) && $v instanceof Iterator)) { |
---|
63 | $out .= $i.' ('.(is_array($v) ? 'array':'object: '.get_class($v)).')'; |
---|
64 | if ($v===$scope) { |
---|
65 | $out .= ' (current scope):<div style="background:#ccc;padding-left:20px;">'.$this->export($v, $scope).'</div>'; |
---|
66 | } else { |
---|
67 | $out .= ':<div style="padding-left:20px;">'.$this->export($v, $scope).'</div>'; |
---|
68 | } |
---|
69 | } elseif (is_object($v)) { |
---|
70 | $out .= $this->exportObj($i.' (object: '.get_class($v).'):', $v); |
---|
71 | } else { |
---|
72 | $out .= $this->exportVar($i.' = ', $v); |
---|
73 | } |
---|
74 | } |
---|
75 | return $out; |
---|
76 | } |
---|
77 | |
---|
78 | protected function exportVar($i, $v) |
---|
79 | { |
---|
80 | if (is_string($v) || is_bool($v) || is_numeric($v)) { |
---|
81 | return $i.htmlentities(var_export($v, true)).'<br />'; |
---|
82 | } elseif (is_null($v)) { |
---|
83 | return $i.'null<br />'; |
---|
84 | } elseif (is_resource($v)) { |
---|
85 | return $i.'resource('.get_resource_type($v).')<br />'; |
---|
86 | } else { |
---|
87 | return $i.htmlentities(var_export($v, true)).'<br />'; |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | protected function exportObj($i, $obj) |
---|
92 | { |
---|
93 | if (array_search($obj, $this->outputObjects, true) !== false) { |
---|
94 | return $i . ' [recursion, skipped]<br />'; |
---|
95 | } |
---|
96 | |
---|
97 | $this->outputObjects[] = $obj; |
---|
98 | |
---|
99 | $list = (array) $obj; |
---|
100 | |
---|
101 | $protectedLength = strlen(get_class($obj)) + 2; |
---|
102 | |
---|
103 | $out = array(); |
---|
104 | |
---|
105 | if ($this->outputMethods) { |
---|
106 | $ref = new ReflectionObject($obj); |
---|
107 | |
---|
108 | foreach ($ref->getMethods() as $method) { |
---|
109 | if (!$method->isPublic()) { |
---|
110 | continue; |
---|
111 | } |
---|
112 | |
---|
113 | if (empty($out['method'])) { |
---|
114 | $out['method'] = ''; |
---|
115 | } |
---|
116 | |
---|
117 | $params = array(); |
---|
118 | foreach ($method->getParameters() as $param) { |
---|
119 | $params[] = ($param->isPassedByReference() ? '&':'') . '$'.$param->getName() . ($param->isOptional() ? ' = '.var_export($param->getDefaultValue(), true) : ''); |
---|
120 | } |
---|
121 | |
---|
122 | $out['method'] .= '(method) ' . $method->getName() .'('.implode(', ', $params).')<br />'; |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | foreach ($list as $attributeName => $attributeValue) { |
---|
127 | if(property_exists($obj, $attributeName)) { |
---|
128 | $key = 'public'; |
---|
129 | } elseif(substr($attributeName, 0, 3) === "\0*\0") { |
---|
130 | $key = 'protected'; |
---|
131 | $attributeName = substr($attributeName, 3); |
---|
132 | } else { |
---|
133 | $key = 'private'; |
---|
134 | $attributeName = substr($attributeName, $protectedLength); |
---|
135 | } |
---|
136 | |
---|
137 | if (empty($out[$key])) { |
---|
138 | $out[$key] = ''; |
---|
139 | } |
---|
140 | |
---|
141 | $out[$key] .= '('.$key.') '; |
---|
142 | |
---|
143 | if (is_array($attributeValue)) { |
---|
144 | $out[$key] .= $attributeName.' (array):<br /> |
---|
145 | <div style="padding-left:20px;">'.$this->export($attributeValue, false).'</div>'; |
---|
146 | } elseif (is_object($attributeValue)) { |
---|
147 | $out[$key] .= $this->exportObj($attributeName.' (object: '.get_class($attributeValue).'):', $attributeValue); |
---|
148 | } else { |
---|
149 | $out[$key] .= $this->exportVar($attributeName.' = ', $attributeValue); |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | $return = $i . '<br /><div style="padding-left:20px;">'; |
---|
154 | |
---|
155 | if (!empty($out['method'])) { |
---|
156 | $return .= $out['method']; |
---|
157 | } |
---|
158 | |
---|
159 | if (!empty($out['public'])) { |
---|
160 | $return .= $out['public']; |
---|
161 | } |
---|
162 | |
---|
163 | if (!empty($out['protected'])) { |
---|
164 | $return .= $out['protected']; |
---|
165 | } |
---|
166 | |
---|
167 | if (!empty($out['private'])) { |
---|
168 | $return .= $out['private']; |
---|
169 | } |
---|
170 | |
---|
171 | return $return . '</div>'; |
---|
172 | } |
---|
173 | } |
---|