nitier / config-loader
There is no license information available for the latest version (0.9.9) of this package.
0.9.9
2025-03-24 21:50 UTC
Requires
- symfony/yaml: ^7.2
README
A flexible configuration loader for PHP projects with support for multiple formats and easy extensibility.
📦 Installation
composer require nitier/config-loader
🌟 Features
- Supports multiple formats:
.php
,.json
,.env
- Deep configuration merging
- Easy integration of custom loaders
- Automatic type conversion for
.env
files - PSR-4 compatible
🚀 Quick Start
Basic Usage
use Nitier\ConfigLoader\Config; // Load a single configuration file $config = Config::load('/path/to/config.php'); // Load multiple files with overrides $config = Config::load([ '/path/to/base.json', '/path/to/override.env' ]);
Example .env
File
APP_DEBUG=true DB_HOST=localhost DB_PORT=3306
🔌 Available Loaders
Format | Class | Supported Extensions |
---|---|---|
PHP | PhpLoader |
.php , .inc |
JSON | JsonLoader |
.json |
ENV | EnvLoader |
.env |
🛠 Extending Functionality
Adding a Custom Loader
- Create a class implementing
LoaderInterface
:
namespace App\ConfigLoaders; use Nitier\ConfigLoader\Interface\LoaderInterface; class XmlLoader implements LoaderInterface { public static function supports(string $extension): bool { return $extension === 'xml'; } public static function load(string $path): array { // Your XML parsing implementation here } }
- Register the loader:
Config::addLoader(\App\ConfigLoaders\XmlLoader::class);
⚙️ Configuration
Loader Priorities
Loaders are checked in the order they were registered. The most recently added loaders take priority.
Deep Merging
When loading multiple configuration files, arrays are merged recursively:
// base.php ['db' => ['host' => 'localhost', 'user'=> 'test']] // override.php ['db' => ['user'=> 'prod', 'port' => 3306]] // Result ['db' => ['host' => 'localhost', 'user'=> 'prod', 'port' => 3306]]
📜 License
MIT