Git Product home page Git Product logo

alt-brite-config's Introduction

Brite Config

A simple PHP INI (or plain array) configuration class with dot-notation access

  • Parses both INI files and PHP arrays
  • Deals allows for configuration inheritance
  • Available via Composer / Packagist

Usage

You need a configuration file. Example .ini contents:

[default]

database.host = bar
database.user = foo
database.pass = baz

service.api_key = 123456

email = [email protected]

[staging:default]

database.user = foo2
database.pass = baz2

[production:staging]

database.user = foo1
database.pass = baz1
email = [email protected]

Or alternatively, if you prefer plain PHP arrays:

<?php

$config['default']['database']['host'] = 'bar';
$config['default']['database']['user'] = 'foo';
$config['default']['database']['pass'] = 'baz';

$config['default']['service']['api_key'] = '123456';
$config['default']['email'] = '[email protected]';

$config['production']['extends'] = 'staging';
$config['production']['database']['user'] = 'foo1';
$config['production']['database']['pass'] = 'baz1';
$config['production']['email'] = '[email protected]';

$config['staging']['extends'] = 'default';
$config['staging']['database']['user'] = 'foo2';
$config['staging']['database']['pass'] = 'baz2';

If you want to use the 'registry', register your configuration file during bootstrap:

<?php

use Brite\Config\Config;

// Registering as the 'default' config means you can grab the config
// without specifying a name.
Config::register('default', __DIR__ . '/test_config/config.php', 'staging');

Then access your configuration when required:

<?php

use Brite\Config\Config;

// This grabs the config registered as 'default'
$config = Config::instance();

echo $config->get('database.host');
// output: "bar"
echo $config->get('database.user');
// output: "foo1"

Alternatively, if you have multiple configuration files, you may name your configuration something other than 'default' during bootstrap, and access it via:

<?php

use Brite\Config\Config;

// This grabs the config registered as 'database'
$dbConfig = Config::instance('database');

echo $dbConfig->get('host');
echo $dbConfig->get('user');

However, we all know that global access is bad, right? You can create your own instance to contain your configuration, rather than using a global static registry:

<?php

use Brite\Config\IniConfig;

$config = new IniConfig('/path/to/file.ini', 'staging');

// Now register $config with your registry

... and that's it. Simple!

alt-brite-config's People

Contributors

chathurasudarsha avatar

Watchers

 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.