| 1: | <?php | 
| 2: |  | 
| 3: |  | 
| 4: |  | 
| 5: |  | 
| 6: |  | 
| 7: |  | 
| 8: |  | 
| 9: |  | 
| 10: |  | 
| 11: |  | 
| 12: |  | 
| 13: |  | 
| 14: |  | 
| 15: |  | 
| 16: |  | 
| 17: |  | 
| 18: |  | 
| 19: |  | 
| 20: |  | 
| 21: |  | 
| 22: |  | 
| 23: |  | 
| 24: |  | 
| 25: |  | 
| 26: | namespace Kint\Parser; | 
| 27: |  | 
| 28: | use DOMNamedNodeMap; | 
| 29: | use DOMNode; | 
| 30: | use DOMNodeList; | 
| 31: | use Kint\Object\BasicObject; | 
| 32: | use Kint\Object\InstanceObject; | 
| 33: | use Kint\Object\Representation\Representation; | 
| 34: |  | 
| 35: |  | 
| 36: |  | 
| 37: |  | 
| 38: |  | 
| 39: |  | 
| 40: | class DOMDocumentPlugin extends Plugin | 
| 41: | { | 
| 42: |  | 
| 43: |  | 
| 44: |  | 
| 45: |  | 
| 46: |  | 
| 47: |  | 
| 48: |  | 
| 49: |  | 
| 50: |  | 
| 51: |  | 
| 52: |  | 
| 53: |  | 
| 54: |  | 
| 55: |  | 
| 56: |  | 
| 57: |  | 
| 58: |  | 
| 59: |  | 
| 60: |  | 
| 61: |  | 
| 62: |  | 
| 63: |  | 
| 64: |  | 
| 65: |  | 
| 66: | public static $blacklist = array( | 
| 67: | 'parentNode' => 'DOMNode', | 
| 68: | 'firstChild' => 'DOMNode', | 
| 69: | 'lastChild' => 'DOMNode', | 
| 70: | 'previousSibling' => 'DOMNode', | 
| 71: | 'nextSibling' => 'DOMNode', | 
| 72: | 'ownerDocument' => 'DOMDocument', | 
| 73: | ); | 
| 74: |  | 
| 75: |  | 
| 76: |  | 
| 77: |  | 
| 78: |  | 
| 79: |  | 
| 80: | public static $verbose = false; | 
| 81: |  | 
| 82: | public function getTypes() | 
| 83: | { | 
| 84: | return array('object'); | 
| 85: | } | 
| 86: |  | 
| 87: | public function getTriggers() | 
| 88: | { | 
| 89: | return Parser::TRIGGER_SUCCESS; | 
| 90: | } | 
| 91: |  | 
| 92: | public function parse(&$var, BasicObject &$o, $trigger) | 
| 93: | { | 
| 94: | if (!$o instanceof InstanceObject) { | 
| 95: | return; | 
| 96: | } | 
| 97: |  | 
| 98: | if ($var instanceof DOMNamedNodeMap || $var instanceof DOMNodeList) { | 
| 99: | return $this->parseList($var, $o, $trigger); | 
| 100: | } | 
| 101: |  | 
| 102: | if ($var instanceof DOMNode) { | 
| 103: | return $this->parseNode($var, $o); | 
| 104: | } | 
| 105: | } | 
| 106: |  | 
| 107: | protected function parseList(&$var, InstanceObject &$o, $trigger) | 
| 108: | { | 
| 109: |  | 
| 110: |  | 
| 111: |  | 
| 112: |  | 
| 113: | if ($trigger & Parser::TRIGGER_RECURSION) { | 
| 114: | return; | 
| 115: | } | 
| 116: |  | 
| 117: | $o->size = $var->length; | 
| 118: | if (0 === $o->size) { | 
| 119: | $o->replaceRepresentation(new Representation('Iterator')); | 
| 120: | $o->size = null; | 
| 121: |  | 
| 122: | return; | 
| 123: | } | 
| 124: |  | 
| 125: |  | 
| 126: |  | 
| 127: | if ($this->parser->getDepthLimit() && $o->depth + 1 >= $this->parser->getDepthLimit()) { | 
| 128: | $b = new BasicObject(); | 
| 129: | $b->name = $o->classname.' Iterator Contents'; | 
| 130: | $b->access_path = 'iterator_to_array('.$o->access_path.')'; | 
| 131: | $b->depth = $o->depth + 1; | 
| 132: | $b->hints[] = 'depth_limit'; | 
| 133: |  | 
| 134: | $r = new Representation('Iterator'); | 
| 135: | $r->contents = array($b); | 
| 136: | $o->replaceRepresentation($r, 0); | 
| 137: |  | 
| 138: | return; | 
| 139: | } | 
| 140: |  | 
| 141: | $data = \iterator_to_array($var); | 
| 142: |  | 
| 143: | $r = new Representation('Iterator'); | 
| 144: | $o->replaceRepresentation($r, 0); | 
| 145: |  | 
| 146: | foreach ($data as $key => $item) { | 
| 147: | $base_obj = new BasicObject(); | 
| 148: | $base_obj->depth = $o->depth + 1; | 
| 149: | $base_obj->name = $item->nodeName; | 
| 150: |  | 
| 151: | if ($o->access_path) { | 
| 152: | if ($var instanceof DOMNamedNodeMap) { | 
| 153: | $base_obj->access_path = $o->access_path.'->getNamedItem('.\var_export($key, true).')'; | 
| 154: | } elseif ($var instanceof DOMNodeList) { | 
| 155: | $base_obj->access_path = $o->access_path.'->item('.\var_export($key, true).')'; | 
| 156: | } else { | 
| 157: | $base_obj->access_path = 'iterator_to_array('.$o->access_path.')'; | 
| 158: | } | 
| 159: | } | 
| 160: |  | 
| 161: | $r->contents[] = $this->parser->parse($item, $base_obj); | 
| 162: | } | 
| 163: | } | 
| 164: |  | 
| 165: | protected function parseNode(&$var, InstanceObject &$o) | 
| 166: | { | 
| 167: |  | 
| 168: |  | 
| 169: |  | 
| 170: | $known_properties = array( | 
| 171: | 'nodeValue', | 
| 172: | 'childNodes', | 
| 173: | 'attributes', | 
| 174: | ); | 
| 175: |  | 
| 176: | if (self::$verbose) { | 
| 177: | $known_properties = array( | 
| 178: | 'nodeName', | 
| 179: | 'nodeValue', | 
| 180: | 'nodeType', | 
| 181: | 'parentNode', | 
| 182: | 'childNodes', | 
| 183: | 'firstChild', | 
| 184: | 'lastChild', | 
| 185: | 'previousSibling', | 
| 186: | 'nextSibling', | 
| 187: | 'attributes', | 
| 188: | 'ownerDocument', | 
| 189: | 'namespaceURI', | 
| 190: | 'prefix', | 
| 191: | 'localName', | 
| 192: | 'baseURI', | 
| 193: | 'textContent', | 
| 194: | ); | 
| 195: | } | 
| 196: |  | 
| 197: | $childNodes = array(); | 
| 198: | $attributes = array(); | 
| 199: |  | 
| 200: | $rep = $o->value; | 
| 201: |  | 
| 202: | foreach ($known_properties as $prop) { | 
| 203: | $prop_obj = $this->parseProperty($o, $prop, $var); | 
| 204: | $rep->contents[] = $prop_obj; | 
| 205: |  | 
| 206: | if ('childNodes' === $prop) { | 
| 207: | $childNodes = $prop_obj->getRepresentation('iterator'); | 
| 208: | } elseif ('attributes' === $prop) { | 
| 209: | $attributes = $prop_obj->getRepresentation('iterator'); | 
| 210: | } | 
| 211: | } | 
| 212: |  | 
| 213: | if (!self::$verbose) { | 
| 214: | $o->removeRepresentation('methods'); | 
| 215: | $o->removeRepresentation('properties'); | 
| 216: | } | 
| 217: |  | 
| 218: |  | 
| 219: |  | 
| 220: | if (\in_array($o->classname, array('DOMAttr', 'DOMText', 'DOMComment'), true)) { | 
| 221: | return; | 
| 222: | } | 
| 223: |  | 
| 224: |  | 
| 225: | if ($attributes) { | 
| 226: | $a = new Representation('Attributes'); | 
| 227: | foreach ($attributes->contents as $attribute) { | 
| 228: | $a->contents[] = self::textualNodeToString($attribute); | 
| 229: | } | 
| 230: | $o->addRepresentation($a, 0); | 
| 231: | } | 
| 232: |  | 
| 233: |  | 
| 234: | if ($childNodes) { | 
| 235: | $c = new Representation('Children'); | 
| 236: |  | 
| 237: | if (1 === \count($childNodes->contents) && ($node = \reset($childNodes->contents)) && \in_array('depth_limit', $node->hints, true)) { | 
| 238: | $n = new InstanceObject(); | 
| 239: | $n->transplant($node); | 
| 240: | $n->name = 'childNodes'; | 
| 241: | $n->classname = 'DOMNodeList'; | 
| 242: | $c->contents = array($n); | 
| 243: | } else { | 
| 244: | foreach ($childNodes->contents as $index => $node) { | 
| 245: |  | 
| 246: | if ('DOMText' === $node->classname || 'DOMComment' === $node->classname) { | 
| 247: | $node = self::textualNodeToString($node); | 
| 248: |  | 
| 249: |  | 
| 250: | if (\ctype_space($node->value->contents) || '' === $node->value->contents) { | 
| 251: | continue; | 
| 252: | } | 
| 253: | } | 
| 254: |  | 
| 255: | $c->contents[] = $node; | 
| 256: | } | 
| 257: | } | 
| 258: |  | 
| 259: | $o->addRepresentation($c, 0); | 
| 260: | } | 
| 261: |  | 
| 262: | if (isset($c) && \count($c->contents)) { | 
| 263: | $o->size = \count($c->contents); | 
| 264: | } | 
| 265: |  | 
| 266: | if (!$o->size) { | 
| 267: | $o->size = null; | 
| 268: | } | 
| 269: | } | 
| 270: |  | 
| 271: | protected function parseProperty(InstanceObject $o, $prop, &$var) | 
| 272: | { | 
| 273: |  | 
| 274: | $base_obj = new BasicObject(); | 
| 275: | $base_obj->depth = $o->depth + 1; | 
| 276: | $base_obj->owner_class = $o->classname; | 
| 277: | $base_obj->name = $prop; | 
| 278: | $base_obj->operator = BasicObject::OPERATOR_OBJECT; | 
| 279: | $base_obj->access = BasicObject::ACCESS_PUBLIC; | 
| 280: |  | 
| 281: | if (null !== $o->access_path) { | 
| 282: | $base_obj->access_path = $o->access_path; | 
| 283: |  | 
| 284: | if (\preg_match('/^[A-Za-z0-9_]+$/', $base_obj->name)) { | 
| 285: | $base_obj->access_path .= '->'.$base_obj->name; | 
| 286: | } else { | 
| 287: | $base_obj->access_path .= '->{'.\var_export($base_obj->name, true).'}'; | 
| 288: | } | 
| 289: | } | 
| 290: |  | 
| 291: | if (!isset($var->{$prop})) { | 
| 292: | $base_obj->type = 'null'; | 
| 293: | } elseif (isset(self::$blacklist[$prop])) { | 
| 294: | $b = new InstanceObject(); | 
| 295: | $b->transplant($base_obj); | 
| 296: | $base_obj = $b; | 
| 297: |  | 
| 298: | $base_obj->hints[] = 'blacklist'; | 
| 299: | $base_obj->classname = self::$blacklist[$prop]; | 
| 300: | } elseif ('attributes' === $prop) { | 
| 301: | $base_obj = $this->parser->parseDeep($var->{$prop}, $base_obj); | 
| 302: | } else { | 
| 303: | $base_obj = $this->parser->parse($var->{$prop}, $base_obj); | 
| 304: | } | 
| 305: |  | 
| 306: | return $base_obj; | 
| 307: | } | 
| 308: |  | 
| 309: | protected static function textualNodeToString(InstanceObject $o) | 
| 310: | { | 
| 311: | if (empty($o->value) || empty($o->value->contents) || empty($o->classname)) { | 
| 312: | return; | 
| 313: | } | 
| 314: |  | 
| 315: | if (!\in_array($o->classname, array('DOMText', 'DOMAttr', 'DOMComment'), true)) { | 
| 316: | return; | 
| 317: | } | 
| 318: |  | 
| 319: | foreach ($o->value->contents as $property) { | 
| 320: | if ('nodeValue' === $property->name) { | 
| 321: | $ret = clone $property; | 
| 322: | $ret->name = $o->name; | 
| 323: |  | 
| 324: | return $ret; | 
| 325: | } | 
| 326: | } | 
| 327: | } | 
| 328: | } | 
| 329: |  |