1
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
<?php /** * Holds a bundle definition in a DOM document */ class Loco_config_XMLModel extends Loco_config_Model {
/** * @var DOMDocument */ private $dom; /** * @var DOMXpath */ private $xpath;
/** * {@inheritdoc} */ public function createDom(){ $dom = new DOMDocument('1.0','utf-8'); $dom->formatOutput = true; $dom->registerNodeClass('DOMElement','LocoConfig_DOMElement'); $this->xpath = new DOMXPath($dom); $this->dom = $dom; }
/** * @return DOMDocument */ public function getDom(){ return $this->dom; }
/** * {@inheritdoc} * @return LocoConfigNodeListIterator */ public function query( $query, $context = null ){ $list = $this->xpath->query( $query, $context ); return new LocoConfigNodeListIterator( $list ); }
/** * @return void */ public function loadXml( $source ){ if( ! $source ){ throw new Loco_error_XmlParseException( __('XML supplied is empty','loco-translate') ); } $dom = $this->getDom(); // parse with silent errors, clearing after $used_errors = libxml_use_internal_errors(true);
$dom->loadXML( $source, LIBXML_NONET ); unset( $source ); // fetch errors and ensure clean for next run. $errors = libxml_get_errors(); $used_errors || libxml_use_internal_errors(false); libxml_clear_errors();
// Throw exception if error level exceeds current tolerance if( $errors ){ foreach( $errors as $error ){ if( $error->level >= LIBXML_ERR_FATAL ){ throw new Loco_error_XmlParseException( trim($error->message) ); // ->setContext( $error->line, $error->column, $source ); } } } // Not currently validating against a DTD, but will preempt generic model loading errors $root = $dom->documentElement; if( ! $root instanceof DOMNode ){ throw new Loco_error_XmlParseException('Expected <bundle> document element'); } if( 'bundle' !== strtolower($root->nodeName) ){ throw new Loco_error_XmlParseException('Expected <bundle> document element, got <'.$root->nodeName.'>'); } $this->xpath = new DOMXPath($dom); }
/** * {@inheritdoc} * Overridden to avoid empty text nodes in XML files, preferring <file>.</file> to <file /> */ protected function setFileElementPath( $node, $path ){ if( ! $path && '0' !== $path ){ $path = '.'; } return parent::setFileElementPath( $node, $path ); }
}
/** * @internal */ class LocoConfig_DOMElement extends DOMElement implements IteratorAggregate, Countable { #[ReturnTypeWillChange] public function getIterator(){ return new LocoConfigNodeListIterator( $this->childNodes ); } #[ReturnTypeWillChange] public function count(){ return $this->childNodes->length; } }
/** * @internal * Cos NodeList doesn't iterate */ class LocoConfigNodeListIterator implements Iterator, Countable, ArrayAccess { /** * @var DOMNodeList */ private $nodes;
/** * @var int */ private $i;
/** * @var int */ private $n; public function __construct( DOMNodeList $nodes ){ $this->nodes = $nodes; $this->n = $nodes->length; }
#[ReturnTypeWillChange] public function count(){ return $this->n; }
#[ReturnTypeWillChange] public function rewind(){ $this->i = -1; $this->next(); }
#[ReturnTypeWillChange] public function key(){ return $this->i; }
#[ReturnTypeWillChange] public function current(){ return $this->nodes->item( $this->i ); }
#[ReturnTypeWillChange] public function valid(){ return is_int($this->i); }
#[ReturnTypeWillChange] public function next(){ while( true ){ $this->i++; if( $child = $this->nodes->item($this->i) ){ break; } $this->i = null; break; } }
#[ReturnTypeWillChange] public function offsetExists( $i ){ return $i >= 0 && $i < $this->n; }
#[ReturnTypeWillChange] public function offsetGet( $i ){ return $this->nodes->item($i); }
/** * @codeCoverageIgnore */ #[ReturnTypeWillChange] public function offsetSet( $i, $value ){ throw new Exception('Read only'); }
/** * @codeCoverageIgnore */ #[ReturnTypeWillChange] public function offsetUnset( $i ){ throw new Exception('Read only'); } }
|