Skip to content

Instantly share code, notes, and snippets.

@jbrooksuk
Created March 26, 2012 09:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jbrooksuk/2204090 to your computer and use it in GitHub Desktop.
Save jbrooksuk/2204090 to your computer and use it in GitHub Desktop.
PHP .properties class
<?php
/**
* Parses the build.properties used by Phing
* @author James <james@bluebaytravel.co.uk>
*/
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