======[PHP] Property Reader====== PropertyReader ist eine Library die ich für mich geschrieben habe. Von Java lernte ich das System der PropertyFiles kennen und lieben. Um dies auch in PHP zu nutzen musste also ein Entsprechender Reader her, der unabhängig von anderen Libraries funktioniert. =====Anwendung===== ====Property File==== Als Beispiel ein Property-File, mit allen möglichen Varianten drin Property File # Komentare mit # am Zeileanfang markieren REM oder mit dem Wort REM (gross geschrieben) test1 = hallo test2 = # test3 = REM # Array als Value testArray = (1,2,value 3, und #4) # Wenn ein # am Anfang stehen soll, muss diesen als ascii-Wert geschrieben werden #5 = Test # # Alle ascii-Strings werden übersetzt TestAscii = #$34 ====Test PHP File==== Hier einige Anwendungsbeispiele der Classe PropertyReader PropertyReader Test'; //Library einbinden include_once('PropertyReader.php'); $nl = "
\n"; echo '

einfache Anwendung des PropertyReader

'; //Reader erstellen $p = new PropertyReader('test.properties'); echo "

Alle Properties ausgeben

"; foreach($p as $name => $value){ //Array_properties if (is_array($value) or is_a($value, 'ArrayObject')){ foreach($value as $index => $sub){ echo "{$name}[{$index}]: {$sub}{$nl}"; } } else { // Normale String-Properties echo "{$name}: {$value}{$nl}"; } } echo "

direkter Zugriff auf ein Property

"; echo "test1: {$p->test1}{$nl}"; echo "testArray[1]: {$p->testArray[1]}{$nl}"; echo '

PropertyReader mit Vererbung

'; class myReader extends PropertyReader{ public function __construct(){ parent::__construct('test.properties'); } public function __get($name){ $value = parent::__get($name); return (is_array($value) or is_a($value, 'ArrayObject')) ? $value : "MyReader_{$value}"; } } $m = new MyReader(); echo "

direkter Zugriff auf ein Property

"; echo "test1: {$m->test1}{$nl}"; echo "testArray[1]: {$m->testArray[1]}{$nl}"; ?>
====Ausgabe==== ||====PropertyReader Test==== ===einfache Anwendung des PropertyReader=== ==Alle Properties ausgeben== test1: hallo test2: # test3: REM testArray[0]: 1 testArray[1]: 2 testArray[2]: value 3 testArray[3]: und #4 #5: Test # TestAscii: #$34 ==direkter Zugriff auf ein Property== test1: hallo testArray[1]: 2 ===PropertyReader mit Vererbung=== ==direkter Zugriff auf ein Property== test1: MyReader_hallo testArray[1]: 2|| ---- {{image class="left" url="images/icons/48/application-x-php.png"}} =====Code===== * @access protected */ protected $paths = array('','ear/cfg'); /** * @var String * @access protected */ protected $seperator = self::DEFAULT_SEPERATOR; /** * construct * @param propertyFilePath */ public function __construct($propertyFileName = NULL, $seperator = self::DEFAULT_SEPERATOR){ $this->seperator = $seperator ? $seperator : self::DEFAULT_SEPERATOR; if($propertyFileName) $this->openPropertyFile($propertyFileName); } /** * @param String FilePath * @access public */ public function openPropertyFile($propertyFileName){ try { //reset the parent $this->exchangeArray(array()); //get the file $propertyFilePath = $this->findFile($propertyFileName); //read the file $this->readPropertiesFromFile($propertyFilePath); } catch (Exception $e){ throw $e; } } /** * @param String file name of the property * @return String file path * @access protected */ protected function findFile($propertyFilePath){ if (!file_exists($propertyFilePath) or !is_file($propertyFilePath)) { /** search the file in all property file paths */ foreach ($this->paths as $path){ if (file_exists("{$path}/{$propertyFilePath}")) { $propertyFilePath = "{$path}/{$propertyFilePath}"; break; } } if (!file_exists($propertyFilePath) or !is_file($propertyFilePath)) throw new Exception("File '{$propertyFilePath}' not found"); } return $propertyFilePath; } /** * @param String file path * @return Arrayvalue> * @access protected */ protected function readPropertiesFromFile($propertyFilePath){ $lines = array_filter(file($propertyFilePath), array($this, 'lineFilter')); array_walk($lines, array($this, 'addPropertyFromLine')); } /** * Filter for the Lines * @param String line * @return boolean */ protected function lineFilter($line){ return (trim($line) && preg_match(self::REMARKS_PATTERN, $line)); } /** * addPropertyFromLine * @param String property line */ protected function addPropertyFromLine($line){ while(is_numeric($length=eregi('~([0-9]+)~', $line, $found))){ $line = str_replace($found[0], chr($found[1]), $line); } $property = array_map('trim', explode($this->seperator, $line)); $key = $property[0]; $value = $property[1]; if (preg_match(self::PATTERN_ARRAY, $value, $found)) { $value = new ArrayObject(explode(',', $found[1])) ; } $this->$key = $value; } /** * @param String * @return Variant * @access public */ public function __get($key){ return $this[$key]; } /** * @param String * @param Variant * @access public */ public function __set($key, $value){ $this[$key] = $value; } /** * getProperties * @return propertyarray */ public function toArray(){ return $this->getArrayCopy(); } } ?> ---- {{image class="left" alt="ico" url="images/icons/48/zip.png"}} {{tag>PHP Library}}