ConfigSystem class

The default configuration system used in Khadem.

The ConfigSystem provides a hierarchical configuration management system that supports loading from JSON files, environment-specific overrides, dot notation access, and automatic cache management. It's designed to be flexible and performant for various application configurations.

Features

  • Hierarchical Loading: Base configs + environment-specific overrides
  • Dot Notation Access: Access nested values with app.database.host
  • Multiple Formats: JSON support (YAML planned)
  • Caching: In-memory caching with TTL-based expiration
  • Runtime Overrides: Dynamic configuration updates
  • Type Safety: Generic type support for value retrieval

Directory Structure

config/
├── app.json              # Base application config
├── database.json         # Base database config
└── development/          # Environment-specific overrides
    ├── app.json         # Development app overrides
    └── database.json    # Development database overrides

Usage

// Create configuration system
final config = ConfigSystem(
  configPath: 'config',
  environment: 'development',
  useCache: true,
  cacheTtl: Duration(minutes: 5),
);

// Access values with dot notation
final appName = config.get<String>('app.name');
final dbHost = config.get<String>('database.host', 'localhost');
final debug = config.get<bool>('app.debug', false);
final port = config.get<int>('app.port', 3000);

// Set runtime values
config.set('app.runtime_key', 'runtime_value');

// Check if key exists
if (config.has('database.url')) {
  // Database is configured
}

// Get entire sections
final appConfig = config.section('app');
final allConfig = config.all();
Implemented types

Constructors

ConfigSystem({required String configPath, required String environment, bool useCache = true, Duration cacheTtl = const Duration(minutes: 5)})
Creates a new configuration system.

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

all() Map<String, dynamic>
Returns a copy of all configuration data.
override
get<T>(String key, [T? defaultValue]) → T?
Retrieves a configuration value using dot notation.
override
getOrFail<T>(String key) → T
Returns the value of the key or throws an exception if not found.
override
has(String key) bool
Checks if a configuration key exists.
override
loadFromRegistry(Map<String, Map<String, dynamic>> registry) → void
Loads configuration data from a registry map.
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pop(String key) → void
Restores the previous value of a configuration key.
override
push(String key, dynamic value) → void
Temporarily overrides a configuration value. Useful for testing.
override
reload() → void
Reloads all configuration files from disk.
override
section(String name) Map<String, dynamic>?
Returns a specific configuration section.
override
set(String key, dynamic value) → void
Sets a configuration value using dot notation.
override
setEnvironment(String environment) → void
Changes the current environment and reloads configurations.
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited