Config: clean up inconsistent data parsing & handling

This commit is contained in:
Dylan K. Taylor 2020-02-05 20:50:36 +00:00
parent e689fd545b
commit 41b1fa7b48

View File

@ -172,31 +172,30 @@ class Config{
}else{ }else{
if($this->correct){ if($this->correct){
$content = file_get_contents($this->file); $content = file_get_contents($this->file);
$config = null;
switch($this->type){ switch($this->type){
case Config::PROPERTIES: case Config::PROPERTIES:
$this->parseProperties($content); $config = $this->parseProperties($content);
break; break;
case Config::JSON: case Config::JSON:
$this->config = json_decode($content, true); $config = json_decode($content, true);
break; break;
case Config::YAML: case Config::YAML:
$content = self::fixYAMLIndexes($content); $content = self::fixYAMLIndexes($content);
$this->config = yaml_parse($content); $config = yaml_parse($content);
break; break;
case Config::SERIALIZED: case Config::SERIALIZED:
$this->config = unserialize($content); $config = unserialize($content);
break; break;
case Config::ENUM: case Config::ENUM:
$this->parseList($content); $config = self::parseList($content);
break; break;
default: default:
$this->correct = false; $this->correct = false;
return false; return false;
} }
if(!is_array($this->config)){ $this->config = is_array($config) ? $config : $default;
$this->config = $default;
}
if($this->fillDefaults($default, $this->config) > 0){ if($this->fillDefaults($default, $this->config) > 0){
$this->save(); $this->save();
} }
@ -531,14 +530,20 @@ class Config{
return $changed; return $changed;
} }
private function parseList(string $content) : void{ /**
* @return true[]
* @phpstan-return array<string, true>
*/
private static function parseList(string $content) : array{
$result = [];
foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){ foreach(explode("\n", trim(str_replace("\r\n", "\n", $content))) as $v){
$v = trim($v); $v = trim($v);
if($v == ""){ if($v == ""){
continue; continue;
} }
$this->config[$v] = true; $result[$v] = true;
} }
return $result;
} }
private function writeProperties() : string{ private function writeProperties() : string{
@ -555,7 +560,12 @@ class Config{
return $content; return $content;
} }
private function parseProperties(string $content) : void{ /**
* @return mixed[]
* @phpstan-return array<string, mixed>
*/
private function parseProperties(string $content) : array{
$result = [];
if(preg_match_all('/^\s*([a-zA-Z0-9\-_\.]+)[ \t]*=([^\r\n]*)/um', $content, $matches) > 0){ //false or 0 matches if(preg_match_all('/^\s*([a-zA-Z0-9\-_\.]+)[ \t]*=([^\r\n]*)/um', $content, $matches) > 0){ //false or 0 matches
foreach($matches[1] as $i => $k){ foreach($matches[1] as $i => $k){
$v = trim($matches[2][$i]); $v = trim($matches[2][$i]);
@ -571,11 +581,13 @@ class Config{
$v = false; $v = false;
break; break;
} }
if(isset($this->config[$k])){ if(isset($result[$k])){
MainLogger::getLogger()->debug("[Config] Repeated property " . $k . " on file " . $this->file); MainLogger::getLogger()->debug("[Config] Repeated property " . $k . " on file " . $this->file);
} }
$this->config[$k] = $v; $result[$k] = $v;
} }
} }
return $result;
} }
} }