bright-components/bright-config

A framework agnostic version of illuminate/config

v0.1.2 2017-04-24 04:50 UTC

README

bright-config

bright-components\bright-config

Build Status Scrutinizer Code Quality Code Coverage Code Climate

WIP! Not production ready.

This is a framework agnostic fork of Illuminate\Config. We've stripped away the other Illuminate dependencies and will provide easy setup instructions in vanilla php, as well as Slim, Silex, etc. The current version still uses the Arr class from Illuminate\Support, however I'm currently working on my own implementation of these helpers.

Install

Via Composer is the easiest way to install. Simply:

$ composer require bright-components/bright-config

Prep

Before we begin, bright-config operates under the assumption that you have one config directory, preferrably at the root level, but location doesn't matter, that stores all of your .php config files. For example, see the 'config' directory in the tree below:

src
|
config
|-- app.php
|-- database
|    |-- redis.php
|    +-- mysql.php
|-- services.php
|
public

Each .php file inside the config folder has the following structure:

//app.php
<?php

return [
    'url' => 'http://localhost',
    'debug' => true,
    'connection' => [
        'host' => 'localhost',
        'port' => '5432'
    ]
];

Instantiation

Vanilla PHP

    $configPath = __Dir__.'/../config';
    $repository = new Bright\Config\Repository($configPath);

    $appUrl = $repository->get('app.url');
    $repository->set('database.mysql.host', 'localhost');

Slim

Using Pimple

$container = $app->getContainer();
$container['config'] = function($c) {
    $configPath = __Dir__.'/../config';

    return new Bright\Config\Repository($configPath);
}

PHP-DI

$containerBuilder->addDefinitions([
    Bright\Config\Repository::class => function($c) {
    $configPath = __Dir__.'/../config';

    return new Bright\Config\Repository($configPath);
    }
]);

Usage

Once we have a $repository, we can use the following methods to interact with our configuration.

  • get($key, $default=null)
  • set($key, $value)
  • all()
  • has($key)
  • prepend($key, $value)
  • push($key, $value)

Note: The path provided must be a valid directory path, the directory can't be empty, and the configuration files must be formatted correctly, or an exception will be thrown.

Example using your configuration repository in other container bindings:

$container = $app->getContainer();
$container['redis'] = function($c) {
    $config = $c->get('config');
    $client = new Predis\Client([
        'scheme' => $config->get('redis.scheme'),
        'host'   => $config->get('redis.host'),
        'port'   => $config->get('redis.port'),
    ]);
}

Example using dependency injection via interface: //Todo

Testing

$ vendor/bin/phpunit

Contributing

Please see CONTRIBUTING for details.

Credits

License

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