Add possibility to define config parameters default value in app's options

This commit is contained in:
Benjamin Renard 2023-02-27 17:52:51 +01:00
parent 7e8b6ee5f2
commit 4f46c38643
Signed by: bn8
GPG key ID: 3E2E1CE1907115BC
4 changed files with 37 additions and 2 deletions

View file

@ -95,8 +95,8 @@ templates:
# Translations
#
i18n:
# Default locale (see locales directory for available languages list)
default_locale: 'en_US.UTF8'
# Default locale (see locales directory for available languages list, default: 'en_US.UTF8')
#default_locale: 'en_US.UTF8'
#
# Session

View file

@ -38,6 +38,12 @@ App::init(
"$root_dir_path/static"
),
),
'default' => array(
'upload_max_filesize' => 10485760,
'i18n' => array(
'default_locale' => "en_US.UTF8",
),
),
),
$root_dir_path
);

View file

@ -38,6 +38,9 @@ App::init(
"$root_dir_path/static"
),
),
'default' => array(
// Set here your configuration parameters default value
),
),
$root_dir_path
);

View file

@ -115,6 +115,7 @@ class App {
* @return mixed The configuration variable value
**/
public static function get($key, $default=null, $cast=null, $split=true) {
$default = self :: get_default($key, $default, $cast, $split);
return Config::get(
$key,
Config::loaded()?Config::get($key, $default, $cast, $split):$default,
@ -124,6 +125,31 @@ class App {
);
}
/**
* Get a specific option default value
*
* @param string $key The configuration variable key
* @param mixed $default Default value provided by context
* (optional, priority if not nul, default : null)
* @param string $cast The type of expected value. The configuration variable
* default value will be cast as this type. Could be : bool, int,
* float or string. (Optional, default : raw value)
* @param bool $split If true, $cast is 'array' and value retreived from configuration
* is a string, split the value by comma (optional, default: true)
* @return mixed The configuration variable default value
**/
public static function get_default($key, $default=null, $cast=null, $split=true) {
if (!is_null($default))
return $default;
return Config::get(
"default.$key",
$default,
$cast,
$split,
self :: $options
);
}
/**
* Set a specific option value
*