App & Config: add isset() and set() methods
This commit is contained in:
parent
227e7bba7f
commit
d461b28b7d
2 changed files with 49 additions and 0 deletions
24
src/App.php
24
src/App.php
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
*
|
*
|
||||||
|
|
Loading…
Reference in a new issue