Giter Site home page Giter Site logo

config's Introduction

Config

Latest version Software License Build Status Coverage Status Quality Score Total Downloads Gitter

Config is a file configuration loader that supports PHP, INI, XML, JSON, and YML files.

Requirements

Config requires PHP 5.3+, and suggests using the Symfony Yaml component.

Installation

The supported way of installing Config is via Composer.

$ 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.

Loading files

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

// 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']);

Files are parsed and loaded depending on the file extension. Note that when loading multiple files, entries with duplicate keys will take on the value from the last loaded file.

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

Getting values

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

// 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 other method, is by using it like an array:

// 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'];

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.

$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');

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:

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

Examples of supported configuration files

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

Testing

$ phpunit

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

License

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

config's People

Contributors

cideveloper avatar davidepastore avatar garoevans avatar hannesvdvreken avatar hassankhan avatar mackenza avatar mre avatar noodlehaus avatar wenschk avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.