2021-03-24 13:24:40 +07:00
|
|
|
import 'package:configurable/configuration_provider.dart';
|
2021-03-24 16:39:10 +07:00
|
|
|
import 'package:configurable/mutable_configurable_provider.dart';
|
2021-03-24 13:24:40 +07:00
|
|
|
import 'package:configurable/simple_configuration_provider.dart';
|
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
/// System Configuration Static Functions
|
|
|
|
///
|
|
|
|
/// @author sombochea
|
|
|
|
/// @since 1.0.0
|
2021-03-24 13:24:40 +07:00
|
|
|
class SystemConfig {
|
2021-03-24 16:39:10 +07:00
|
|
|
static ConfigurationProvider? _provider;
|
2021-03-24 13:24:40 +07:00
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
/// Set configuration provider
|
2021-03-24 13:24:40 +07:00
|
|
|
static void setProvider(ConfigurationProvider provider) {
|
2021-03-24 16:39:10 +07:00
|
|
|
_provider = provider;
|
2021-03-24 13:24:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static ConfigurationProvider getProvider() {
|
2021-03-24 16:39:10 +07:00
|
|
|
if (_provider == null) {
|
2021-03-24 13:24:40 +07:00
|
|
|
setProvider(SimpleConfigurationProvider());
|
|
|
|
}
|
|
|
|
|
2021-03-24 16:39:10 +07:00
|
|
|
return _provider!;
|
2021-03-24 13:24:40 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
static String get(String key, {String? defaultValue}) {
|
|
|
|
return getOrNull(key, defaultValue: defaultValue)!;
|
|
|
|
}
|
|
|
|
|
|
|
|
static String? getOrNull(String key, {String? defaultValue}) {
|
2021-03-24 13:34:03 +07:00
|
|
|
return getProvider().getOrNull(key, defaultValue: defaultValue);
|
2021-03-24 13:24:40 +07:00
|
|
|
}
|
2021-03-24 14:10:25 +07:00
|
|
|
|
|
|
|
static bool containsKey(String key) => getProvider().containsKey(key);
|
2021-03-24 16:39:10 +07:00
|
|
|
|
|
|
|
/// 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);
|
|
|
|
}
|
2021-03-24 13:24:40 +07:00
|
|
|
}
|