noccylabs/settings

$ git tag


$ git branch
* master

Settings: Simple configuration management

This library is used to set up configuration management when all you need is a simple configuration file. The usual workflow is like this:

  1. Create a new NoccyLabs\Settings\Settings. This is the collection that holds all the settings and facilitate access to the values.
  2. Either create your NoccyLabs\Settings\Setting manually, or use the #create() method on the Settings object to create your settings and lock the name and type. Descriptions are also useful.
  3. Look for configuration files in the expected locations, use the appropriate FileReader such as the NoccyLabs\Settings\Reader\IniReader to parse ini-files in a git config like format. This will only populate values, and invalid values will throw exceptions.
  4. Save the settings in the desired default location using a FileWriter such as the NoccyLabs\Settings\Writer\IniWriter. This way they can be read back in the next time.
  5. Access your settings using array notation, f.ex. $settings['my.setting'].
  6. Remember to save again if you update any settings that should be persistent.

Example

use NoccyLabs\Settings\Settings;
use NoccyLabs\Settings\Setting;
use NoccyLabs\Settings\Writer\IniWriter;
use NoccyLabs\Settings\Writer\XmlWriter;

$settings = new Settings();

$settings->create("app.greeting", Setting::TYPE_STRING, "Hello")
    ->setDescription("How the app should greet you");

echo $settings['app.greeting']."\n";

$iniwriter = new IniWriter();
$iniwriter->write($settings, "config.ini", true);

$xmlwriter = new XmlWriter();
$xmlwriter->write($settings, "config.xml");

File readers/writers

Examples

With the following setup:

$settings->create("foo.bool", Setting::TYPE_BOOLEAN)->setValue(true);
$settings->create("foo.string", Setting::TYPE_STRING)->setValue("yes");

The following format is used:

ini

[foo]
    bool = true
    string = yes

yaml

foo:
    bool: true
    string: yes

json

{
    "foo.bool": true,
    "foo.string": "yes"
}

conf

foo.bool true
foo.string "yes"

xml

<settings>
    <setting name="foo.bool" type="bool" value="true" />
    <setting name="foo.string" type="string">yes</setting>
</settings>

php

<?php return array(
    "foo.bool" => true,
    "foo.string" => "yes"
);

Roadmap

0.1.x

0.2.x