values = null; } public $values; public function load($file) { $doc = new DOMDocument; if (! @$doc->load($file)) throw new IMuException('ConfigLoad', $file); $xpath = new DOMXpath($doc); $values = $this->loadNode($xpath, $doc->documentElement); $this->mergeValue($this->values, $values); } public function merge($values) { $this->mergeValue($this, $values); } protected function loadNode($xpath, $node) { $children = $xpath->query('*', $node); $length = $children->length; if ($length == 0) return $node->nodeValue; if ($length == 1) { $type = $node->getAttribute('type'); if ($type == '') $type = 'set'; } else { $type = 'list'; $first = $children->item(0)->nodeName; for ($i = 1; $i < $length; $i++) { $child = $children->item($i); if ($child->nodeName != $first) { $type = 'set'; break; } } } if ($type == 'list') { $list = array(); for ($i = 0; $i < $length; $i++) { $child = $children->item($i); $list[] = $this->loadNode($xpath, $child); } return $list; } $set = array(); for ($i = 0; $i < $length; $i++) { $child = $children->item($i); $name = $child->nodeName; $set[$name] = $this->loadNode($xpath, $child); } return $set; } protected function mergeValue(&$old, $new) { if (! is_array($old)) $old = $new; else if (array_keys($old) === range(0, count($old) - 1)) { /* list */ if (is_array($new)) { foreach ($new as $name => $value) $old[] = $value; } else $old[] = $new; } else { /* set */ if (is_array($new)) { foreach ($new as $name => $value) { if (array_key_exists($name, $old)) $this->mergeValue($old[$name], $value); else $old[$name] = $value; } } } } } ?>