Commit 6f022153 authored by Ad Schellevis's avatar Ad Schellevis

remove complexity in fromArray, write new file in stead of update xml object approach.

parent 23abb70c
...@@ -111,87 +111,43 @@ class Config extends Singleton ...@@ -111,87 +111,43 @@ class Config extends Singleton
} }
/** /**
* update config with array structure (backwards compatibility mode) * update (reset) config with array structure (backwards compatibility mode)
* @param $source source array structure * @param $source source array structure
* @param null $node simplexml node * @param null $node simplexml node
* @return number of updates * @param null|string $parentTagName
* @throws ConfigException * @throws ConfigException
*/ */
public function fromArray($source, $node = null) public function fromArray($source, $node = null, $parentTagName = null)
{ {
$this->checkvalid(); $this->checkvalid();
$updatecount = 0;
// root node // root node
if ($node == null) { if ($node == null) {
$node = $this->simplexml; $this->configxml = new \DOMDocument('1.0');
$this->configxml->loadXML('<'.$this->simplexml[0]->getName().'/>');
$this->simplexml = simplexml_import_dom($this->configxml);
$node = $this->simplexml ;
} }
$activeNodes = array();
$useIndex = false ; // tag recurring, use index
foreach ($source as $itemKey => $itemValue) { foreach ($source as $itemKey => $itemValue) {
// find / create target node
if (is_numeric($itemKey)) { if (is_numeric($itemKey)) {
$useIndex = true; // recurring tag (content), use parent tagname.
// recurring item, select by number $childNode = $node->addChild($parentTagName);
$items = $node->xpath("./*"); } elseif (is_array($itemValue) && !(array_keys($itemValue) !== range(0, count($itemValue) - 1))) {
if (count($items) > $itemKey) { // recurring tag, skip placeholder.
$targetNode = $items[$itemKey]; $childNode = $node;
} else {
// new sequence, add item
$targetNode = $node->addChild($node->getName());
}
} elseif (!isset($node->{$itemKey})) {
// new node, add
if (is_numeric($itemKey)) {
$targetNode = $node->xpath("..")[0]->addChild($node->getName());
} else {
$targetNode = $node->addChild($itemKey);
}
} else { } else {
// if items are a recurring list, skip one level. // add new child
$targetNode = $node->xpath("//".$itemKey); $childNode = $node->addChild($itemKey);
if (count($targetNode) > 1) {
$targetNode = $node ;
} else {
$targetNode = $node->{$itemKey};
}
} }
// add active node // set content, propagate container items.
$activeNodes[] = $itemKey;
// (propagate) update
if (is_array($itemValue)) { if (is_array($itemValue)) {
$updatecount += $this->fromArray($itemValue, $targetNode); $this->fromArray($itemValue, $childNode, $itemKey);
} elseif ($itemValue != $targetNode->{$itemKey}->__toString()) { } else {
$targetNode->{$itemKey} = $itemValue; $childNode[0] = $itemValue;
$updatecount++;
}
}
// remove nodes not in source
$removeItems = array();
$itemId = 0;
foreach ($node->children() as $xmlNode) {
if ((!in_array($itemId, $activeNodes) && $useIndex) ||
!in_array($xmlNode->getName(), $activeNodes)
) {
$removeItems[] = $xmlNode;
} }
$itemId++;
}
foreach ($removeItems as $xmlNode) {
$dom=dom_import_simplexml($xmlNode);
$dom->parentNode->removeChild($dom);
$updatecount++;
} }
// return number of changes
return $updatecount;
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment