App & Config: add isset() and set() methods

This commit is contained in:
Benjamin Renard 2023-02-27 16:15:30 +01:00
parent 227e7bba7f
commit d461b28b7d
WARNING! Although there is a key with this ID in the database it does not verify this commit! This commit is SUSPICIOUS.
GPG key ID: 3E2E1CE1907115BC
2 changed files with 49 additions and 0 deletions

View file

@ -85,6 +85,17 @@ class App {
return !is_null(self :: $root_directory_path); return !is_null(self :: $root_directory_path);
} }
/**
* Check if a specific configuration variable is set
*
* @param string $key The configuration variable key
* @param array|null $config Optional configuration to use instead of current loaded configuration
* @return bool
**/
public static function isset($key, &$config=null) {
return Config::isset($key, self :: $options) || (Config::loaded() && Config::isset($key));
}
/** /**
* Get a specific option value * Get a specific option value
* *
@ -108,6 +119,19 @@ class App {
); );
} }
/**
* Set a specific option value
*
* @param string $key The configuration variable key
* @param mixed $value The configuration variable value
* @return boolean
**/
public static function set($key, $value) {
if (Config::isset($key, self :: $options))
return Config::set($key, $value, self :: $options);
return Config::set($key, $value);
}
/** /**
* Retreive application root directory path * Retreive application root directory path
* @return string|null * @return string|null

View file

@ -101,6 +101,31 @@ Class Config {
self :: $extra_variables[$key] = $value; self :: $extra_variables[$key] = $value;
} }
/**
* Check if a specific configuration variable is set
*
* @param string $key The configuration variable key
* @param array|null $config Optional configuration to use instead of current loaded configuration
* @return bool
**/
public static function isset($key, &$config=null) {
if (array_key_exists($key, self :: $extra_variables))
return true;
if (!is_array($config) && !self :: loaded()) {
Log :: fatal('Configuration not loaded (on checking if %s is set)', $key);
exit(1);
}
$exploded_key = explode('.', $key);
if (!is_array($exploded_key)) return false;
$value = is_array($config)?$config:self :: $config;
foreach ($exploded_key as $k) {
if (!is_array($value) || !isset($value[$k]))
return false;
$value = $value[$k];
}
return true;
}
/** /**
* Get a specific configuration variable value * Get a specific configuration variable value
* *