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.
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
Hier einige Anwendungsbeispiele der Classe PropertyReader
<?php echo '<h1>PropertyReader Test</h1>'; //Library einbinden include_once('PropertyReader.php'); $nl = "<br />\n"; echo '<h2>einfache Anwendung des PropertyReader</h2>'; //Reader erstellen $p = new PropertyReader('test.properties'); echo "<h3>Alle Properties ausgeben</h3>"; 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 "<h3>direkter Zugriff auf ein Property</h3>"; echo "test1: {$p->test1}{$nl}"; echo "testArray[1]: {$p->testArray[1]}{$nl}"; echo '<h2>PropertyReader mit Vererbung</h2>'; 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 "<h3>direkter Zugriff auf ein Property</h3>"; echo "test1: {$m->test1}{$nl}"; echo "testArray[1]: {$m->testArray[1]}{$nl}"; ?>
test1: hallo test2: # test3: REM testArray[0]: 1 testArray[1]: 2 testArray[2]: value 3 testArray[3]: und #4 #5: Test # TestAscii: #$34
test1: hallo testArray[1]: 2
<?php /** * mpl by ERB software * @author stefan.erb(at)erb-software.com */ interface IPropertyClass extends ArrayAccess{ /** * @param String FilePath * @access public */ public function openPropertyFile($propertyFileName); /** * @param String * @return Variant * @access public */ public function __get($name); /** * @param String * @param Variant * @access public */ public function __set($name, $value); /** * getProperties * @return propertyarray */ public function toArray(); } ?>
<?php /** * mpl by ERB software * @author stefan.erb(at)erb-software.com */ require_once(dirname(__FILE__)."/IPropertyClass.php"); class PropertyReader extends ArrayObject implements IPropertyClass{ const PATTERN_ARRAY = '|\A\((.+)\)\Z|'; const DEFAULT_SEPERATOR = '='; const REMARKS_PATTERN = '/^(?!#|REM)/'; /** * In diesem Array können Standartpfade definiert werden, in denen bei fehlendem * Pfad nach der Propertydatei gesucht wird * @var Array<String> * @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 Array<key=>value> * @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(); } } ?>