xeriab/konfig

Yet another simple configuration loader library.

0.2.6 2016-10-18 08:39 UTC

This package is not auto-updated.

Last update: 2024-10-26 19:05:27 UTC


README

Yet another simple configuration loader library.

Latest Version Software License Build Status Coverage Status Quality Score Total Downloads

Konfig is a simple configuration loader library that supports INI, JSON, NEON, PHP, TOML, XML, Java-Properties and YML/YAML files.

REQUIREMENTS

Konfig requires PHP 5.6+ and suggests using Yosymfony Toml Parser, Nette NEON and PHP YAML or Symfony YAML.

INSTALLATION

The supported way of installing Konfig is via Composer.

$ composer require xeriab/konfig

USAGE

Konfig is designed with simplicity in mind and it is lightweight and straightforward to use. All you can do with it is load, get, set and delete.

LOADING FILES

The Konfig object can be created via the factory method load(), or by direct instantiation:

use Exen\Konfig\Konfig;

// Load a single file
$config = Konfig::load('konfig.json');
$config = new Konfig('konfig.json');

// Load values from multiple files
$config = new Konfig(['konfig.json', 'konfig.xml']);

// Load all supported files in a directory
$config = new Konfig(__DIR__ . '/konfig');

// Load values from optional files
$config = new Konfig(['konfig.dist.json', 'konfig.json']);

Files are parsed and loaded depending on the file extension.

NOTE: When loading multiple files, entries with duplicate keys will take on the value from the last loaded file.

NOTE: When loading a directory, the path is globed and files are loaded in by name alphabetically.

GETTING VALUES

Getting values can be done in three ways. One, by using the get() method:

// Get value using key
$debug = $config->get('debug');

// Get value using nested key
$secret = $config->get('security.secret');

// Get a value with a fallback
$ttl = $config->get('app.timeout', 3000);

The second method, is by using it like an array:

// Get value using a simple key
$debug = $config['debug'];

// Get value using a nested key
$secret = $config['security.secret'];

// Get nested value like you would from a nested array
$secret = $config['security']['secret'];

The third method, is by using the all() method:

// Get all values
$data = $config->all();

SETTING VALUES

Although Konfig supports setting values via set() or, via the array syntax, any changes made this way are NOT reflected back to the source files. By design, if you need to make changes to your configuration files, you have to do it manually.

$config = Konfig::load('konfig.json');

// Sample value from our konfig file
assert($config['secret'] == '123');

// Update konfig value to something else
$config['secret'] = '456';

// Reload the file
$config = Konfig::load('konfig.json');

// Same value as before
assert($config['secret'] == '123');

// This will fail
assert($config['secret'] == '456');

USING WITH DEFAULT VALUES

Sometimes in your own projects you may want to use Konfig for storing application settings, without needing file I/O. You can do this by extending the AbstractKonfig class and populating the getDefaults() method:

use Exen\Konfig\AbstractKonfig;

class MyKonfig extends AbstractKonfig
{
    protected function getDefaults()
    {
        return [
            'host' => 'localhost',
            'port' => 80,
            'servers' => [
                'host1',
                'host2',
                'host3',
            ],
            'app' => [
                'name' => 'konfig',
                'secret' => 'secret',
            ],
        ];
    }
}

EXAMPLES OF SUPPORTED CONFIGURATION FILES

Examples of simple, valid configuration files can be found here.

CHANGELOG

Please see CHANGELOG for details.

TESTING

$ phpunit

CONTRIBUTING

Please see CONTRIBUTING for details.

CREDITS

CONTRIBUTORS

LICENSE

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