emargareten/config-php

Simple config manager for PHP

v0.0.1 2023-04-18 09:04 UTC

This package is auto-updated.

Last update: 2024-12-18 12:51:10 UTC


README

Latest Version on Packagist Software License Tests Total Downloads

This package makes it easy to manage configuration settings in your application. It provides a simple and convenient way to set, get, and manipulate configuration values.

Requirements

This package requires PHP 8.0 or later.

Installation

You can install the package via composer:

composer require emargareten/config-php

Usage

Setting the Configuration File Path

Before you can use the package, you must create a configuration file and set the path to it. The configuration file should return an array of configuration values:

<?php

return [
    'key' => 'value',
    'another_key' => 'another_value',
];

Now set the path to your configuration file in your application bootstrap file etc.:

Config::setPath('/path/to/config.php');

Alternatively, you can pass the path as a parameter when you instantiate the Config class for the first time:

$config = new Config('/path/to/config.php');

If you don't want to use a configuration file, you can use the setValues method to set configuration values directly:

Config::setValues([
    'key' => 'value',
    'another_key' => 'another_value',
]);

Using the Configuration

The Config value are static, so you can access them anywhere in your application.

You can the following methods to get, set, and manipulate configuration values: (call these methods statically or on an instance of the Config class)

// Get a value from your configuration
$value = Config::get('key');

// Get a value with a default value if key is not found
$value = Config::get('key', 'default');

// Set a value in your configuration
Config::set('key', 'value');

// Remove a value from your configuration
Config::forget('key');

// Remove all values from your configuration
Config::clear();

// Set multiple values in your configuration
Config::setMany([
    'key1' => 'value1',
    'key2' => 'value2',
]);

// Reset your configuration to its initial state (rereads the config file)
Config::reset();

// Check if a key exists in your configuration
if (Config::has('key')) {
    // ...
}

// Get all values from your configuration
$values = Config::all();

You can also use the config helper function to access the Config class:

config()->get('key'); // or config('key')

config()->set('key', 'value');

// ...

Changelog

Please see CHANGELOG for more information about what has changed recently.

Testing

composer test

Contributing

Contributions are welcome! If you find any bugs or issues or have a feature request, please open a new issue or submit a pull request. Before contributing, please make sure to read the Contributing Guide.

License

The MIT License (MIT). Please see License File for more information.