This library provides configurational files system, that alows You to combine multiple files and multiple formats into one object and accessing to it from simple array DOT notation.
- Merging multiple files into one object
- Imports files in multiple formats in the same way: PHP, INI, YAML
- Import files from another files
- Simple dot notation for accessing arrays
- Cache system, for save parsed files into one
{
"require": {
"requtize/config": "^1.2.0"
}
}
composer require requtize/config:^1.2.0
Dot notation is created for accessing multidimentional arrays (PHP) in simple way. If we want to access to some value, we need separate each index by dot. Following code should to explain that:
$array = [
'one' => [
'two' => [
'three' => 'value'
]
]
];
// PHP access
$array['one']['two']['three'];
// Dot notation access
$config->get('one.two.three');
// Without cache system
$config = new Config();
// With cache system
$config = new Config('config-filepath.php');
// Import files - multiple formats in the same way
$config->import('filename.php');
$config->import('filename.ini');
$config->import('filename.yaml');
// Or import as array
$config->import([ 'filename.php', 'filename.ini', 'filename.yaml' ]);
// Get value - each index in multidimensional array separate by dot
// Default value will be returned if given key(s) will not be existed
$value = $config->get('some.deep.index.in-config.file', 'default value');
// Set value in current request live (and in cache).
$config->set('key', 'some value to save');
// Key exists?
$config->has('key') ? 'yes' : 'no';
// Get all data in configs (in all files)
$config->all();
If you want to import other files, You musn't write any PHP to do this. Just use imports.files index in any file, and type files names in each index You want to import. Importing can be used for imports other formats in the same way. Remember that the files are searched relative to file you place importing rules!
return [
// Other data...
'imports' => [
'filepath.php',
'../../global-config/main.ini',
'./some-yaml.file.yaml'
]
// Other data...
];
# Other data...
imports:
"filepath.php"
"../../global-config/main.ini"
"./some-yaml.file.yaml"
# Other data...
; Other data...
[imports]
0 = "filepath.php"
1 = "../../global-config/main.ini"
2 = "./some-yaml.file.yaml"
; Other data...
- Prefixes for imported files to allow import files from different place: '%root%/config.file.php', '%server-config%/some.file.ini'
- XML files support
- You want some more...?
This code is licensed under MIT License.