From e7e955f5739acc3bbb04a7aee153867f0c8bc3ab Mon Sep 17 00:00:00 2001 From: Franco Fichtner Date: Thu, 16 Sep 2021 13:43:14 +0200 Subject: [PATCH] mvc: retain attributes in single values; closes #4633 Second try: retain attribute values as sibling nodes with a name up front. If the sibling does not exist fail silently like before. At least from testing this no longer produces any shift in the config.xml between string nodes with attributes. Test XML: Test PHP: fromArray(load_config_from_file('foo.xml')); print_r(OPNsense\Core\Config::getInstance()->__toString()); Result XML: --- .../mvc/app/library/OPNsense/Core/Config.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/opnsense/mvc/app/library/OPNsense/Core/Config.php b/src/opnsense/mvc/app/library/OPNsense/Core/Config.php index 523b57b5a..58d1ac5ed 100644 --- a/src/opnsense/mvc/app/library/OPNsense/Core/Config.php +++ b/src/opnsense/mvc/app/library/OPNsense/Core/Config.php @@ -1,7 +1,7 @@ attributes() as $AttrKey => $AttrValue) { if (!isset($result['@attributes'])) { - $result['@attributes'] = array(); + $result['@attributes'] = []; } $result['@attributes'][$AttrKey] = $AttrValue->__toString(); } @@ -147,6 +147,13 @@ class Config extends Singleton } else { $result[$xmlNodeName] = $xmlNode->__toString(); } + // copy attributes to xzy@attribute key item + foreach ($xmlNode->attributes() as $AttrKey => $AttrValue) { + if (!isset($result["${xmlNodeName}@attributes"])) { + $result["${xmlNodeName}@attributes"] = []; + } + $result["${xmlNodeName}@attributes"][$AttrKey] = $AttrValue->__toString(); + } } } } @@ -206,6 +213,15 @@ class Config extends Singleton $node->addAttribute($attrKey, $attrValue); } continue; + } elseif (strstr($itemKey , '@attributes') !== false) { + $origname = str_replace('@attributes', '', $itemKey); + if (count($node->$origname)) { + // copy xml attributes + foreach ($itemValue as $attrKey => $attrValue) { + $node->$origname->addAttribute($attrKey, $attrValue); + } + } + continue; } elseif (is_numeric($itemKey)) { // recurring tag (content), use parent tagname. $childNode = $node->addChild($parentTagName);