Skip to content

Commit

Permalink
Update config file with missing fields from default.
Browse files Browse the repository at this point in the history
  • Loading branch information
sonntam committed Oct 21, 2017
1 parent aae8da2 commit b384d88
Showing 1 changed file with 56 additions and 19 deletions.
75 changes: 56 additions & 19 deletions classes/ConfigFile.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,40 @@ public function ReadFromFile(string $filename) : bool
}
}

// Check if any fields were missing. If so add the default value
// Get Sections of config file
$nsf = new NameSpaceFinder();

$nsc = $nsf->getClassesOfNameSpace("ConfigFile");

// Traverse each Sections
$missing = false;
foreach ($nsc as $key => $sectionpath) {
// The section name itself
$section = lcfirst( endn(explode("\\",$sectionpath)) );
$class = new ReflectionClass($sectionpath);
$props = $class->getStaticProperties();

if( !array_key_exists($section, $cfg) ) {
Log::Warning("Configuration file is missing section \"$section\". I'm adding the default values...");
$cfg[$section] = $props;
$missing = true;
} else {
foreach( $props as $propKey => $propVal ) {
if( !array_key_exists($propKey, $cfg[$section] ) ) {
Log::Warning("Configuration file is missing value \"$section::$propKey\". Setting it to current value \"$propVal\" (probably the default).");
$cfg[$section][$propKey] = $propVal;
$missing = true;
}
}
}
}

// Write missing config options to file
if( $missing ) {
$this->SaveToNewFile($this->filename, $cfg);
}

// Check if versions differ
if( $version != ConfigFile::VERSION )
{
Expand All @@ -136,7 +170,7 @@ public function ReadFromFile(string $filename) : bool
* Save the current configuration to a specific FilesystemIterator
* @param string $filename Path to JSON config file
*/
public function SaveToNewFile(string $filename)
public function SaveToNewFile(string $filename, $cfg = null)
{
if( !isset($filename) || $filename == "" )
{
Expand All @@ -150,25 +184,28 @@ public function SaveToNewFile(string $filename)
$nsc = $nsf->getClassesOfNameSpace("ConfigFile");

// Traverse each Sections
$cfg = [];
foreach ($nsc as $key => $sectionpath) {
// The section name itself
$section = lcfirst( endn(explode("\\",$sectionpath)) );
$class = new ReflectionClass($sectionpath);
$props = $class->getStaticProperties();

// Formatted configurations fit for JSON export
$props_f = $props;
/*foreach ($props as $propname => $propvalue) {
$propname_l = strtolower($propname);
if( $propname != $propname_l )
{
$props_f[$propname_l] = $propvalue;
unset($props_f[$propname]);
}
}*/
if( empty($cfg) ) {

$cfg = [];
foreach ($nsc as $key => $sectionpath) {
// The section name itself
$section = lcfirst( endn(explode("\\",$sectionpath)) );
$class = new ReflectionClass($sectionpath);
$props = $class->getStaticProperties();

// Formatted configurations fit for JSON export
$props_f = $props;
/*foreach ($props as $propname => $propvalue) {
$propname_l = strtolower($propname);
if( $propname != $propname_l )
{
$props_f[$propname_l] = $propvalue;
unset($props_f[$propname]);
}
}*/

$cfg[$section] = $props_f;
$cfg[$section] = $props_f;
}
}

Log::Debug("Saving JSON: ".var_export($cfg,true));
Expand Down

0 comments on commit b384d88

Please sign in to comment.