Updated configuration manager to not use hard-coded configuration values (there's no way I'm hard-coding every single configuration value and checking their correct type.)

This commit is contained in:
Netkas 2022-12-19 18:59:02 -05:00
parent 7ffbdf72c1
commit 8ff91590d2
2 changed files with 34 additions and 194 deletions

View file

@ -121,202 +121,18 @@
*/
public function updateProperty(string $property, $value): bool
{
// composer.options.quiet
$result = match (strtolower(explode('.', $property)[0]))
$keys = explode('.', $property);
$current = &$this->Configuration;
foreach ($keys as $k)
{
'ncc' => $this->updateNccProperties($property, $value),
'php' => $this->updatePhpProperties($property, $value),
'git' => $this->updateGitProperties($property, $value),
'runners' => $this->updateRunnerProperties($property, $value),
'composer' => $this->updateComposerProperties($property, $value),
default => false,
};
if (!array_key_exists($k, $current))
{
return false;
}
$current = &$current[$k];
}
$current = Functions::stringTypeCast($value);
$this->save();
return $result;
}
/**
* Updates NCC configuration properties in the configuration
*
* @param string $property
* @param $value
* @return bool
*/
private function updateNccProperties(string $property, $value): bool
{
$delete = false;
if(is_null($value))
$delete = true;
switch(strtolower($property))
{
case 'ncc.cli.no_colors':
$this->Configuration['ncc']['cli']['no_colors'] = Functions::cbool($value);
break;
case 'ncc.cli.basic_ascii':
$this->Configuration['ncc']['cli']['basic_ascii'] = Functions::cbool($value);
break;
case 'ncc.cli.logging':
$this->Configuration['ncc']['cli']['logging'] = ($delete ? LogLevel::Info : (string)$value);
break;
default:
return false;
}
return true;
}
/**
* Updates PHP properties in the configuraiton
*
* @param string $property
* @param $value
* @return bool
*/
private function updatePhpProperties(string $property, $value): bool
{
$delete = false;
if(is_null($value))
$delete = true;
switch(strtolower($property))
{
case 'php.executable_path':
$this->Configuration['php']['executable_path'] = ($delete ? null : (string)$value);
break;
case 'php.runtime.initialize_on_require':
$this->Configuration['php']['runtime']['initialize_on_require'] = Functions::cbool($value);
break;
case 'php.runtime.handle_exceptions':
$this->Configuration['php']['runtime']['handle_exceptions'] = Functions::cbool($value);
break;
default:
return false;
}
return true;
}
/**
* Updated git properties
*
* @param string $property
* @param $value
* @return bool
*/
private function updateGitProperties(string $property, $value): bool
{
$delete = false;
if(is_null($value))
$delete = true;
switch(strtolower($property))
{
case 'git.enabled':
$this->Configuration['git']['enabled'] = Functions::cbool($value);
break;
case 'git.executable_path':
$this->Configuration['git']['executable_path'] = ($delete? null : (string)$value);
break;
default:
return false;
}
return true;
}
/**
* Updaters runner properties
*
* @param string $property
* @param $value
* @return bool
*/
private function updateRunnerProperties(string $property, $value): bool
{
$delete = false;
if(is_null($value))
$delete = true;
switch(strtolower($property))
{
case 'runners.php':
$this->Configuration['runners']['php'] = ($delete? null : (string)$value);
break;
case 'runners.bash':
$this->Configuration['runners']['bash'] = ($delete? null : (string)$value);
break;
case 'runners.sh':
$this->Configuration['runners']['sh'] = ($delete? null : (string)$value);
break;
case 'runners.python':
$this->Configuration['runners']['python'] = ($delete? null : (string)$value);
break;
case 'runners.python3':
$this->Configuration['runners']['python3'] = ($delete? null : (string)$value);
break;
case 'runners.python2':
$this->Configuration['runners']['python2'] = ($delete? null : (string)$value);
break;
default:
return false;
}
return true;
}
/**
* Updates a composer property value
*
* @param string $property
* @param $value
* @return bool
*/
private function updateComposerProperties(string $property, $value): bool
{
$delete = false;
if(is_null($value))
$delete = true;
switch(strtolower($property))
{
case 'composer.enabled':
$this->Configuration['composer']['enabled'] = Functions::cbool($value);
break;
case 'composer.enable_internal_composer':
$this->Configuration['composer']['enable_internal_composer'] = Functions::cbool($value);
break;
case 'composer.executable_path':
$this->Configuration['composer']['executable_path'] = ($delete? null : (string)$value);
break;
case 'composer.options.quiet':
$this->Configuration['composer']['options']['quiet'] = Functions::cbool($value);
break;
case 'composer.options.no_ansi':
$this->Configuration['composer']['options']['no_ansi'] = Functions::cbool($value);
break;
case 'composer.options.no_interaction':
$this->Configuration['composer']['options']['no_interaction'] = Functions::cbool($value);
break;
case 'composer.options.profile':
$this->Configuration['composer']['options']['profile'] = Functions::cbool($value);
break;
case 'composer.options.no_scripts':
$this->Configuration['composer']['options']['no_scripts'] = Functions::cbool($value);
break;
case 'composer.options.no_cache':
$this->Configuration['composer']['options']['no_cache'] = Functions::cbool($value);
break;
case 'composer.options.logging':
$this->Configuration['composer']['options']['logging'] = ((int)$value > 0 ? (int)$value : 1);
break;
default:
return false;
}
return true;
}

View file

@ -918,4 +918,28 @@
return $selected;
}
/**
* Attempts to cast the correct type of the given value
*
* @param string $input
* @return float|int|mixed|string
*/
public static function stringTypeCast(string $input)
{
if (is_numeric($input))
{
if (strpos($input, '.') !== false)
return (float)$input;
if (ctype_digit($input))
return (int)$input;
}
elseif (in_array(strtolower($input), ['true', 'false']))
{
return filter_var($input, FILTER_VALIDATE_BOOLEAN);
}
return (string)$input;
}
}