forked from cubetiq/cubetiq_dart_shared
Task: Add cubetiq dart shared library and functions and utils and extensions for developers and completed with configurable
This commit is contained in:
15
lib/src/configurable/configurable.dart
Normal file
15
lib/src/configurable/configurable.dart
Normal file
@@ -0,0 +1,15 @@
|
||||
import 'system_config.dart';
|
||||
|
||||
/// get config or null from system config
|
||||
String? getConfigOrNull(String key, {String? defaultValue}) =>
|
||||
SystemConfig.getOrNull(key, defaultValue: defaultValue);
|
||||
|
||||
/// get config with non-null from system config
|
||||
String getConfig(String key, {String? defaultValue}) =>
|
||||
SystemConfig.get(key, defaultValue: defaultValue);
|
||||
|
||||
/// check has config key or not from system config
|
||||
bool hasConfigkey(String key) => SystemConfig.containsKey(key);
|
||||
|
||||
/// set config into memory
|
||||
void setConfig(String key, String? value) => SystemConfig.set(key, value);
|
||||
11
lib/src/configurable/configuration_provider.dart
Normal file
11
lib/src/configurable/configuration_provider.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
/// Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
abstract class ConfigurationProvider {
|
||||
/// check has key or not in implemented classes
|
||||
bool containsKey(String key);
|
||||
|
||||
/// get or null in implemented classes
|
||||
String? getOrNull(String key, {String? defaultValue});
|
||||
}
|
||||
19
lib/src/configurable/mutable_configurable_provider.dart
Normal file
19
lib/src/configurable/mutable_configurable_provider.dart
Normal file
@@ -0,0 +1,19 @@
|
||||
import 'configuration_provider.dart';
|
||||
|
||||
/// Mutable Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.3
|
||||
abstract class MutableConfigurationProvider implements ConfigurationProvider {
|
||||
/// Allow to set the value into config values
|
||||
void set(String key, String? value);
|
||||
|
||||
/// Allow to set all values into config values
|
||||
void setAll(Map<String, String?> configs);
|
||||
|
||||
/// Allow to remove the key from config values
|
||||
void remove(String key);
|
||||
|
||||
/// Allow to remove all values from config values
|
||||
void removeAll();
|
||||
}
|
||||
47
lib/src/configurable/simple_configuration_provider.dart
Normal file
47
lib/src/configurable/simple_configuration_provider.dart
Normal file
@@ -0,0 +1,47 @@
|
||||
import 'mutable_configurable_provider.dart';
|
||||
|
||||
/// Simple Configuration Provider
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
class SimpleConfigurationProvider implements MutableConfigurationProvider {
|
||||
final Map<String, String?> _configs = {};
|
||||
|
||||
@override
|
||||
String? getOrNull(String key, {String? defaultValue}) {
|
||||
var value = _configs[key];
|
||||
|
||||
/// if value is null, then set the default value and return it back
|
||||
if (value == null) {
|
||||
_configs[key] = defaultValue;
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
@override
|
||||
bool containsKey(String key) {
|
||||
return _configs.containsKey(key);
|
||||
}
|
||||
|
||||
@override
|
||||
void set(String key, String? value) {
|
||||
_configs[key] = value;
|
||||
}
|
||||
|
||||
@override
|
||||
void remove(String key) {
|
||||
_configs.remove(key);
|
||||
}
|
||||
|
||||
@override
|
||||
void removeAll() {
|
||||
_configs.clear();
|
||||
}
|
||||
|
||||
@override
|
||||
void setAll(Map<String, String?> configs) {
|
||||
_configs.addAll(configs);
|
||||
}
|
||||
}
|
||||
52
lib/src/configurable/system_config.dart
Normal file
52
lib/src/configurable/system_config.dart
Normal file
@@ -0,0 +1,52 @@
|
||||
import 'configuration_provider.dart';
|
||||
import 'mutable_configurable_provider.dart';
|
||||
import 'simple_configuration_provider.dart';
|
||||
|
||||
/// System Configuration Static Functions
|
||||
///
|
||||
/// @author sombochea
|
||||
/// @since 1.0.0
|
||||
class SystemConfig {
|
||||
static ConfigurationProvider? _provider;
|
||||
|
||||
/// Set configuration provider
|
||||
static void setProvider(ConfigurationProvider provider) {
|
||||
_provider = provider;
|
||||
}
|
||||
|
||||
static ConfigurationProvider getProvider() {
|
||||
if (_provider == null) {
|
||||
setProvider(SimpleConfigurationProvider());
|
||||
}
|
||||
|
||||
return _provider!;
|
||||
}
|
||||
|
||||
static String get(String key, {String? defaultValue}) {
|
||||
return getOrNull(key, defaultValue: defaultValue)!;
|
||||
}
|
||||
|
||||
static String? getOrNull(String key, {String? defaultValue}) {
|
||||
return getProvider().getOrNull(key, defaultValue: defaultValue);
|
||||
}
|
||||
|
||||
static bool containsKey(String key) => getProvider().containsKey(key);
|
||||
|
||||
/// Convert to mutable configuration provider
|
||||
static MutableConfigurationProvider _getMutableConfigurationProvider() {
|
||||
if (_provider is MutableConfigurationProvider) {
|
||||
return (_provider as MutableConfigurationProvider);
|
||||
} else {
|
||||
throw Exception(
|
||||
'Error cannot use mutable functions without mutable configuration provider!');
|
||||
}
|
||||
}
|
||||
|
||||
static void set(String key, String? value) {
|
||||
_getMutableConfigurationProvider().set(key, value);
|
||||
}
|
||||
|
||||
static void setAll(Map<String, String?> values) {
|
||||
_getMutableConfigurationProvider().setAll(values);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +0,0 @@
|
||||
// TODO: Put public facing types in this file.
|
||||
|
||||
/// Checks if you are awesome. Spoiler: you are.
|
||||
class Awesome {
|
||||
bool get isAwesome => true;
|
||||
}
|
||||
Reference in New Issue
Block a user