2017 © Pedro Peláez
 

library config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

image

hassankhan/config

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  • Saturday, March 31, 2018
  • by hassankhan
  • Repository
  • 32 Watchers
  • 658 Stars
  • 734,523 Installations
  • PHP
  • 127 Dependents
  • 1 Suggesters
  • 104 Forks
  • 14 Open issues
  • 28 Versions
  • 26 % Grown

The README.md

Config

Latest version ![Software License][ico-license] [Build Status][link-gh-actions] ![Coverage Status][ico-scrutinizer] Quality Score ![Total Downloads][ico-downloads] [Gitter][link-gitter], (*1)

Config is a file configuration loader that supports PHP, INI, XML, JSON, YML, Properties and serialized files and strings., (*2)

Requirements

Config requires PHP 7.4+., (*3)

IMPORTANT: If you want to use YAML files or strings, require the Symfony Yaml component in your composer.json., (*4)

Installation

The supported way of installing Config is via Composer., (*5)

$ composer require hassankhan/config

Usage

Config is designed to be very simple and straightforward to use. All you can do with it is load, get, and set., (*6)

Loading files

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

use Noodlehaus\Config;
use Noodlehaus\Parser\Json;

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

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

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

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

// Load a file using specified parser
$conf = new Config('configuration.config', new Json);

Files are parsed and loaded depending on the file extension or specified parser. If the parser is specified, it will be used for all files. Note that when loading multiple files, entries with duplicate keys will take on the value from the last loaded file., (*8)

When loading a directory, the path is globed and files are loaded in by name alphabetically., (*9)

Warning: Do not include untrusted configuration in PHP format. It could contain and execute malicious code., (*10)

Loading string

Configuration from string can be created via the factory method load() or by direct instantiation, with argument $string set to true:, (*11)

use Noodlehaus\Config;
use Noodlehaus\Parser\Json;
use Noodlehaus\Parser\Yaml;

$settingsJson = <<<FOOBAR
{
  "application": {
    "name": "configuration",
    "secret": "s3cr3t"
  },
  "host": "localhost",
  "port": 80,
  "servers": [
    "host1",
    "host2",
    "host3"
  ]
}
FOOBAR;

$settingsYaml = <<<FOOBAR
application:
    name: configuration
    secret: s3cr3t
host: localhost
port: 80
servers:
- host1
- host2
- host3

FOOBAR;

$conf = Config::load($settingsJson, new Json, true);
$conf = new Config($settingsYaml, new Yaml, true);

Warning: Do not include untrusted configuration in PHP format. It could contain and execute malicious code., (*12)

Getting values

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

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

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

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

The second method, is by using it like an array:, (*14)

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

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

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

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

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

Setting values

Although Config 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., (*16)

$conf = Config::load('config.json');

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

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

// Reload the file
$conf = Config::load('config.json');

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

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

Saving config

It is possible to save the config back to a file in any of the supported formats except PHP., (*17)

$config = Config::load('config.json');

$ini = $config->toString(new Ini()); // Encode to string if you want to save the file yourself

$config->toFile('config.yaml');
$config->toFile('config.txt', new Serialize()); // you can also force the writer

Using with default values

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

class MyConfig extends AbstractConfig
{
    protected function getDefaults()
    {
        return [
            'host' => 'localhost',
            'port'    => 80,
            'servers' => [
                'host1',
                'host2',
                'host3'
            ],
            'application' => [
                'name'   => 'configuration',
                'secret' => 's3cr3t'
            ]
        ];
    }
}

Merging instances

You may want merging multiple Config instances:, (*19)

$conf1 = Config::load('conf1.json');
$conf2 = Config::load('conf2.json');
$conf1->merge($conf2);

Examples of supported configuration files

Examples of simple, valid configuration files can be found here., (*20)

Testing

bash $ phpunit, (*21)

Contributing

Please see CONTRIBUTING for details., (*22)

Security

If you discover any security related issues, please email contact@hassankhan.me instead of using the issue tracker., (*23)

Credits

License

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

The Versions

31/03 2018

dev-develop

dev-develop http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

31/03 2018

dev-master

9999999-dev http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

31/03 2018

1.0.1

1.0.1.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

03/03 2018

1.0.0

1.0.0.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

03/03 2018

dev-add-more-info-about-symfony-yaml

dev-add-more-info-about-symfony-yaml http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

03/03 2018

dev-edit-php-versions-for-build

dev-edit-php-versions-for-build http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

03/03 2018

dev-php-5.5.9.-minimum-required-version

dev-php-5.5.9.-minimum-required-version http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

03/03 2018

dev-fix-php-5.6-test

dev-fix-php-5.6-test http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

07/11 2017

0.11.2

0.11.2.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

07/11 2017

0.11.1

0.11.1.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

07/11 2017

0.11.0

0.11.0.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

11/02 2016

0.10.0

0.10.0.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

23/01 2016

0.9.1

0.9.1.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

23/01 2016

0.9.0

0.9.0.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

22/03 2015

dev-feature/major-changes

dev-feature/major-changes http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

21/03 2015

0.8.2

0.8.2.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

21/03 2015

0.8.1

0.8.1.0 http://hassankhan.me/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

21/03 2015

0.8.0

0.8.0.0 http://noodlehaus.github.io/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

24/02 2015

0.7.1

0.7.1.0 http://noodlehaus.github.io/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

23/02 2015

0.7.0

0.7.0.0 http://noodlehaus.github.io/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

23/02 2015

0.6.0

0.6.0.0 http://noodlehaus.github.io/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

23/02 2015

0.5.0

0.5.0.0 http://noodlehaus.github.io/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

22/02 2015

0.4.0

0.4.0.0 http://noodlehaus.github.io/config/

Lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

22/02 2015

0.3.0

0.3.0.0 http://noodlehaus.github.io/config/

lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

22/02 2015

0.2.1

0.2.1.0 http://noodlehaus.github.io/config/

lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml yml microphp unframework

21/02 2015

0.2.0

0.2.0.0 http://noodlehaus.github.io/config/

lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml microphp unframework

27/11 2014

0.1.0

0.1.0.0 http://noodlehaus.github.io/config/

lightweight configuration file loader that supports PHP, INI, XML, JSON, and YAML files

  Sources   Download

MIT

The Requires

 

The Development Requires

json xml configuration ini config yaml microphp unframework

19/11 2014

0.0.1

0.0.1.0 http://noodlehaus.github.io/config/

configuration file loader that supports json, ini, php or xml hashes

  Sources   Download

MIT

The Requires

  • php >= 5.3.0

 

The Development Requires

json xml configuration ini config microphp unframework