parent
ad784311c1
commit
0acb2c038c
@ -1,97 +1,152 @@ |
||||
<?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 |
||||
*/ |
||||
|
||||
final class configure { |
||||
private static $config = array(); |
||||
protected function __construct() { } |
||||
/** |
||||
* 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 |
||||
* |
||||
* $name The name of the setting to get |
||||
* $key [optional] The Array Key to fetch |
||||
* The setting specified by $name, or null if $name was not set |
||||
* |
||||
* return type: ?array |
||||
*/ |
||||
public static function get(string $name, $key = false) { |
||||
if (isset(self::$config[strtolower($name)])) { |
||||
$a = self::$config[strtolower($name)]; |
||||
if ($key === false) { |
||||
return $a; |
||||
} |
||||
if (isset($a[$key])) { |
||||
return $a[$key]; |
||||
} |
||||
/** |
||||
* 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; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/* |
||||
* Checks if the setting exists |
||||
* |
||||
* $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), self::$config)) { |
||||
return true; |
||||
/** |
||||
* 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; |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/* |
||||
* Overwrite/Update/Add to $config |
||||
* $name the main key to update |
||||
* $key the sub key |
||||
* type $value the data to update |
||||
*/ |
||||
public static function update(string $name, string $key, $value): void { |
||||
self::$config[strtolower($name)][strtolower($key)] = $value; |
||||
} |
||||
/** |
||||
* 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 |
||||
* $name the main key |
||||
* $key the sub key |
||||
* $value new data to add |
||||
*/ |
||||
public static function add(string $name, string $key, $value): void { |
||||
self::$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 |
||||
* |
||||
* $name The name of the setting to free |
||||
*/ |
||||
public static function free(string $name): void { |
||||
if (self::exists($name)) |
||||
unset(self::$config[strtolower($name)]); |
||||
} |
||||
/** |
||||
* 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 |
||||
* |
||||
* $name The name to give this setting. Use Configure::exists() |
||||
* to check for pre-existing settings with the same name |
||||
* $value The value to set |
||||
*/ |
||||
public static function set(string $name, $value): void { |
||||
self::$config[strtolower($name)] = $value; |
||||
} |
||||
/** |
||||
* 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 |
||||
* array $a ($name => $value) |
||||
* retutns a void |
||||
*/ |
||||
public static function load_array(array $a): void { |
||||
foreach ($a as $name => $value) { |
||||
self::$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); |
||||
} |
||||
unset($a); |
||||
} |
||||
|
||||
} // end of configure |
||||
|
||||
|
||||
@ -0,0 +1,7 @@ |
||||
<?php |
||||
|
||||
$ok = useMe('wget'); |
||||
if ($ok !== false) { |
||||
doCommand('wget::download', 'phpcs', 'https://squizlabs.github.io/PHP_CodeSniffer/phpcs.phar'); |
||||
doCommand('wget::download', 'phpcbf', 'https://squizlabs.github.io/PHP_CodeSniffer/phpcbf.phar'); |
||||
} |
||||
Loading…
Reference in new issue