|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace N98\Magento\Command\Developer\Console\Util; |
|
4
|
|
|
|
|
5
|
|
|
class Xml |
|
6
|
|
|
{ |
|
7
|
|
|
/** |
|
8
|
|
|
* @param string $xmlString |
|
9
|
|
|
* |
|
10
|
|
|
* @return string |
|
11
|
|
|
*/ |
|
12
|
|
|
public static function formatString($xmlString) |
|
13
|
|
|
{ |
|
14
|
|
|
$dom = new \DOMDocument('1.0'); |
|
15
|
|
|
$dom->preserveWhiteSpace = false; |
|
16
|
|
|
$dom->formatOutput = true; |
|
17
|
|
|
|
|
18
|
|
|
$dom->loadXML($xmlString); |
|
19
|
|
|
|
|
20
|
|
|
return preg_replace('%(^\s*)%m', '$1$1', $dom->saveXml()); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @param \SimpleXMLElement $xml |
|
25
|
|
|
* @param string $path |
|
26
|
|
|
* |
|
27
|
|
|
* @return \SimpleXMLElement|\SimpleXMLElement[] |
|
28
|
|
|
* |
|
29
|
|
|
* @link https://github.com/astorm/pestle |
|
30
|
|
|
* @copyright Pulse Storm LLC, Alan Storm |
|
31
|
|
|
* |
|
32
|
|
|
* @throws \Exception |
|
33
|
|
|
*/ |
|
34
|
|
|
public static function addSimpleXmlNodesByXPath(\SimpleXMLElement $xml, $path) |
|
35
|
|
|
{ |
|
36
|
|
|
$path = trim($path, '/'); |
|
37
|
|
|
$node = $xml; |
|
38
|
|
|
|
|
39
|
|
|
foreach (explode('/', $path) as $part) { |
|
40
|
|
|
$parts = explode('[', $part); |
|
41
|
|
|
$nodeName = array_shift($parts); |
|
42
|
|
|
$isNewNode = true; |
|
43
|
|
|
|
|
44
|
|
|
if (isset($node->{$nodeName})) { |
|
45
|
|
|
$isNewNode = false; |
|
46
|
|
|
$node = $node->{$nodeName}; |
|
47
|
|
|
} else { |
|
48
|
|
|
$node = $node->addChild($nodeName); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$attributeString = trim(array_pop($parts), ']'); |
|
52
|
|
|
|
|
53
|
|
|
if (!$attributeString) { |
|
54
|
|
|
continue; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$pairs = explode(',', $attributeString); |
|
58
|
|
|
|
|
59
|
|
|
foreach ($pairs as $pair) { |
|
60
|
|
|
if (!$isNewNode) { |
|
61
|
|
|
continue; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
list($key, $value) = explode('=', $pair); |
|
65
|
|
|
|
|
66
|
|
|
if (strpos($key, '@') !== 0) { |
|
67
|
|
|
throw new \Exception("Invalid Attribute Key"); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
$key = trim($key, '@'); |
|
71
|
|
|
if (strpos($key, ':') !== false) { |
|
72
|
|
|
list($namespacePrefix, $rest) = explode(':', $key); |
|
|
|
|
|
|
73
|
|
|
$namespace = self::getXmlNamespaceFromPrefix($xml, $namespacePrefix); |
|
74
|
|
|
$node->addAttribute($key, $value, $namespace); |
|
75
|
|
|
} else { |
|
76
|
|
|
$node->addAttribute($key, $value); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
return $xml; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param \SimpleXMLElement $xml |
|
86
|
|
|
* @param string $prefix |
|
87
|
|
|
* @return string |
|
88
|
|
|
* @throws \Exception |
|
89
|
|
|
* |
|
90
|
|
|
* @link https://github.com/astorm/pestle |
|
91
|
|
|
* @copyright Pulse Storm LLC, Alan Storm |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function getXmlNamespaceFromPrefix(\SimpleXMLElement $xml, $prefix) |
|
94
|
|
|
{ |
|
95
|
|
|
$namespaces = $xml->getDocNamespaces(); |
|
96
|
|
|
|
|
97
|
|
|
if (array_key_exists($prefix, $namespaces)) { |
|
98
|
|
|
return $namespaces[$prefix]; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
throw new \Exception('Unknown namespace in ' . __FILE__); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
This checks looks for assignemnts to variables using the
list(...)function, where not all assigned variables are subsequently used.Consider the following code example.
Only the variables
$aand$care used. There was no need to assign$b.Instead, the list call could have been.