yosymfony/config-loader

Configuration file loader

v2.0.0 2018-09-02 15:36 UTC

This package is auto-updated.

Last update: 2024-10-13 04:15:21 UTC


README

An agnostics configuration loader with built-in loaders for YAML, TOML and JSON.

Build Status Latest Stable Version Scrutinizer Code Quality

Installation

Requires PHP >= 7.2.

Use Composer to install this package:

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:

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:

composer require symfony/yaml

Initialization:

$config = new ConfigLoader([
  new YamlLoader($locator),
]);

Toml loader

Requires: Toml component:

composer require yosymfony/toml

Initialization:

$config = new ConfigLoader([
  new TomlLoader($locator),
]);

Json loader

Initialization:

$config = new ConfigLoader([
  new JsonLoader($locator),
]);

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:

  1. filename.ext
  2. 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:

$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:

---
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"
]

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.

// 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:

$resultC = $repositoryA->union($repositoryB);

The values of $repositoryB have less priority than values in $repositoryA.

Intersections

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

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');

Unit tests

You can run the unit tests with the following command:

$ cd toml
$ composer test

License

This library is open-sourced software licensed under the MIT license.