-
Notifications
You must be signed in to change notification settings - Fork 0
/
XmlAdapter.php
436 lines (393 loc) · 10.8 KB
/
XmlAdapter.php
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
<?php
/**
* Copyright (c) 2015 Petr Bilek (http://ww.sallyx.org)
*/
namespace Sallyx\Nette\DI\Config\Adapters;
use Nette;
use Nette\DI\Config\Helpers;
use Nette\DI\Statement;
use Nette\DI\Config\Adapters\NeonAdapter;
/**
* Reading and generating XML files.
*/
class XmlAdapter implements Nette\DI\Config\IAdapter
{
use \Nette\SmartObject;
const NS = "http://www.sallyx.org/xmlns/nette/config/1.0";
/**
* Reads configuration from XML file.
* @param string file name
* @return array
*/
public function load($file)
{
$options = LIBXML_NOBLANKS | LIBXML_NOCDATA | LIBXML_NOENT | LIBXML_NSCLEAN; // | LIBXML_PEDANTIC ?
$parserClass = "\\Sallyx\\Nette\\DI\\Config\\Adapters\\XMLElementParser";
$document = simplexml_load_file($file, $parserClass, $options, self::NS);
return $document->parse();
}
/**
* Generates configuration in XML format.
* @return string
*/
public function dump(array $data)
{
$writer = new XmlElementWriter('<config/>', LIBXML_NOEMPTYTAG, false, self::NS, "nc");
$writer->addAttribute('xmlns:xmlns:nc', self::NS);
$writer->addAttribute('xmlns', self::NS);
$writer->addConfig($data);
return $writer->asXML();
}
}
/**
* This class helps to parse XML config to PHP array
* @internal
*/
class XMLElementParser extends \SimpleXMLElement
{
/** Names of attributes */
const ATTR_ARRAY = 'array',
ATTR_BOOL = 'bool',
ATTR_NUMBER = 'number',
ATTR_DELIMITER = 'delimiter',
ATTR_EXTENDS = 'extends',
ATTR_MERGING = 'merging',
ATTR_NULL = 'null',
ATTR_STATEMENT = 'statement',
ATTR_SPACE = 'space';
/** Values of attributes */
const ATTR_MERGING_REPLACE = 'replace',
ATTR_ARRAY_ASSOCIATIVE = 'associative',
ATTR_ARRAY_NUMERIC = 'numeric',
ATTR_ARRAY_STRING = 'string',
ATTR_NULL_VALUE = 'null',
ATTR_SPACE_PRESERVE = 'preserve',
DEFAULT_DELIMITER = ',';
/**
* Parse DI config
*
* @return array|string|NULL|Statement
*/
public function parse()
{
$statement = $this->getAttribute(self::ATTR_STATEMENT);
if (!empty($statement)) {
if ($statement === self::ATTR_STATEMENT) {
return $this->parseStatements($statement);
}
$res = $this->getNeonAdapter()->load('data://text/plain,- ' . $statement);
return array_shift($res);
}
$res = $this->parseChildren();
$extends = $this->getAttribute(self::ATTR_EXTENDS);
$merging = $this->getattribute(self::ATTR_MERGING);
if ($merging === self::ATTR_MERGING_REPLACE && !is_array($res)) {
$name = $this->getName();
throw new Nette\InvalidStateException("Replacing operator is available only for arrays, element '$name' is not array");
}
if (!empty($extends)) {
$res[Helpers::EXTENDS_KEY] = $extends;
} elseif ($merging === self::ATTR_MERGING_REPLACE) {
$res[Helpers::EXTENDS_KEY] = Helpers::OVERWRITE;
}
return $res;
}
/**
* Returns element value
*
* If element has a bool attribute, returns bool value of this attribute.
* If element has a null attribute, returns NULL;
* Othervise returns string value of this element.
*
* @return string|bool|NULL
*/
public function getValue()
{
if ($this->getAttribute(self::ATTR_NULL)) {
return NULL;
}
$bool = $this->getAttribute(self::ATTR_BOOL);
if ($bool) {
switch (strtolower($bool)) {
case 'yes':
// break intentionally omitted
case 'true':
// break intentionally omitted
case 'on':
// break intentionally omitted
case '1':
return TRUE;
default:
return FALSE;
}
}
$number = $this->getAttribute(self::ATTR_NUMBER);
if (!is_null($number)) {
return $number + 0;
}
return (string) $this;
}
/**
* Returns attribute of name $name if exists, or $default value
*
* @param string $name
* @param string $default
* @return string|NULL
*/
public function getAttribute($name, $default = NULL)
{
$attrs = $this->attributes();
$attr = $attrs[$name];
if (is_null($attr)) {
return $default;
}
return (string) $attr;
}
/**
* @staticvar type $neonAdapter
* @return type
*/
private function getNeonAdapter()
{
static $neonAdapter;
$neonAdapter = $neonAdapter ? : new NeonAdapter();
return $neonAdapter;
}
/**
* Parse element with attribute statement (list of statements)
*
* @param string $statements
* @return Statement
*/
private function parseStatements($statements)
{
$res = null;
foreach ($this->children() as $statement) {
list($entity, $arguments) = $this->parseStatement($statement);
if (!$res) {
$res = new Statement($entity, $arguments);
} else {
$res = new Statement(array($res, $entity), $arguments);
}
}
return $res;
}
/**
* Parse one statement from statement list
* @param \SimpleXmlElement $child
* @return array [0 => $entity, 1 => $attributes]
*/
private function parseStatement($child)
{
$entityElement = $child->ent;
if (empty($entityElement)) {
throw new \Nette\InvalidStateException("Expected <ent> element in statement " . $this->asXML());
}
$entity = $child->ent->getValue();
if (!is_string($entity) or $entity === '') {
throw new \Nette\InvalidStateException("Element <ent> must have a non-empty string value.");
}
$argsElement = $child->args;
if (empty($argsElement)) {
return array($entity, NULL);
}
$arrayAttr = $argsElement[0]->getAttribute(self::ATTR_ARRAY);
if (is_null($arrayAttr)) {
if($argsElement[0]->count()) {
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_NUMERIC);
} else {
$argsElement[0]->addAttribute(self::ATTR_ARRAY, self::ATTR_ARRAY_STRING);
}
}
$attributes = $argsElement[0]->parseChildren();
if (!is_array($attributes)) {
$attributes = array($attributes);
}
return array($entity, $attributes);
}
/**
* Parse children elements or value of this element
*
* @return array|string|NULL|Nette\DI\Statement
*/
private function parseChildren()
{
$arrayType = $this->getAttribute(self::ATTR_ARRAY);
$space = (string) $this->getAttribute(self::ATTR_SPACE);
if ($space !== self::ATTR_SPACE_PRESERVE and ! empty($space)) {
throw new Nette\InvalidStateException("Attribute " . self::ATTR_SPACE . " has an unknown value '$space'");
}
if ($arrayType === self::ATTR_ARRAY_STRING) {
$res = $this->parseStringArray();
$this->trim($res, $space);
return $res;
}
if ($arrayType === NULL && $this->count()) {
$arrayType = self::ATTR_ARRAY_ASSOCIATIVE;
}
if ($arrayType === NULL) {
$res = $this->getValue();
$this->trim($res, $space);
return $res;
}
$res = array();
switch ($arrayType) {
default:
case self::ATTR_ARRAY_ASSOCIATIVE:
foreach ($this->children() as $key => $child) {
if (isset($res[$key])) {
throw new Nette\InvalidStateException("Duplicated key '$key'.");
}
$res[$key] = $child->parse();
}
break;
case self::ATTR_ARRAY_NUMERIC:
foreach ($this->children() as $key => $child) {
$res[] = $child->parse();
}
break;
}
return $res;
}
/**
* @param mixed $value
* @param string $space
*/
private function trim(&$value, $space)
{
if ($space === self::ATTR_SPACE_PRESERVE) {
return;
}
if (is_array($value)) {
array_walk(
$value, function(& $val) {
if (is_string($val)) {
$val = trim($val);
}
});
return;
}
if (is_string($value)) {
$value = trim($value);
}
}
/**
* Parse value of this element with attribute array="string"
*
* @return array
*/
private function parseStringArray()
{
if ($this->count() > 0) {
throw new Nette\InvalidStateException('Element with attribute array="string" can\'t have children: ' . $this->asXML());
}
$value = $this->getValue();
if (is_null($value)) {
return array();
}
$delimiter = $this->getAttribute(self::ATTR_DELIMITER, self::DEFAULT_DELIMITER);
return explode($delimiter[0], $value);
}
}
/**
* This class helps to save array into XML configuration
* @internal
*/
class XMLElementWriter extends \SimpleXMLElement
{
/** Entity names */
const ENT_ITEM = 'item',
ENT_STATEMENT = 's',
ENT_STATEMENT_ENTITY = 'ent',
ENT_STATEMENT_ARGUMENTS = 'args';
/**
* Construct XML DOM from config array
* @param array $config
*/
public function addConfig(array $config, $element = NULL)
{
if ($element === NULL) {
$element = $this;
}
if (isset($config[Helpers::EXTENDS_KEY])) {
$section = $config[Helpers::EXTENDS_KEY];
if ($section === Helpers::OVERWRITE) {
$element->addAttribute(XMLElementParser::ATTR_MERGING, XMLElementParser::ATTR_MERGING_REPLACE);
} else {
$element->addAttribute(XMLElementParser::ATTR_EXTENDS, $section);
}
unset($config[Helpers::EXTENDS_KEY]);
}
$isNumeric = array_keys($config) === range(0, count($config) - 1);
if ($isNumeric) {
$element->addAttribute(XMLElementParser::ATTR_ARRAY, XMLElementParser::ATTR_ARRAY_NUMERIC);
}
foreach ($config as $name => $value) {
$name = $isNumeric ? self::ENT_ITEM : $name;
$this->addElement($element, $name, $value);
}
}
/**
* @param \SimpleXmlElement $element
* @param string $name
* @param mixed $value
* @return void
*/
private function addElement(\SimpleXMLElement $element, $name, $value)
{
if (is_string($value)) {
$element->addChild($name, $value);
return;
}
if ($value instanceof Statement) {
$this->addStatementList($element, $name, $value);
return;
}
$child = $element->addChild($name);
if ($value === NULL) {
$child->addAttribute(XMLElementParser::ATTR_NULL, XMLElementParser::ATTR_NULL_VALUE);
} elseif (is_bool($value)) {
$child->addAttribute(XMLElementParser::ATTR_BOOL, $value ? '1' : '0');
} elseif (is_numeric($value)) {
$child->addAttribute(XMLElementParser::ATTR_NUMBER, $value);
} elseif (is_array($value)) {
$this->addConfig($value, $child);
} else {
throw new \Nette\InvalidStateException("Unsupported type " . gettype($value));
}
}
/**
* @param \SimpleXMLElement $element
* @param string $name
* @param \Statement $value
* @return void
*/
private function addStatementList(\SimpleXMLElement $element, $name, Statement $value)
{
$child = $element->addChild($name);
$child->addAttribute(XMLElementParser::ATTR_STATEMENT, XMLElementParser::ATTR_STATEMENT);
$this->addStatement($child, $value);
}
/**
*
* @param \SimpleXMLElement $list
* @param Statement $value
* @return void
* @throws Nette\InvalidStateException
*/
private function addStatement(\SimpleXMLElement $list, Statement $value)
{
if (!is_array($value->entity)) {
$child = $list->addChild(self::ENT_STATEMENT);
$child->addChild(self::ENT_STATEMENT_ENTITY, $value->entity);
$args = $child->addChild(self::ENT_STATEMENT_ARGUMENTS);
$this->addConfig($value->arguments, $args);
return;
}
if (!($value->entity[0] instanceof Statement)) {
throw new Nette\InvalidStateException("Unsupported statement state");
}
$this->addStatement($list, $value->entity[0]);
$this->addStatement($list, new Statement($value->entity[1], $value->arguments));
}
}