You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
152 lines
4.0 KiB
152 lines
4.0 KiB
<?php
|
|
/**
|
|
* Setup Config data
|
|
*
|
|
* PHP version 8.3
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author Robert S. <tips@technowizardbob.com>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy
|
|
*/
|
|
|
|
/**
|
|
* Configure your app
|
|
*
|
|
* @category Util
|
|
* @package Neato
|
|
* @author Robert S. <tips@technowizardbob.com>
|
|
* @license https://mit-license.org/ MIT License
|
|
* @link https://git.mysnippetsofcode.com/tts/neatoDeploy
|
|
*/
|
|
final class Configure
|
|
{
|
|
|
|
/**
|
|
* This private static var holds all configuration data.
|
|
*
|
|
* @staticvar static array $config holds all system config data.
|
|
*/
|
|
private static array $_config = [];
|
|
|
|
/**
|
|
* Deny creating instance as all methods are static here.
|
|
*/
|
|
protected function __construct()
|
|
{
|
|
}
|
|
|
|
/**
|
|
* Fetches a setting set from using Configure::set() or add or update
|
|
*
|
|
* @param string $name The name of the setting to get
|
|
* @param string|false $key [optional] The Array Key to fetch
|
|
* The setting specified by $name,
|
|
* or null if $name was not set
|
|
*
|
|
* @return mixed Fetch Config data
|
|
*/
|
|
public static function get(string $name, string|false $key = false): mixed
|
|
{
|
|
if (isset(static::$_config[strtolower($name)])) {
|
|
$a = static::$_config[strtolower($name)];
|
|
if ($key === false) {
|
|
return $a;
|
|
}
|
|
if (isset($a[$key])) {
|
|
return $a[$key];
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Checks if the setting exists
|
|
*
|
|
* @param $name The name of the setting to check existance
|
|
*
|
|
* @return boolean true if $name was set, false otherwise
|
|
*/
|
|
public static function exists(string $name): bool
|
|
{
|
|
if (array_key_exists(strtolower($name), static::$_config)) {
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Overwrite/Update/Add to $config
|
|
*
|
|
* @param string $name the main key to update.
|
|
* @param string $key the sub key.
|
|
* @param mixed $value the data to update
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
public static function update(string $name, string $key, mixed $value): void
|
|
{
|
|
static::$_config[strtolower($name)][strtolower($key)] = $value;
|
|
}
|
|
|
|
/**
|
|
* Add to existing data without loss... to $config
|
|
*
|
|
* @param string $name the main key
|
|
* @param string $key the sub key
|
|
* @param mixed $value new data to add
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
public static function add(string $name, string $key, mixed $value): void
|
|
{
|
|
static::$_config[strtolower($name)][strtolower($key)][] = $value;
|
|
}
|
|
|
|
/**
|
|
* Frees the setting given by $name, if it exists. All settings no longer in
|
|
* use should be freed using this method whenever possible.
|
|
*
|
|
* @param string $name The name of the setting to free
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
public static function free(string $name): void
|
|
{
|
|
if (static::exists($name)) {
|
|
unset(static::$_config[strtolower($name)]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Adds the given $value to the configuration using the $name given
|
|
*
|
|
* @param string $name The name to give this setting. Use Configure::exists()
|
|
* to check for pre-existing settings with the same name.
|
|
* @param mixed $value The value to set
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
public static function set(string $name, mixed $value): void
|
|
{
|
|
static::$_config[strtolower($name)] = $value;
|
|
}
|
|
|
|
/**
|
|
* Sets $config data from an Array
|
|
*
|
|
* @param array $a ($name => $value)
|
|
*
|
|
* @return void not ah
|
|
*/
|
|
public static function loadArray(array $a): void
|
|
{
|
|
foreach ($a as $name => $value) {
|
|
static::$_config[strtolower($name)] = $value;
|
|
}
|
|
unset($a);
|
|
}
|
|
|
|
} // end of configure
|
|
|
|
|