2017 © Pedro Peláez
 

library config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

image

simplecomplex/config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  • Sunday, July 29, 2018
  • by jacobfriis
  • Repository
  • 2 Watchers
  • 0 Stars
  • 84 Installations
  • PHP
  • 2 Dependents
  • 2 Suggesters
  • 0 Forks
  • 0 Open issues
  • 17 Versions
  • 91 % Grown

The README.md

Config

Simple and sectioned configuration interfaces

ConfigInterface
is basically PSR-16 Simple Cache without time-to-live.
There's a key and a value., (*1)

That principle isn't optimal for general use, unless you like real long and complex key names., (*2)

SectionedConfigInterface
Splits the item key into 'section' plus 'key', facilitating a more organized structure., (*3)

The immediate benefit is that you can namespace all items that belong to a particular library., (*4)

The section-key pattern can be implemented and utilized in two manners:, (*5)

  • concatenation: the configuration setters and getters simply concatenate section and key
  • listing: the section is a list of keys, you can access the whole section as well as a particular key

SectionedConfigInterface also allows for short-term keeping items in memory, via methods remember() and forget()., (*6)

Implementations

Environment variable based

EnvConfig
is a simple abstraction of server environment variables., (*7)

EnvSectionedConfig
a sectioned implementation, using concatenation., (*8)

Ini-files as source, cache as store

The ini-file based classes parse ini-files, and save to cache stores., (*9)

They read recursively from their ini-file paths. That allows one to clone and use ini-files from multiple version control repositories., (*10)

Ini-files are so old-school..., (*11)

Aye, but the ini format is less error-prone than JSON, YAML, what-not.
The syntax is so simple it's hard to make mistakes. And operations people are used to ini-files., (*12)

Cache layer

is SimpleComplex Cache PersistentFileCache.
Cache store names are prefixed with 'config.'
Beware of conflict; do not prefix other cache stores that way., (*13)

Types of ini-based configuration

IniConfig
is not sectioned. Simple but probably not that useful.
$value = $config->get('key'), (*14)

IniSectionedConfig
is a powerful general usage implementation.
$value = $config->get('section', 'key'), (*15)

Reads ini-files from a base path and an override path.
Keep development/production invariant variables (ini-files) in the base path.
Let operations keep production variables in the override path., (*16)

Using the list-principle - and fully supporting remember() and forget() - IniSectionedConfig is optimal for accessing many/all keys of a section within a limited procedure., (*17)

IniSectionedFlatConfig
a sectioned implementation, using concatenation., (*18)

Optimal for types of configuration one expects to access keys of diverse sections in an unpredictable manner, but still want the organisational benefit of sections; many but exact cache reads.
SimpleComplex Locale uses this config class for localized texts., (*19)

Abstraction

The Config class is an abstraction of sectioned configuration., (*20)

In this package it extends IniSectionedConfig.
In an extending package it could be some other sectioned config implementation., (*21)

CLI interface

CliConfig delivers CLI commands for setting, getting and deleting config items.
And commands for refreshing and exporting full configuration stores., (*22)

It exposes IniSectionedConfig instances, via the Config class.
The other config classes are not accessible via CLI., (*23)

Global config

Config defaults to deliver an instance named 'global'., (*24)

A typical system could probably benefit from a single config instance for the bulk of items.
Since the whole thing runtime is cache based, there's no performance reason for using multiple instances., (*25)

Dependency injection container ID: config

Recommendation: access (and thus instantiate) the global config via DI container ID 'config'.
See SimpleComplex Utils Dependency., (*26)

Example

// Bootstrap.
Dependency::genericSetMultiple([
    'cache-broker' => function () {
        return new \SimpleComplex\Cache\CacheBroker();
    },
    'config' => function() {
        return new \SimpleComplex\Config\Config('global');
    },
]);
// ...
// Use.
/** @var \Psr\Container\ContainerInterface $container */
$container = Dependency::container();
/**
 * Create or re-initialize the 'global' config store;
 * based on ini-files placed in base and override paths,
 * cached by a PSR-16 Simple Cache cache store.
 *
 * @var \SimpleComplex\Config\IniSectionedConfig $config
 */
$config = $container->get('config');
/** @var mixed $whatever */
$whatever = $config->get('some-section', 'some-key', 'the default value');

CLI commands

# List all config commands and their help.
php cli.php config -h
# One command's help.
php cli.php config-xxx -h

# List existing config stores.
php cli.php config-list-stores

# Display/get value of a config item.
php cli.php config-get store section key

# Set a config item.
php cli.php config-set store section key value

# Delete a config item.
php cli.php config-delete store section key

# Refresh a config store from .ini file sources.
# The fresh store gets applied atomically, when fully built.
php cli.php config-refresh store

# Export a config store as JSON to a file.
php cli.php config-export store target-path-and-file

Installation

Create a 'conf' directory alongside the document root dir., (*27)

Like:
/var/www/my-host/http
/var/www/my-host/conf, (*28)

Create 'base' and 'override' paths within the 'conf', like:
conf/ini/base
conf/ini/override, (*29)

For the 'global' config store, place or symlink or git clone your system's
.ini configuration files under the 'base' and 'override' paths
using file extension global.ini (= [store name].ini)., (*30)

Like:
conf/ini/base/some-ding.global.ini
conf/ini/override/some-ding.prod.global.ini, (*31)

If that directory structure isn't suitable, do either: - supply Config constructor with a 'paths' argument - extend Config and override it's class constant PATH_DEFAULTS, (*32)

Requirements

Suggestions

The Versions

29/07 2018

dev-develop

dev-develop https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

29/07 2018

dev-master

9999999-dev https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

29/07 2018

2.1

2.1.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

28/07 2018

dev-cli-hook

dev-cli-hook https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

25/06 2018

2.0.1

2.0.1.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

24/06 2018

2.0

2.0.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

24/06 2018

dev-feature-discovery-mode

dev-feature-discovery-mode https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

19/06 2018

1.x-dev

1.9999999.9999999.9999999-dev https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

19/06 2018

1.4

1.4.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

16/06 2018

1.3.2

1.3.2.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

01/05 2018

1.3.1

1.3.1.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

07/04 2018

1.3

1.3.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

23/10 2017

dev-feature-js-html-include

dev-feature-js-html-include https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

10/10 2017

1.2

1.2.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

03/10 2017

1.1.1

1.1.1.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

24/09 2017

1.1

1.1.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache

13/07 2017

1.0

1.0.0.0 https://github.com/simplecomplex/php-config

Simple Cache like configuration interfaces; ini-files as source, cache as store.

  Sources   Download

MIT

The Requires

 

by Jacob Friis Mathiasen

php configuration psr-16 simple cache