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 DOMDocument; |
29: | use Exception; |
30: | use Kint\Object\BasicObject; |
31: | use Kint\Object\Representation\Representation; |
32: | |
33: | class XmlPlugin extends Plugin |
34: | { |
35: | |
36: | |
37: | |
38: | |
39: | |
40: | |
41: | |
42: | |
43: | |
44: | public static $parse_method = 'SimpleXML'; |
45: | |
46: | public function getTypes() |
47: | { |
48: | return array('string'); |
49: | } |
50: | |
51: | public function getTriggers() |
52: | { |
53: | return Parser::TRIGGER_SUCCESS; |
54: | } |
55: | |
56: | public function parse(&$var, BasicObject &$o, $trigger) |
57: | { |
58: | if ('<?xml' !== \substr($var, 0, 5)) { |
59: | return; |
60: | } |
61: | |
62: | if (!\method_exists(\get_class($this), 'xmlTo'.self::$parse_method)) { |
63: | return; |
64: | } |
65: | |
66: | $xml = \call_user_func(array(\get_class($this), 'xmlTo'.self::$parse_method), $var, $o->access_path); |
67: | |
68: | if (empty($xml)) { |
69: | return; |
70: | } |
71: | |
72: | list($xml, $access_path, $name) = $xml; |
73: | |
74: | $base_obj = new BasicObject(); |
75: | $base_obj->depth = $o->depth + 1; |
76: | $base_obj->name = $name; |
77: | $base_obj->access_path = $access_path; |
78: | |
79: | $r = new Representation('XML'); |
80: | $r->contents = $this->parser->parse($xml, $base_obj); |
81: | |
82: | $o->addRepresentation($r, 0); |
83: | } |
84: | |
85: | protected static function xmlToSimpleXML($var, $parent_path) |
86: | { |
87: | try { |
88: | $errors = \libxml_use_internal_errors(true); |
89: | $xml = \simplexml_load_string($var); |
90: | \libxml_use_internal_errors($errors); |
91: | } catch (Exception $e) { |
92: | if (isset($errors)) { |
93: | \libxml_use_internal_errors($errors); |
94: | } |
95: | |
96: | return; |
97: | } |
98: | |
99: | if (!$xml) { |
100: | return; |
101: | } |
102: | |
103: | if (null === $parent_path) { |
104: | $access_path = null; |
105: | } else { |
106: | $access_path = 'simplexml_load_string('.$parent_path.')'; |
107: | } |
108: | |
109: | $name = $xml->getName(); |
110: | |
111: | return array($xml, $access_path, $name); |
112: | } |
113: | |
114: | |
115: | |
116: | |
117: | |
118: | |
119: | |
120: | |
121: | |
122: | |
123: | |
124: | |
125: | |
126: | |
127: | |
128: | |
129: | protected static function xmlToDOMDocument($var, $parent_path) |
130: | { |
131: | |
132: | if (!self::xmlToSimpleXML($var, $parent_path)) { |
133: | return null; |
134: | } |
135: | |
136: | $xml = new DOMDocument(); |
137: | $xml->loadXML($var); |
138: | $xml = $xml->firstChild; |
139: | |
140: | if (null === $parent_path) { |
141: | $access_path = null; |
142: | } else { |
143: | $access_path = '@\\DOMDocument::loadXML('.$parent_path.')->firstChild'; |
144: | } |
145: | |
146: | $name = $xml->nodeName; |
147: | |
148: | return array($xml, $access_path, $name); |
149: | } |
150: | } |
151: | |