An agnostics configuration loader with built-in loaders for YAML, TOML and JSON.
Requires PHP >= 7.2.
Use Composer to install this package:
composer require yosymfony/config-loader
The class ConfigLoader
let you load your configuration resources. It expects a list of
loaders in the constructor so you can pass to it only those ones you need:
use Yosymfony\ConfigLoader\FileLocator;
use Yosymfony\ConfigLoader\ConfigLoader;
// The file locator uses an array of pre-defined paths to find files:
$locator = new FileLocator(['/path1', '/path2']);
// Set up the ConfigLoader to work with YAML and TOML configuration files:
$config = new ConfigLoader([
new YamlLoader($locator),
new TomlLoader($locator),
]);
Requires: Symfony YAML component:
composer require symfony/yaml
Initialization:
$config = new ConfigLoader([
new YamlLoader($locator),
]);
Requires: Toml component:
composer require yosymfony/toml
Initialization:
$config = new ConfigLoader([
new TomlLoader($locator),
]);
Initialization:
$config = new ConfigLoader([
new JsonLoader($locator),
]);
// Search this file in "path1" and "path2":
$config->load('user.yml');
// or load a file using its absolute path:
$config->load('/var/config/user1.yml');
This library has support for .dist
files. The filename is resolved following the next hierarchy:
- filename.ext
- filename.ext.dist (if
filename.ext
does not exist)
To parse inline configurations you just need to set the configuration text as first argument instead of the filename and set the format type as second one:
$repository = $config->load('server: "your-name.com"', YamlLoader::TYPE);
This library has support for importing files. The example below shows a YAML file importing three files:
---
imports:
- config-imported.yml
- config-imported.toml
- config-imported.json
Similar example using JSON syntax:
{
"imports": [
"config.json"
]
}
An example using TOML syntax:
imports = [
"config.toml"
]
A configuration file is loaded into a repository. A repository is a wrapper that implements the ArrayAccess interface and exposes methods for working with configuration values.
// Returns the value associeted with key "name" or the default value in case not found
$repository->get('name', 'default');
// Do the same that the previous sentence but using array notation
$repository['name'];
You can performs the union of a repository A with another B into C as result:
$resultC = $repositoryA->union($repositoryB);
The values of $repositoryB
have less priority than values in $repositoryA
.
You can performs the intersection of a repository A with another B into C as result:
$resultC = $repositoryA->intersection($repositoryB);
The values of $repositoryB
have less priority than values in $repositoryA
.
Creating a blank repository is too easy. You just need to create a instance of
a Repository
class:
use Yosymfony\Config-loader\Repository;
//...
$repository = new Repository([
'name' => 'Yo! Symfony',
]);
$repository->set('server', 'your-name.com');
You can run the unit tests with the following command:
$ cd toml
$ composer test
This library is open-sourced software licensed under the MIT license.