Created
March 26, 2012 09:14
-
-
Save jbrooksuk/2204090 to your computer and use it in GitHub Desktop.
PHP .properties class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Parses the build.properties used by Phing | |
* @author James <[email protected]> | |
*/ | |
class Properties { | |
protected static $_instance = NULL; | |
public static $propFile = ""; | |
public static $oProps = []; | |
public function __construct($propFile = NULL) { | |
$propFile = ($propFile !== NULL ? $propFile : FALSE); | |
if(!$propFile) trigger_error("Properties class requires *.properties filename"); | |
static::$propFile = $propFile; | |
static::parse(); | |
} | |
public static function parse() { | |
$hFile = file_get_contents(static::$propFile); | |
$aLines = explode("\n", $hFile); | |
$keyVal = ""; | |
$isWaitingOtherLine = FALSE; | |
foreach($aLines as $i => $line) { | |
if(empty($line) || (!$isWaitingOtherLine && strpos($line, "#") === 0)) continue; | |
if(!$isWaitingOtherLine) { | |
$keyVal = trim(substr($line, 0, strpos($line, "="))); | |
$valVal = trim(substr($line, strpos($line, "=") + 1, strlen($line))); | |
}else{ | |
$valVal .= $line; | |
} | |
# Check if ends with single '\' | |
if(strpos($valVal, "\\") === strlen($valVal) - strlen("\\")) { | |
$valVal = substr($valVal, 0, strlen($valVal)-1) . "\n"; | |
$isWaitingOtherLine = TRUE; | |
}else{ | |
$isWaitingOtherLine = FALSE; | |
} | |
static::$oProps[$keyVal] = $valVal; | |
unset($aLines[$i]); | |
} | |
return static::$oProps; | |
} | |
public function get($propName) { | |
if(isset(static::$oProps[$propName])) return static::$oProps[$propName]; | |
return FALSE; | |
} | |
public static function getInstance() { | |
if(static::$_instance === NULL) { | |
static::$_instance = new static(static::$propFile); | |
} | |
return static::$_instance; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment