2017 © Pedro Peláez
 

library charcoal-config

Charcoal component for configuration data and object modeling

image

locomotivemtl/charcoal-config

Charcoal component for configuration data and object modeling

  • Tuesday, June 5, 2018
  • by mducharme
  • Repository
  • 10 Watchers
  • 0 Stars
  • 14,126 Installations
  • PHP
  • 13 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 19 Versions
  • 7 % Grown

The README.md

Charcoal Config

![License][badge-license] Latest Stable Version ![Code Quality][badge-scrutinizer] Coverage Status ![SensioLabs Insight][badge-sensiolabs] Build Status, (*1)

A Charcoal component for organizing configuration data and designing object data models., (*2)

This component is the glue for much of the Charcoal framework., (*3)

Table of Contents

Installation

The preferred (and only supported) method is with Composer:, (*4)

$ composer require locomotivemtl/charcoal-config

Requirements

PSR

Entity & Config

The Config component simplifies access to object data. It provides a property-based user interface for retrieving and storing arbitrary data within application code. Data is organized into two primary object types: Entity and Config., (*5)

Entity

Entities represent simple data-object containers designed as a flexible foundation for domain model objects.
Examples: a single result from a repository or serve as the basis for each component of an MVC system., (*6)

Config

Configs are advanced Entities designed for runtime configuration values with support for loading files and storing hierarchical data.
Examples: application preferences, service options, and factory settings., (*7)

  • Class: Charcoal\Config\AbstractConfig
    • IteratorAggregate
    • Psr\Container\ContainerInterface
  • Methods: defaults, merge, addFile
  • Interface: Charcoal\Config\ConfigInterface
    • Charcoal\Config\EntityInterface
    • Charcoal\Config\FileAwareInterface
    • Charcoal\Config\SeparatorAwareInterface
    • Charcoal\Config\DelegatesAwareInterface

Features

File Loader

The Config container currently supports four file formats: INI, JSON, PHP, and YAML., (*8)

A configuration file can be imported into a Config object via the addFile($path) method, or by direct instantiation:, (*9)

use Charcoal\Config\GenericConfig as Config;

$cfg = new Config('config.json');
$cfg->addFile('config.yml');

The file's extension will be used to determine how to import the file. The file will be parsed and, if its an array, will be merged into the container., (*10)

If you want to load a configuration file without adding its content to the Config, use loadFile($path) instead. The file will be parsed and returned regardless if its an array., (*11)

$data = $cfg->loadFile('config.php');

Check out the documentation and examples for more information., (*12)

Key Separator Lookup

It is possible to lookup, retrieve, assign, or merge values in multi-dimensional arrays using key separators., (*13)

In Config objects, the default separator is the period character (.). The token can be retrieved with the separator() method and customized using the setSeparator() method., (*14)

use Charcoal\Config\GenericConfig as Config;

$cfg = new Config();
$cfg->setSeparator('/');
$cfg->setData([
    'database' => [
        'params' => [
            'name' => 'mydb',
            'user' => 'myname',
            'pass' => 'secret',
        ]
    ]
]);

echo $cfg['database/params/name']; // "mydb"

Check out the documentation for more information., (*15)

Delegates Lookup

Delegates allow several objects to share values and act as fallbacks when the current object cannot resolve a given data key., (*16)

In Config objects, delegate objects are regsitered to an internal stack. If a data key cannot be resolved, the Config iterates over each delegate in the stack and stops on the first match containing a value that is not NULL., (*17)

use Charcoal\Config\GenericConfig as Config;

$cfg = new Config([
    'driver' => null,
    'host'   => 'localhost',
]);
$delegate = new Config([
    'driver' => 'pdo_mysql',
    'host'   => 'example.com',
    'port'   => 11211,
]);

$cfg->addDelegate($delegate);

echo $cfg['driver']; // "pdo_mysql"
echo $cfg['host']; // "localhost"
echo $cfg['port']; // 11211

Check out the documentation for more information., (*18)

Array Access

The Entity object implements the ArrayAccess interface and therefore can be used with array style:, (*19)

$cfg = new \Charcoal\Config\GenericConfig();

// Assigns a value to "foobar"
$cfg['foobar'] = 42;

// Returns 42
echo $cfg['foobar'];

// Returns TRUE
isset($cfg['foobar']);

// Returns FALSE
isset($cfg['xyzzy']);

// Invalidates the "foobar" key
unset($cfg['foobar']);

👉 A data key MUST be a string otherwise InvalidArgumentException is thrown., (*20)

Interoperability

The Config object implements PSR-11: Psr\Container\ContainerInterface., (*21)

This interface exposes two methods: get() and has(). These methods are implemented by the Entity object as aliases of ArrayAccess::offsetGet() and ArrayAccess::offsetExists()., (*22)

$config = new \Charcoal\Config\GenericConfig([
    'foobar' => 42
]);

// Returns 42
$config->get('foobar');

// Returns TRUE
$config->has('foobar');

// Returns FALSE
$config->has('xyzzy');

👉 A call to the get() method with a non-existing key DOES NOT throw an exception., (*23)

Configurable Objects

Also provided in this package is a Configurable mixin:, (*24)

  • Charcoal\Config\ConfigrableInterface
  • Charcoal\Config\ConfigurableTrait

Configurable objects (which could have been called "Config Aware") can have an associated Config object that can help define various properties, states, or other., (*25)

The Config object can be assigned with setConfig() and retrieved with config()., (*26)

An added benefit of ConfigurableTrait is the createConfig($data) method which is used to create a Config object if one is not assigned. This method can be overridden in sub-classes to customize the instance returned and whatever initial state might be needed., (*27)

Check out the documentation for examples and more information., (*28)

Development

To install the development environment:, (*29)

$ composer install

To run the scripts (phplint, phpcs, and phpunit):, (*30)

$ composer test

API Documentation

Development Dependencies

Coding Style

The charcoal-config module follows the Charcoal coding-style:, (*31)

Coding style validation / enforcement can be performed with composer phpcs. An auto-fixer is also available with composer phpcbf., (*32)

This module should also throw no error when running phpstan analyse -l7 src/ 👍., (*33)

Credits

License

Charcoal is licensed under the MIT license. See LICENSE for details., (*34)

The Versions

05/06 2018

dev-master

9999999-dev https://locomotivemtl.github.io/charcoal-config/

Charcoal component for configuration data and object modeling

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

14/05 2018

dev-mcaskill-patch-xml

dev-mcaskill-patch-xml https://locomotivemtl.github.io/charcoal-config/

Charcoal component for configuration data and object modeling

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

10/05 2018

0.9.0

0.9.0.0 https://locomotivemtl.github.io/charcoal-config/

Charcoal component for configuration data and object modeling

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

30/04 2018

dev-mcaskill-patch-cleanup

dev-mcaskill-patch-cleanup http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

13/04 2018

dev-mcaskill-patch-fileawareness

dev-mcaskill-patch-fileawareness http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

20/11 2017

0.8

0.8.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

17/10 2017

dev-mcaskill-patch-1

dev-mcaskill-patch-1 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

17/10 2017

dev-mcaskill-develop

dev-mcaskill-develop http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

03/02 2017

0.7

0.7.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

29/11 2016

0.6.1

0.6.1.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

json xml configuration ini yaml yml charcoal

11/05 2016

0.6

0.6.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

09/05 2016

0.5.1

0.5.1.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

01/02 2016

0.5

0.5.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

28/01 2016

0.4.1

0.4.1.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

15/01 2016

0.4

0.4.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

15/01 2016

0.3

0.3.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

09/12 2015

0.2

0.2.0.0 http://locomotivemtl.github.io/charcoal-config/

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

 

The Development Requires

by Mathieu Ducharme

02/12 2015

0.1.1

0.1.1.0

Configuration container for all things Charcoal

  Sources   Download

MIT

The Requires

  • ext-json *
  • ext-spl *
  • php >=5.5.0

 

The Development Requires

by Mathieu Ducharme

25/08 2015

v0.1.0

0.1.0.0

Configuration container for all things Charcoal

  Sources   Download

The Requires

  • php >=5.5.0

 

The Development Requires

by Mathieu Ducharme