* * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. */ class ReadConfig { private $contentRootNode; private $dataRootNode; function __construct($file) { $this->contentRootNode = new ListPathContentElement(); $this->dataRootNode = new ListPathDataElement(); $matches = null; $sortedMatches = null; $commentStarted = null; $commentContent = null; foreach (new FileIterator($file) as $line) { unset ($matches); unset ($sortedMatches); if (ereg("^([^:space:]*)='([^']*)'[:space:]*(.*)$", $line, $matches)) { // Wenn der Variablen-Wert nicht mit " eingeschlossen ist, wird versucht ' zu benutzen $commentStarted = false; } elseif (ereg('^([^:space:]*)="([^"]*)"[:space:]*(.*)$', $line, $matches)) { $commentStarted = false; } else { // Kommentar if (preg_match("/^#[-]+/", $line)) { if ($commentStarted) { $commentStarted = false; $this->contentRootNode->add(new ListValueElement(null, $commentContent)); unset ($commentContent); } else $commentStarted = true; } elseif ($commentStarted) { $commentContent .= trim(str_replace('#', "", $line)) . "\n"; } continue; } if (count($matches) < 2) throw new Exception("Syntaxerror in Zeile $lineNo"); $varName = $matches[1]; $varValue = $matches[2]; $varComment = trim($matches[3]); $matches = preg_split("/_([0-9]+)[_]?/", $varName, -1, PREG_SPLIT_DELIM_CAPTURE); // Ergebnis puffern damit später im selben Schleifendurchlauf auf den Index und den Namen zugegriffen werden kann ($sortedMatches[] = array(%index%, %Inhalt%) $index = 0; if (count($matches > 1)) { foreach ($matches as $match) { if (is_numeric($match)) { $index = $match; } else $sortedMatches[] = array ( $index, $match ); } } else { $sortedMatches = $matches; } /** * Zwei Bäume anlegen: * 1. Struktur wie in check-Datei (actElementData) * - Ein Objekt pro Variable * 2. Struktur wie in config-Datei (actElementContent) * - Ein Objekt pro Variablen-Wert */ $actElementContent = $this->contentRootNode; $actElementData = $this->dataRootNode; foreach ($sortedMatches as $matchData) { list ($index, $varNamePart) = $matchData; #echo $varNamePart."_".$index."\n"; // Wenn der Variablen-Teil bereits besteht, kein neues Objekt anlegen sondern Referenz auf vorhandenes setzen. if (!$actElementData->isExistingName($varNamePart)) $actElementData = $actElementData->add(new ListPathDataElement($varNamePart)); else $actElementData = $actElementData->getChildByName($varNamePart); /** * Wenn die Variablen-Teil-Position bereits besteht, kein neues Objekt anlegen sondern Referenz auf vorhandenes setzen. * Wenn wir in der obersten Ebene sind (z.B. START_ASTERISK=...), sind keine Positionen vorhanden, also immer anlegen. */ if (!$actElementContent->isExistingPos($index) || $index == 0) $actElementContent = $actElementContent->add(new ListPathContentElement($actElementData, $index)); else $actElementContent = $actElementContent->getChildByPos($index); } // Variablen-Wert als Leaf-Element an den Content-Baum anhängen. $actElementContent->add(new ListValueElement($varValue, $varComment)); } // Falls ein Kommentar nicht mit #---... geschlossen wurde if (!empty ($commentContent)) $this->contentRootNode->add(new ListValueElement(null, trim($commentContent) . "\n")); } function output() { $this->contentRootNode->output(); #$this->dataRootNode->output(); #var_dump($this->contentRootNode); #print_r($this->dataRootNode); #print_r($this->contentRootNode); #foreach($this->contentRootNode as $child) { #var_dump($this->contentRootNode->getChildByName("START_ASTERISK")); #echo "START_ASTERISK='".$this->contentRootNode->getValue("START_ASTERISK")."'\n"; #foreach ($this->contentRootNode as $child) { # echo $child->getName() . "='" . $child->getValue() . "'\n"; #} #print_r($this->dataRootNode->getChildByPath(array("ASTERISK_REDIRECT", "SCHEME"))); #print_r($this->contentRootNode->getChildByPath(array("ASTERISK_REDIRECT", 1, "SCHEME", 1))); #print_r($this->contentRootNode->getChildByNamePos("ASTERISK_REDIRECT", 1)); #print_r($this->contentRootNode->getChildByName("ASTERISK_SIP")); } } ?>