Wallogit.com
2017 © Pedro Peláez
A mini config library
mini-config takes a list of files and directories, and becomes an ArrayAccess object with the parsed config data., (*1)
Currently supports JSON*, INI, PHP arrays, and XML** out of the box., (*2)
**JSON must be in the following format, though you can register a custom JSON parser to override this:*, (*3)
{
"cat1": {
"key1": "value1",
"key2: "value2"
},
"cat2": {
"key1": 0
}
}
**XML must be in the following format, though you can register a custom XML parser to override this:, (*4)
<root>
<key>value</key>
<cat>
<key>value</key>
</cat>
</root>
Documentation is available at: http://abreksa4.github.io/mini-config-docs/, (*5)
mini-config merges the config data recursively. (Meaning that if two sources (files) share keys, the values will be merged as an array as well.), (*6)
Add this to your composer.json, (*7)
{
"require": {
"abreksa4/mini-config": "0.2"
}
}
Create a new Config instance, passing in the optional $options array, which currently supports the keys 'targets' which should contain an array of targets, and 'handlers' an array of handlers in the format [$extension => $handler]., (*8)
$config = new Config([
'targets' => [
'module/config.xml',
'config',
]
],
'handlers' => array(
'yml' => function ($file) { return yaml_parse_file($file); }
)
));
At this point the $config object is up and running, with the data from the files in config, and in module/config.xml.
(Note, we'd need to have the YAML PHP extension installed to use yaml_parse.), (*9)
We can add more targets by calling addTarget.
As you can see we can either add an array of targets or just one., (*10)
$config->addTarget('/anothermodule/config');
$config->addTarget(['config_ini', '../config/local']);
You can register a custom handler for any file extension. For example:, (*11)
$config->registerHandler(['yml'],
function($file){
return yaml_parse_file($file);
}
);
Notice we can register an array of extensions to one handler, we can also specify a single extension as a string. Extensions are case-sensitive., (*12)
$config->merge([
'cat3'=> [
'key1' => 'value1';
]
]);
Instead of re-scanning and importing all the data every time we add a target, we call the refresh() method to re-import the data:, (*13)
$config->refresh();
We can access the data by treating the $config object as an array, i.e., (*14)
$config['database']['password'];