minond/configurare

Configuration management

v2.0.0 2014-02-02 04:57 UTC

This package is not auto-updated.

Last update: 2024-11-04 15:14:35 UTC


README

Build Status Coverage Status Latest Stable Version Dependencies Status Scrutinizer Quality Score SensioLabsInsight

Sample usage

Initializing configuration

use Efficio\Configurare\Configuration;

$conf = new Configuration;
$conf->setDirectory('./config/');
# config/app.yml
name: 'My Application'
usa:
  utah:
    provo:
      author: 'Marcos Minond'

Configuring configuratio file formats and parsers

// available parsers
use Efficio\Configurare\Parser\Json;

$conf->setExtension('.json'); // default is '.yml'
$conf->setParser(new Json); // default is Efficio\Configurare\Parser\Yaml

Using custom formats

use Efficio\Configurare\Parser\Parser;

class CustomParser implements Parser
{
    /**
     * takes a raw string, parses it, and returns the array representing the
     * data
     * @param string $raw
     * @return array
     */
    public function decode($raw)
    {
        return unserialize($raw);
    }

    /**
     * takes an array or an object and converts it into a string that can be
     * saved in a file
     * @param mixed $obj
     * @return string
     */
    public function encode($obj)
    {
        return serialize($obj);
    }
}

$conf->setExtension('.custom');
$conf->setParser(new CustomParser);

Getting values

// looks for in ./config/app.yml
// this gets [ 'name': ]
echo $conf->get('app:name'); // => My Application

// this gets [ 'usa': 'utah': 'provo': 'author': ]
echo $conf->get('app:usa:utah:provo:author'); // => Marcos Minond

// you can also get nested configuration files
// looks in config/users/2014/jan.yml
echo $conf->get('users/2014/jan:activities:music');

Setting values

// if a key(s) already exists, just set it
$conf->set('app:name', 'My Other Application');

// if they do not then the write must be forced by passing a third parameter
// set to true
$conf->set('app:does:not:exists:yet', 'yes', true);
# config/app.yml
name: 'My Other Application'
usa:
  utah:
    provo:
      author: 'Marcos Minond'
does:
  not:
    exists:
      yet: 'yes'

Enviroments

Adding enviroments allows additional configuration files, which may or may not be tracked by version control, to be used. For example, you may commit a "default" config/app.yml configuration file which makes assumptions about the enviroments (ie. database connection information) and overwrite it using config/app.prod.yml. This "prod" file has data which is sensative and is only stored in the production server where your application is running. This allows you to use the same configuration retrieval code, get the correct configuration for your enviroments, AND not have to check that into source control.

// I can have one enviroment or multiple
$conf->setEnvironments([ 'dev', 'test' ]);

// the following files will be parsed and merged before the configuration
// value is sent back
// - config/database.yaml
// - config/database.dev.yaml
// - config/database.test.yaml
$conf->get('database:connection:username');

Caching

Configurare is compatible with the Cache package

use Efficio\Cache\NullCache;
$conf->setCache(new NullCache);