2017 © Pedro Peláez
 

library config-loader

Configuration loader for YAML, TOML and JSON files

image

yosymfony/config-loader

Configuration loader for YAML, TOML and JSON files

  • Sunday, December 4, 2016
  • by yosymfony
  • Repository
  • 1 Watchers
  • 5 Stars
  • 34,196 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 6 Forks
  • 0 Open issues
  • 7 Versions
  • 9 % Grown

The README.md

Config loader for PHP

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

Build Status Latest Stable Version Scrutinizer Code Quality, (*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)

  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:, (*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)

The Versions

04/12 2016

dev-master

9999999-dev

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml

26/10 2016

v1.3.2

1.3.2.0

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml

05/12 2015

v1.3.1

1.3.1.0

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml

23/07 2015

v1.3.0

1.3.0.0

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml

28/01 2015

v1.2.0

1.2.0.0

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml

07/01 2015

v1.1.0

1.1.0.0

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml

19/07 2014

v1.0.0

1.0.0.0

Configuration loader for YAML, TOML and JSON files

  Sources   Download

MIT

The Requires

 

The Development Requires

json configuration config toml yaml