2017 © Pedro Peláez
 

library config

configuration handler for file based configuration

image

theiconic/config

configuration handler for file based configuration

  • Tuesday, June 19, 2018
  • by wyrfel
  • Repository
  • 39 Watchers
  • 2 Stars
  • 4,615 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 1 Open issues
  • 13 Versions
  • 35 % Grown

The README.md

theiconic/config

General purpose config manager for file-based configuration., (*1)

Build Status Coverage Status Scrutinizer Code Quality Latest Stable Version Total Downloads License Dependency Status, (*2)

Setup

```$php composer require theiconic/config, (*3)


## Concepts ### Spaces Configuration is grouped into configuration spaces. Spaces are managed by the configuration factory. ### Multi-file & multi-section configuration A configuration space can read configuration from several files. The configuration is merged and then flattened by section. ### Placeholders During parsing, pre-defined placeholders will be replaced with the configured replacement values. ### Example So the basic setup looks something like this (modified example based on alice):

// get the factory instance $factory = Factory::getInstance();, (*4)

// set the current environment $factory->setEnvironment(APPLICATION_ENV);, (*5)

// configure cache path $factory->setCachePath(sprintf('%s/config', APPLICATION_TMP));, (*6)

// instanciate and configure the application config space $factory->getSpace('application') ->setPaths([ '/etc/iconic.ini', $basePath . 'application.ini', $basePath . 'application.local.ini', ]) ->setSections([ 'default', 'production', ]) ->setPlaceholders([ '%APPLICATION_ENV%' => APPLICATION_ENV, '%APPLICATION_ROOT%' => APPLICATION_ROOT, '%APPLICATION_TMP%' => APPLICATION_TMP, ]);, (*7)


## Multi-section config format Configuration files must contain configuration sections. This is the sections in .ini files and the first level array/object items in .php or .json config files. No sections are pre-selected by default. You will need to explicitly state the sections in code, like so: ```$php $factory->getSpace('myConfig') ->setSections(['main', 'development', 'testing']);

Sections will be merged in the order specified, i.e. entries in later sections will override those in earlier sections., (*8)

Caching

All configuration is parsed into multidimensional PHP arrays. The arrays are then stored in cache files so that expensive parsing is bypassed., (*9)

Cache keys are determined based on - the list of source files names - the list on section names., (*10)

Cache is automatically validated based on file modification timestamps. Hence, the cache will automatically update itself whenever any of the source configuration files changes., (*11)

Parsing

Extendable parsers are used to parse different file formats. Currently implemented parsers are: * Ini (for .ini files) * Json (for .json files) * Php (for .php files) * Autodetect (automatically picks the right parser based on extension) * Dummy (for unit tests etc.), (*12)

Accessing configuration values

Configuration can be accessed via dot-paths. These paths are dynamically resolved against the internal array-representation of configuration. This allows retrieving individual entries as well as collections of entries., (*13)

Factory::getInstance()->getSpace('application')->get('redis.retries', 3);
Factory::getInstance()->getSpace('application')->get('redis.hosts', 'localhost');

Factory::getInstance()->getSpace('application')->get('redis', ['retries': 3, 'hosts': 'localhost]);

You can also retrieve the configuration as a flat array of dot-path to value mappings:, (*14)

Factory::getInstance()->getSpace('application')->flatten();

Using environment variables

There is no explicit functionality to handle environment variables, however they can be dynamically used in the configurations via the placeholders mechanism:, (*15)

$space = Factory::getInstance()->getSpace('application');
foreach ($_ENV as $key => $value) {
    $placeholder = sprintf('%%ENV_%s%%', strtoupper(str_replace('%', '_', $key)));
    $space->addPlaceholder($placeholder, $value);
}

With these few lines in place, a configuration file could look like this:, (*16)

[main]
user.name = %ENV_USER%
user.home = %ENV_HOME%

License

THE ICONIC config library for PHP is released under the MIT License., (*17)

The Versions

19/06 2018

dev-master

9999999-dev

configuration handler for file based configuration

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by TheIconic Engineering

13/04 2018

v1.0.5

1.0.5.0

configuration handler for file based configuration

  Sources   Download

MIT

The Requires

  • php >=7.1

 

The Development Requires

by TheIconic Engineering

29/08 2017

v1.0.4

1.0.4.0

configuration handler for file based configuration

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by TheIconic Engineering

08/06 2017

v1.0.3

1.0.3.0

configuration handler for file based configuration

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by TheIconic Engineering

08/06 2017

v1.0.2

1.0.2.0

configuration handler for file based configuration

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by TheIconic Engineering

26/05 2017

v1.0.1

1.0.1.0

configuration handler for file based configuration

  Sources   Download

MIT

The Requires

  • php >=7.0

 

The Development Requires

by TheIconic Engineering

24/05 2017

v1.0.0

1.0.0.0

configuration handler for file based configuration

  Sources   Download

The Development Requires

by TheIconic Engineering

11/12 2016

v0.2.0

0.2.0.0

more or less generic config module

  Sources   Download

The Development Requires

by TheIconic Engineering

01/08 2016

v0.1.4

0.1.4.0

more or less generic config module

  Sources   Download

The Development Requires

  • mikey179/vfsstream ^1.6

by TheIconic Engineering

07/06 2016

v0.1.3

0.1.3.0

more or less generic config module

  Sources   Download

The Development Requires

  • mikey179/vfsstream ^1.6

by TheIconic Engineering

07/06 2016

v0.1.2

0.1.2.0

more or less generic config module

  Sources   Download

The Development Requires

  • mikey179/vfsstream ^1.6

by TheIconic Engineering

06/06 2016

v0.1.1

0.1.1.0

more or less generic config module

  Sources   Download

The Development Requires

  • mikey179/vfsstream ^1.6

by TheIconic Engineering

06/06 2016

v0.1

0.1.0.0

more or less generic config module

  Sources   Download

The Development Requires

  • mikey179/vfsstream ^1.6

by TheIconic Engineering