Config loader for PHP
An agnostics configuration loader with built-in loaders for YAML, TOML and JSON., (*1)
, (*2)
Installation
Requires PHP >= 7.2., (*3)
Use Composer to install this package:, (*4)
composer require yosymfony/config-loader
Usage
Initialization
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:, (*5)
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),
]);
Available loaders
Yaml loader
Requires: Symfony YAML component:
bash
composer require symfony/yaml
, (*6)
Initialization:
php
$config = new ConfigLoader([
new YamlLoader($locator),
]);
, (*7)
Toml loader
Requires: Toml component:
bash
composer require yosymfony/toml
, (*8)
Initialization:
php
$config = new ConfigLoader([
new TomlLoader($locator),
]);
, (*9)
Json loader
Initialization:
php
$config = new ConfigLoader([
new JsonLoader($locator),
]);
, (*10)
Loading configuration files:
// 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');
.dist files
This library has support for .dist
files. The filename is resolved following the next hierarchy:, (*11)
- filename.ext
- filename.ext.dist (if
filename.ext
does not exist)
Loading inline configuration:
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:, (*12)
$repository = $config->load('server: "your-name.com"', YamlLoader::TYPE);
Importing files
This library has support for importing files.
The example below shows a YAML file importing three files:, (*13)
---
imports:
- config-imported.yml
- config-imported.toml
- config-imported.json
Similar example using JSON syntax:, (*14)
{
"imports": [
"config.json"
]
}
An example using TOML syntax:, (*15)
imports = [
"config.toml"
]
Repository
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., (*16)
// 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'];
Operations
Unions
You can performs the union of a repository A with another B into C as result:, (*17)
$resultC = $repositoryA->union($repositoryB);
The values of $repositoryB
have less priority than values in $repositoryA
., (*18)
Intersections
You can performs the intersection of a repository A with another B into C as result:, (*19)
$resultC = $repositoryA->intersection($repositoryB);
The values of $repositoryB
have less priority than values in $repositoryA
., (*20)
Creating a blank repository
Creating a blank repository is too easy. You just need to create a instance of
a Repository
class:, (*21)
use Yosymfony\Config-loader\Repository;
//...
$repository = new Repository([
'name' => 'Yo! Symfony',
]);
$repository->set('server', 'your-name.com');
Unit tests
You can run the unit tests with the following command:, (*22)
$ cd toml
$ composer test
License
This library is open-sourced software licensed under the
MIT license., (*23)