From d461b28b7db60c2e591e4d113fdc6d59cd88645c Mon Sep 17 00:00:00 2001 From: Benjamin Renard Date: Mon, 27 Feb 2023 16:15:30 +0100 Subject: [PATCH] App & Config: add isset() and set() methods --- src/App.php | 24 ++++++++++++++++++++++++ src/Config.php | 25 +++++++++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/App.php b/src/App.php index 760e7cb..2413d1c 100644 --- a/src/App.php +++ b/src/App.php @@ -85,6 +85,17 @@ class App { 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 * @@ -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 * @return string|null diff --git a/src/Config.php b/src/Config.php index 4e7efbc..886eb7b 100644 --- a/src/Config.php +++ b/src/Config.php @@ -101,6 +101,31 @@ Class Config { 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 *