Welcome to the advanced section of JG\Config! This document delves deeper into the library's powerful features that cater to complex and high-performance applications.
Extend the library to support additional file formats by registering your custom parsers.
use JG\Config\ConfigParserFactory;
// Register a custom parser for `.custom` files
ConfigParserFactory::registerParser('custom', MyCustomParser::class);
To create a custom parser, implement the ParserInterface
:
use JG\Config\Parsers\ParserInterface;
class MyCustomParser implements ParserInterface {
public function parse(string $filePath): array {
// Custom parsing logic
}
}
By default, the library flattens nested configuration keys into dot notation. You can toggle this behavior dynamically.
use JG\Config\Config;
$config = new Config('/path/to/config', false); // Disable flattening
$config->load('nested_config.json');
// Access without flattening
$nested = $config->get('database.credentials');
Leverage robust caching to optimize performance and improve access times.
$config->saveCache('/path/to/cache.json', Config::EXPIRE_ONE_DAY);
if ($config->loadCache('/path/to/cache.json')) {
echo "Loaded from cache!";
}
$config->deleteCache('/path/to/cache.json');
Load configurations directly from streams, such as PSR-7 compatible interfaces.
use Psr\Http\Message\StreamInterface;
$config->loadFromStream($stream);
Identify and resolve configuration issues with informative error messages and detailed exception handling.
use JG\Config\Exceptions\ConfigException;
try {
$config->load('invalid_file.xyz');
} catch (ConfigException $e) {
echo "Error: " . $e->getMessage();
}
Prevent performance issues and improve manageability by controlling the maximum depth for nested configurations.
$config->setMaxDepth(2); // Enforces a depth limit of 2
$config->add('nested', [
'level1' => [
'level2' => [
'level3' => 'value' // This will throw an exception
]
]
]);
Organize configuration data into manageable groups.
$config->add('app.name', 'TestApp');
$config->add('app.version', '1.0.0');
// Access grouped keys
echo $config->get('app.name'); // Outputs: TestApp
$config->delete('app'); // Removes all keys within the `app` group
Designed for high performance, the library supports:
- Efficient caching mechanisms.
- Lazy loading to avoid unnecessary overhead.
- Optimized key-value lookups, even in deeply nested configurations.
The library is built with compatibility in mind:
- Works seamlessly with PSR-7 for stream-based operations.
- Adheres to PSR-12 for coding standards.
If you have ideas to expand these advanced features, check out our Contribution Guidelines for details on how to get involved.