|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kint\Parser; |
|
4
|
|
|
|
|
5
|
|
|
use DOMNamedNodeMap; |
|
6
|
|
|
use DOMNode; |
|
7
|
|
|
use DOMNodeList; |
|
8
|
|
|
use Kint\Object\BasicObject; |
|
9
|
|
|
use Kint\Object\InstanceObject; |
|
10
|
|
|
use Kint\Object\Representation\Representation; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* The DOMDocument parser plugin is particularly useful as it is both the only |
|
14
|
|
|
* way to see inside the DOMNode without print_r, and the only way to see mixed |
|
15
|
|
|
* text and node inside XML (SimpleXMLElement will strip out the text). |
|
16
|
|
|
*/ |
|
17
|
|
|
class DOMDocumentPlugin extends Plugin |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* List of properties to skip parsing. |
|
21
|
|
|
* |
|
22
|
|
|
* The properties of a DOMNode can do a *lot* of damage to debuggers. The |
|
23
|
|
|
* DOMNode contains not one, not two, not three, not four, not 5, not 6, |
|
24
|
|
|
* not 7 but 8 different ways to recurse into itself: |
|
25
|
|
|
* * firstChild |
|
26
|
|
|
* * lastChild |
|
27
|
|
|
* * previousSibling |
|
28
|
|
|
* * nextSibling |
|
29
|
|
|
* * ownerDocument |
|
30
|
|
|
* * parentNode |
|
31
|
|
|
* * childNodes |
|
32
|
|
|
* * attributes |
|
33
|
|
|
* |
|
34
|
|
|
* All of this combined: the tiny SVGs used as the caret in Kint are already |
|
35
|
|
|
* enough to make parsing and rendering take over a second, and send memory |
|
36
|
|
|
* usage over 128 megs. So we blacklist every field we don't strictly need |
|
37
|
|
|
* and hope that that's good enough. |
|
38
|
|
|
* |
|
39
|
|
|
* In retrospect - this is probably why print_r does the same |
|
40
|
|
|
* |
|
41
|
|
|
* @var array |
|
42
|
|
|
*/ |
|
43
|
|
|
public static $blacklist = array( |
|
44
|
|
|
'parentNode' => 'DOMNode', |
|
45
|
|
|
'firstChild' => 'DOMNode', |
|
46
|
|
|
'lastChild' => 'DOMNode', |
|
47
|
|
|
'previousSibling' => 'DOMNode', |
|
48
|
|
|
'nextSibling' => 'DOMNode', |
|
49
|
|
|
'ownerDocument' => 'DOMDocument', |
|
50
|
|
|
); |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Show all properties and methods. |
|
54
|
|
|
* |
|
55
|
|
|
* @var bool |
|
56
|
|
|
*/ |
|
57
|
|
|
public static $verbose = false; |
|
58
|
|
|
|
|
59
|
|
|
public function getTypes() |
|
60
|
|
|
{ |
|
61
|
|
|
return array('object'); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function getTriggers() |
|
|
|
|
|
|
65
|
|
|
{ |
|
66
|
|
|
return Parser::TRIGGER_SUCCESS; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
public function parse(&$var, BasicObject &$o, $trigger) |
|
|
|
|
|
|
70
|
|
|
{ |
|
71
|
|
|
if (!$o instanceof InstanceObject) { |
|
72
|
|
|
return; |
|
73
|
|
|
} elseif ($var instanceof DOMNamedNodeMap || $var instanceof DOMNodeList) { |
|
74
|
|
|
return $this->parseList($var, $o, $trigger); |
|
75
|
|
|
} elseif ($var instanceof DOMNode) { |
|
76
|
|
|
return $this->parseNode($var, $o, $trigger); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
protected function parseList(&$var, InstanceObject &$o, $trigger) |
|
|
|
|
|
|
81
|
|
|
{ |
|
82
|
|
|
// Recursion should never happen, should always be stopped at the parent |
|
83
|
|
|
// DOMNode. Depth limit on the other hand we're going to skip since |
|
84
|
|
|
// that would show an empty iterator and rather useless. Let the depth |
|
85
|
|
|
// limit hit the children (DOMNodeList only has DOMNode as children) |
|
86
|
|
|
if ($trigger & Parser::TRIGGER_RECURSION) { |
|
87
|
|
|
return; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
$o->size = $var->length; |
|
91
|
|
|
if ($o->size === 0) { |
|
92
|
|
|
$o->replaceRepresentation(new Representation('Iterator')); |
|
93
|
|
|
$o->size = null; |
|
94
|
|
|
|
|
95
|
|
|
return; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
// Depth limit |
|
99
|
|
|
// Make empty iterator representation since we need it in DOMNode to point out depth limits |
|
100
|
|
|
if ($this->parser->getDepthLimit() && $o->depth + 1 >= $this->parser->getDepthLimit()) { |
|
101
|
|
|
$b = new BasicObject(); |
|
|
|
|
|
|
102
|
|
|
$b->name = $o->classname.' Iterator Contents'; |
|
103
|
|
|
$b->access_path = 'iterator_to_array('.$o->access_path.')'; |
|
104
|
|
|
$b->depth = $o->depth + 1; |
|
105
|
|
|
$b->hints[] = 'depth_limit'; |
|
106
|
|
|
|
|
107
|
|
|
$r = new Representation('Iterator'); |
|
|
|
|
|
|
108
|
|
|
$r->contents = array($b); |
|
109
|
|
|
$o->replaceRepresentation($r, 0); |
|
110
|
|
|
|
|
111
|
|
|
return; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
$data = iterator_to_array($var); |
|
115
|
|
|
|
|
116
|
|
|
$r = new Representation('Iterator'); |
|
117
|
|
|
$o->replaceRepresentation($r, 0); |
|
118
|
|
|
|
|
119
|
|
|
foreach ($data as $key => $item) { |
|
120
|
|
|
$base_obj = new BasicObject(); |
|
|
|
|
|
|
121
|
|
|
$base_obj->depth = $o->depth + 1; |
|
|
|
|
|
|
122
|
|
|
$base_obj->name = $item->nodeName; |
|
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
if ($o->access_path) { |
|
|
|
|
|
|
125
|
|
|
if ($var instanceof DOMNamedNodeMap) { |
|
126
|
|
|
$base_obj->access_path = $o->access_path.'->getNamedItem('.var_export($key, true).')'; |
|
|
|
|
|
|
127
|
|
|
} elseif ($var instanceof DOMNodeList) { |
|
128
|
|
|
$base_obj->access_path = $o->access_path.'->item('.var_export($key, true).')'; |
|
|
|
|
|
|
129
|
|
|
} else { |
|
130
|
|
|
$base_obj->access_path = 'iterator_to_array('.$o->access_path.')'; |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
$r->contents[] = $this->parser->parse($item, $base_obj); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
protected function parseNode(&$var, InstanceObject &$o, $trigger) |
|
|
|
|
|
|
139
|
|
|
{ |
|
140
|
|
|
// Fill the properties |
|
141
|
|
|
// They can't be enumerated through reflection or casting, |
|
142
|
|
|
// so we have to trust the docs and try them one at a time |
|
143
|
|
|
$known_properties = array( |
|
|
|
|
|
|
144
|
|
|
'nodeValue', |
|
145
|
|
|
'childNodes', |
|
146
|
|
|
'attributes', |
|
147
|
|
|
); |
|
148
|
|
|
|
|
149
|
|
|
if (self::$verbose) { |
|
150
|
|
|
$known_properties = array( |
|
|
|
|
|
|
151
|
|
|
'nodeName', |
|
152
|
|
|
'nodeValue', |
|
153
|
|
|
'nodeType', |
|
154
|
|
|
'parentNode', |
|
155
|
|
|
'childNodes', |
|
156
|
|
|
'firstChild', |
|
157
|
|
|
'lastChild', |
|
158
|
|
|
'previousSibling', |
|
159
|
|
|
'nextSibling', |
|
160
|
|
|
'attributes', |
|
161
|
|
|
'ownerDocument', |
|
162
|
|
|
'namespaceURI', |
|
163
|
|
|
'prefix', |
|
164
|
|
|
'localName', |
|
165
|
|
|
'baseURI', |
|
166
|
|
|
'textContent', |
|
167
|
|
|
); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
$childNodes = array(); |
|
171
|
|
|
$attributes = array(); |
|
172
|
|
|
|
|
173
|
|
|
$rep = $o->value; |
|
174
|
|
|
|
|
175
|
|
|
foreach ($known_properties as $prop) { |
|
|
|
|
|
|
176
|
|
|
$prop_obj = $this->parseProperty($o, $prop, $var); |
|
|
|
|
|
|
177
|
|
|
$rep->contents[] = $prop_obj; |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
if ($prop === 'childNodes') { |
|
180
|
|
|
$childNodes = $prop_obj->getRepresentation('iterator'); |
|
|
|
|
|
|
181
|
|
|
} elseif ($prop === 'attributes') { |
|
182
|
|
|
$attributes = $prop_obj->getRepresentation('iterator'); |
|
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
if (!self::$verbose) { |
|
187
|
|
|
$o->removeRepresentation('methods'); |
|
188
|
|
|
$o->removeRepresentation('properties'); |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
// Attributes and comments and text nodes don't |
|
192
|
|
|
// need children or attributes of their own |
|
193
|
|
|
if (in_array($o->classname, array('DOMAttr', 'DOMText', 'DOMComment'))) { |
|
194
|
|
|
return; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
// Set the attributes |
|
198
|
|
|
if ($attributes) { |
|
199
|
|
|
$a = new Representation('Attributes'); |
|
|
|
|
|
|
200
|
|
|
foreach ($attributes->contents as $attribute) { |
|
201
|
|
|
$a->contents[] = self::textualNodeToString($attribute); |
|
202
|
|
|
} |
|
203
|
|
|
$o->addRepresentation($a, 0); |
|
204
|
|
|
} |
|
205
|
|
|
|
|
206
|
|
|
// Set the children |
|
207
|
|
|
if ($childNodes) { |
|
208
|
|
|
$c = new Representation('Children'); |
|
|
|
|
|
|
209
|
|
|
|
|
210
|
|
|
if (count($childNodes->contents) === 1 && ($node = reset($childNodes->contents)) && in_array('depth_limit', $node->hints)) { |
|
211
|
|
|
$node = $node->transplant(new InstanceObject()); |
|
212
|
|
|
$node->name = 'childNodes'; |
|
213
|
|
|
$node->classname = 'DOMNodeList'; |
|
214
|
|
|
$c->contents = array($node); |
|
215
|
|
|
} else { |
|
216
|
|
|
foreach ($childNodes->contents as $index => $node) { |
|
217
|
|
|
// Shortcircuit text nodes to plain strings |
|
218
|
|
|
if ($node->classname === 'DOMText' || $node->classname === 'DOMComment') { |
|
219
|
|
|
$node = self::textualNodeToString($node); |
|
220
|
|
|
|
|
221
|
|
|
// And remove them if they're empty |
|
222
|
|
|
if (ctype_space($node->value->contents) || $node->value->contents === '') { |
|
223
|
|
|
continue; |
|
224
|
|
|
} |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
$c->contents[] = $node; |
|
228
|
|
|
} |
|
229
|
|
|
} |
|
230
|
|
|
|
|
231
|
|
|
$o->addRepresentation($c, 0); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
if (isset($c) && count($c->contents)) { |
|
235
|
|
|
$o->size = count($c->contents); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
if (!$o->size) { |
|
|
|
|
|
|
239
|
|
|
$o->size = null; |
|
240
|
|
|
} |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
protected function parseProperty(InstanceObject $o, $prop, &$var) |
|
|
|
|
|
|
244
|
|
|
{ |
|
245
|
|
|
// Duplicating (And slightly optimizing) the Parser::parseObject() code here |
|
246
|
|
|
$base_obj = new BasicObject(); |
|
|
|
|
|
|
247
|
|
|
$base_obj->depth = $o->depth + 1; |
|
|
|
|
|
|
248
|
|
|
$base_obj->owner_class = $o->classname; |
|
|
|
|
|
|
249
|
|
|
$base_obj->name = $prop; |
|
|
|
|
|
|
250
|
|
|
$base_obj->operator = BasicObject::OPERATOR_OBJECT; |
|
|
|
|
|
|
251
|
|
|
$base_obj->access = BasicObject::ACCESS_PUBLIC; |
|
|
|
|
|
|
252
|
|
|
|
|
253
|
|
|
if ($o->access_path !== null) { |
|
254
|
|
|
$base_obj->access_path = $o->access_path; |
|
|
|
|
|
|
255
|
|
|
|
|
256
|
|
|
if (preg_match('/^[A-Za-z0-9_]+$/', $base_obj->name)) { |
|
|
|
|
|
|
257
|
|
|
$base_obj->access_path .= '->'.$base_obj->name; |
|
|
|
|
|
|
258
|
|
|
} else { |
|
259
|
|
|
$base_obj->access_path .= '->{'.var_export($base_obj->name, true).'}'; |
|
|
|
|
|
|
260
|
|
|
} |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
if (!isset($var->$prop)) { |
|
264
|
|
|
$base_obj->type = 'null'; |
|
|
|
|
|
|
265
|
|
|
} elseif (isset(self::$blacklist[$prop])) { |
|
266
|
|
|
$base_obj = $base_obj->transplant(new InstanceObject()); |
|
|
|
|
|
|
267
|
|
|
$base_obj->hints[] = 'blacklist'; |
|
|
|
|
|
|
268
|
|
|
$base_obj->classname = self::$blacklist[$prop]; |
|
|
|
|
|
|
269
|
|
|
} elseif ($prop === 'attributes') { |
|
270
|
|
|
$base_obj = $this->parser->parseDeep($var->$prop, $base_obj); |
|
|
|
|
|
|
271
|
|
|
} else { |
|
272
|
|
|
$base_obj = $this->parser->parse($var->$prop, $base_obj); |
|
|
|
|
|
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
return $base_obj; |
|
|
|
|
|
|
276
|
|
|
} |
|
277
|
|
|
|
|
278
|
|
|
protected static function textualNodeToString(InstanceObject $o) |
|
|
|
|
|
|
279
|
|
|
{ |
|
280
|
|
|
if (empty($o->value) || empty($o->value->contents) || empty($o->classname)) { |
|
281
|
|
|
return; |
|
282
|
|
|
} |
|
283
|
|
|
|
|
284
|
|
|
if (!in_array($o->classname, array('DOMText', 'DOMAttr', 'DOMComment'))) { |
|
285
|
|
|
return; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
foreach ($o->value->contents as $property) { |
|
289
|
|
|
if ($property->name === 'nodeValue') { |
|
290
|
|
|
$ret = clone $property; |
|
291
|
|
|
$ret->name = $o->name; |
|
292
|
|
|
|
|
293
|
|
|
return $ret; |
|
294
|
|
|
} |
|
295
|
|
|
} |
|
296
|
|
|
} |
|
297
|
|
|
} |
|
298
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.