jalet/tvnu-config

Environment based configuration helper for multiple formats. INI, YAML, Databse (PDO) or Array.

Installs: 2 611

Dependents: 0

Stars: 6

Watchers: 3

Language: PHP

1.1.1 2013-08-21 06:13 UTC

README

Environment based configuration helper for multiple formats. INI, YAML, Arrays or Database (PDO.

Build Status

Requirements

  • PHP 5.3+
  • PDO (optional)
  • YAML (optional)

Usage

INI

[production]
foo     = Bar
bar.foo = Hello World
bar.biz = World Hello
biz     = Baz

[development : production]
foo = Foo Dev

[local : development]
bar.biz = Hello Local
// Production
$a = new Tvnu\Config\Adapter\Ini('production', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Bar

// Development
$a = new Tvnu\Config\Adapter\Ini('development', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Foo Dev

// Local
$a = new Tvnu\Config\Adapter\Ini('local', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('bar.biz'); // Hello Local

PHP Array

// path/to/config/config.php
return array(
    'foo' => 'Bar',
    'bar' => array(
        'foo' => "Hello World",
        'biz' => "World Hello",
        'baz' => array(
            'foo' => 'Foobar'
        )
    ),
    'biz' => 'Baz'
);

// path/to/config/config.development.php
return array(
    'foo' => 'Foo Dev',
    'bar' => array(
        'foo' => "Hello World",
        'biz' => "World Hello",
        'baz' => array(
            'foo' => 'Barfoo'
        )
    ),
);

// path/to/config/config.local.php
return array(
    'biz' => array(
        'baz' => 'Hello Local'
    )
);
// Production
$a = new Tvnu\Config\Adapter\PhpArray('production', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Bar

// Development
$a = new Tvnu\Config\Adapter\PhpArray('development', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Foo Dev

// Local
$a = new Tvnu\Config\Adapter\PhpArray('local', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('bar.biz'); // Hello Local

YAML

production:
    foo: Bar
    bar:
        foo: "Hello World"
        biz: "World Hello"
        bar:
            foo: "Hello World Third"
    biz: Baz   

development:
    foo: "Foo Dev"

local:
    bar:
        biz: "Hello Local"
// Production
$a = new Tvnu\Config\Adapter\Yaml('production', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Bar

// Development
$a = new Tvnu\Config\Adapter\Yaml('development', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Foo Dev

// Local
$a = new Tvnu\Config\Adapter\Yaml('local', 'path/to/config'));
$c = new Tvnu\Config\Config($a);

echo $c->get('bar.biz'); // Hello Local

Database (PDO)

    #    |    key    |    val    
-------------------------------------
    1    |    foo    |    Bar    
    2    |    biz    |    Baz    
    3    |  bar.foo  |    Hello World    
    4    |  bar.biz  |    World Hello
// Production
$a = new Tvnu\Config\Adapter\Database($pdoConnection, $myTableName, array('my', 'fields', 'of', 'choice'));
$c = new Tvnu\Config\Config($a);

echo $c->get('foo'); // Bar

Composer

Packagegist

composer.json

{
    "require": {
        "jalet/tvnu-config": "1.1.0"
    }
}

Tests

Automatic testing requires Ruby gem watchr gem install watchr

# Run tests
$ ./runTests

# Run tests with coverage
$ ./runCoverage

ToDo

  • Return array of values

Licence

Copyright (c) 2013 Joakim Jarsäter joakim@jarsater.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.