Added Config base Class

This commit is contained in:
Shoghi Cervantes Pueyo 2013-01-13 16:59:42 +01:00
parent 109b44a205
commit d0cff39aa3

View File

@ -30,15 +30,15 @@ define("CONFIG_PROPERTIES", 0); // .properties
define("CONFIG_CNF", CONFIG_PROPERTIES); // .cnf define("CONFIG_CNF", CONFIG_PROPERTIES); // .cnf
define("CONFIG_JSON", 1); // .js, .json define("CONFIG_JSON", 1); // .js, .json
define("CONFIG_YAML", 2); // .yml, .yaml define("CONFIG_YAML", 2); // .yml, .yaml
define("CONFIG_EXPORT", 3); // .export, .xport //define("CONFIG_EXPORT", 3); // .export, .xport
define("CONFIG_SERIALIZE", 4); // .sl define("CONFIG_SERIALIZED", 4); // .sl
define("CONFIG_LIST", 5); // .txt, .list define("CONFIG_LIST", 5); // .txt, .list
class Config{ class Config{
private $config; private $config;
private $file; private $file;
private $correct;
private $type = CONFIG_DETECT; private $type = CONFIG_DETECT;
private $correct = true;
public static $formats = array( public static $formats = array(
"properties" => CONFIG_PROPERTIES, "properties" => CONFIG_PROPERTIES,
"cnf" => CONFIG_CNF, "cnf" => CONFIG_CNF,
@ -48,16 +48,25 @@ class Config{
"js" => CONFIG_JSON, "js" => CONFIG_JSON,
"yml" => CONFIG_YAML, "yml" => CONFIG_YAML,
"yaml" => CONFIG_YAML, "yaml" => CONFIG_YAML,
"export" => CONFIG_EXPORT, //"export" => CONFIG_EXPORT,
"xport" => CONFIG_EXPORT, //"xport" => CONFIG_EXPORT,
"sl" => CONFIG_SERIALIZE, "sl" => CONFIG_SERIALIZED,
"serialize" => CONFIG_SERIALIZE, "serialize" => CONFIG_SERIALIZED,
"txt" => CONFIG_LIST, "txt" => CONFIG_LIST,
"list" => CONFIG_LIST, "list" => CONFIG_LIST,
); );
public function __construct($file, $type = CONFIG_DETECT, $default = array()){ public function __construct($file, $type = CONFIG_DETECT, $default = array(), &$correct = null){
$this->load($file, $type, $default);
$correct = $this->check();
}
public function load($file, $type = CONFIG_DETECT, $default = array()){
$this->correct = true;
$this->type = (int) $type; $this->type = (int) $type;
$this->file = $file; $this->file = $file;
if(!is_array($default)){
$default = array();
}
if(!file_exists($file)){ if(!file_exists($file)){
$this->config = $default; $this->config = $default;
}else{ }else{
@ -77,17 +86,120 @@ class Config{
case CONFIG_CNF: case CONFIG_CNF:
$this->parseProperties($content); $this->parseProperties($content);
break; break;
case CONFIG_JSON:
$this->config = @json_decode($content, true);
break;
case CONFIG_YAML:
$this->config = Spyc::YAMLLoad($content);
break;
case CONFIG_SERIALIZED:
$this->config = @unserialize($content);
break;
case CONFIG_LIST:
$this->parseList($content);
break;
default:
$this->correct = false;
return false;
break;
} }
var_dump($this->config); if(!is_array($this->config)){
die(); $this->config = $default;
}
$this->fillDefaults($default, $this->config);
}else{
return false;
} }
} }
return true;
}
public function check(){
return $this->correct === true;
}
public function save(){
if($this->correct === true){
switch($this->type){
case CONFIG_PROPERTIES:
case CONFIG_CNF:
$content = $this->writeProperties();
break;
case CONFIG_JSON:
$content = json_encode($this->config);
break;
case CONFIG_YAML:
$content = Spyc::YAMLDump($this->config);
break;
case CONFIG_SERIALIZED:
$content = @serialize($this->config);
break;
case CONFIG_LIST:
$content = implode("\r\n", array_keys($this->config));
break;
}
@file_put_contents($this->file, $content, LOCK_EX);
return true;
}else{
return false;
}
}
public function get($k){
if($this->correct === false or !isset($this->config[$k])){
return false;
}
return ($this->config[$k]);
}
public function set($k, $v = true){
$this->config[$k] = $v;
}
public function getAll(){
return ($this->config);
}
private function fillDefaults($default, &$data){
foreach($default as $k => $v){
if(is_array($v)){
if(!isset($data[$k]) or !is_array($data[$k])){
$data[$k] = array();
}
$this->fillDefaults($v, $data[$k]);
}elseif(!isset($data[$k])){
$data[$k] = $v;
}
}
}
private function parseList($content){
foreach(explode("\n", trim(str_replace("\r", "", $content))) as $v){
$v = trim($v);
if($v == ""){
continue;
}
$this->config[$v] = true;
}
}
private function writeProperties(){
$content = "#Properties Config file\r\n#".date("D M j H:i:s T Y")."\r\n";
foreach($this->config as $k => $v){
if(is_bool($v) === true){
$v = $v === true ? "on":"off";
}elseif(is_array($v)){
$v = implode(";", $v);
}
$content .= $k."=".$v."\r\n";
}
return $content;
} }
private function parseProperties($content){ private function parseProperties($content){
if(preg_match_all('/([a-zA-Z0-9\-_]*)=([^\r\n]*)/u', $content, $matches) == 0){ //false or 0 matches if(preg_match_all('/([a-zA-Z0-9\-_]*)=([^\r\n]*)/u', $content, $matches) > 0){ //false or 0 matches
foreach($matches[1] as $k => $i){ foreach($matches[1] as $i => $k){
$v = trim($matches[2][$k]); $v = trim($matches[2][$i]);
switch(strtolower($v)){ switch(strtolower($v)){
case "on": case "on":
case "true": case "true":