duyler / config
Duyler config file loader
Installs: 869
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/duyler/config
Requires
- php: ^8.4
- vlucas/phpdotenv: ^5.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.77
- phpunit/phpunit: ^10.0
- vimeo/psalm: ^6.13
README
Duyler Config
Description
Duyler Config is a powerful and flexible configuration management library for PHP applications. It provides a simple and intuitive way to handle configuration files and environment variables in your projects.
Features
- Recursive configuration file loading
- Environment variables support with .env files
- Configuration caching for production environments
- Type-safe configuration values with typed getters
- Circular dependency detection
- Extensible through interfaces
- Clean architecture with SOLID principles
- PHP 8.4+ support
Installation
composer require duyler/config
Basic Usage
use Duyler\Config\FileConfig; // Initialize config $config = new FileConfig( configDir: 'config', rootFile: 'composer.json' ); // Get configuration value $value = $config->get('app', 'name', 'default'); // Check if config key exists if ($config->has('app', 'name')) { // ... } // Get all values from config file $allConfig = $config->all('app'); // Type-safe getters $timeout = $config->getInt('app', 'timeout', 30); $debug = $config->getBool('app', 'debug', false); $name = $config->getString('app', 'name', 'MyApp'); $servers = $config->getArray('app', 'servers', []); // Get environment variable $env = $config->env('APP_ENV', 'production'); // Get project path $path = $config->path('storage/logs');
Advanced Usage
Configuration Caching
For production environments, you can enable configuration caching:
$config = new FileConfig( configDir: 'config', rootFile: 'composer.json', cacheDir: 'var/cache', useCache: true ); // Warm up cache (optional, useful for deployment) $config->warmup(); // Clear cache when needed $config->clearCache();
Cross-Config References
Configuration files can reference values from other configs:
// config/app.php return [ 'name' => 'MyApp', 'database' => $config->get('database', 'default', 'mysql'), ]; // config/database.php return [ 'default' => 'postgresql', 'connections' => [ 'mysql' => ['host' => 'localhost'], 'postgresql' => ['host' => 'localhost'], ], ];
The library automatically handles circular dependencies.
Requirements
- PHP 8.4 or higher
- Composer
Architecture
The library follows clean architecture principles and SOLID design:
- ProjectRootFinder - Locates project root directory
- ConfigFileLoader - Loads configuration files from filesystem
- EnvironmentResolver - Handles environment variables
- ConfigCache - Manages configuration caching
- FileConfig - Main facade for configuration access
License
This project is licensed under the MIT License - see the LICENSE file for details.