comphp/config

Library designed to simplify the management of configuration files in PHP applications. It leverages the power of dynamic driver loading and custom behaviors to provide a highly adaptable solution for application configuration.

v0.2 2025-03-16 00:43 UTC

This package is auto-updated.

Last update: 2025-04-16 00:52:20 UTC


README

Overview

The comphp/config library is designed to simplify the management of configuration files in PHP applications. It leverages dynamic driver loading and custom behaviors to provide a highly adaptable solution for application configuration.

Features

  • Supports Multiple Formats: JSON and PHP configurations out of the box.
  • Modular & Extensible: Easily register additional parsers.
  • Dependency Injection Ready: Works seamlessly with PSR-11 containers.
  • Merge Strategies: Replace, merge, or ignore existing configuration values.
  • Dot-Notation Access: Retrieve nested configuration values effortlessly.

Installation

Install via Composer:

composer require comphp/config

Usage

Initialize ConfigManager

use Neuron\Configuration\ConfigManager;
use DI\ContainerBuilder;

$container = (new ContainerBuilder())->build();
$configManager = new ConfigManager($container);

Load Configuration Files

$configManager->load('database', 'config.json');
$configManager->load('app', 'config.php');

Access Configuration Values

echo $configManager->get('database.host', 'default_host');

Modify Configuration

$configManager->set('cache.enabled', true);
$configManager->unset('cache.enabled');

Convert Configuration to Array

print_r($configManager->toArray());

Supported Formats

By default, comphp/config supports:

  • JSON (.json)
  • PHP (.php returning an array)

You can register additional parsers using ParserRegistry.

Extending with Custom Parsers

use Neuron\Configuration\Parsers\JsonParser;
use Neuron\Configuration\ParserRegistry;

$parserRegistry = $configManager->parsers;
$parserRegistry->register(YamlParser::class, 'yaml');
$parserRegistry->register(DatabaseParser::class, 'db')

Merge Strategies

When loading configurations, you can define how new values are merged:

  • MergeMode::Replace (default): Overwrites existing values.
  • MergeMode::Merge: Merges arrays recursively.
  • MergeMode::Ignore: Keeps existing values untouched.
  • MergeMode::Error: Throws an exception if a key already exists.

Example:

use Neuron\Configuration\MergeMode;
$configManager->load('app', 'config.json', MergeMode::Merge);

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

See CONTRIBUTING.md for guidelines on how to contribute.

Changelog

See CHANGELOG.md for a history of updates.