2017 © Pedro Peláez
 

library expressive-config-manager

Lightweight library for merging and caching application config

image

mtymek/expressive-config-manager

Lightweight library for merging and caching application config

  • Monday, March 13, 2017
  • by mtymek
  • Repository
  • 8 Watchers
  • 29 Stars
  • 35,950 Installations
  • PHP
  • 9 Dependents
  • 1 Suggesters
  • 4 Forks
  • 2 Open issues
  • 15 Versions
  • 10 % Grown

The README.md

Expressive Configuration Manager

Deprecated! This library was merged into Zend Framework organization as zend-config-aggregator - please use it instead., (*1)


Lightweight library for collecting and merging configuration from different sources., (*2)

It is designed for zend-expressive applications, but it can work with any PHP project., (*3)

Usage

Config files

At the basic level, ConfigManager can be used to merge PHP-based configuration files:, (*4)

use Zend\Expressive\ConfigManager\ConfigManager;
use Zend\Expressive\ConfigManager\PhpFileProvider;

$configManager = new ConfigManager(
    [
        new PhpFileProvider('*.global.php'),
    ]
);

var_dump($configManager->getMergedConfig());

Each file should return plain PHP array:, (*5)

// db.global.php
return [
    'db' => [
        'dsn' => 'mysql:...',
    ],    
];

// cache.global.php
return [
    'cache_storage' => 'redis',
    'redis' => [ ... ],
];

Result:, (*6)

array(3) {
  'db' =>
  array(1) {
    'dsn' =>
    string(9) "mysql:..."
  }
  'cache_storage' =>
  string(5) "redis"
  'redis' =>
  array(0) {
     ...
  }
}

Configuration is merged in the same order as it is passed, with later entries having precedence., (*7)

Config providers

ConfigManager works by aggregating "Config Providers" passed when creating object. Each provider should be a callable, returning configuration array (or generator) to be merged., (*8)

$configManager = new ConfigManager(
    [
        function () { return ['foo' => 'bar']; },
        new PhpFileProvider('*.global.php'),
    ]
);
var_dump($configManager->getMergedConfig());

If provider is a class name, it is automatically instantiated. This can be used to mimic ZF2 module system - you can specify list of config classes from different packages, and aggregated configuration will be available to your application. Or, as a library owner you can distribute config class with default values., (*9)

Example:, (*10)

class ApplicationConfig
{
    public function __invoke()
    {
        return ['foo' => 'bar'];
    }
}

$configManager = new ConfigManager(
    [
        ApplicationConfig::class,
        new PhpFileProvider('*.global.php'),
    ]
);
var_dump($configManager->getMergedConfig());

Output from both examples will be the same:, (*11)

array(4) {
  'foo' =>
  string(3) "bar"
  'db' =>
  array(1) {
    'dsn' =>
    string(9) "mysql:..."
  }
  'cache_storage' =>
  string(5) "redis"
  'redis' =>
  array(0) {
  }
}

Caching

In order to enable configuration cache, pass cache file name as a second parameter to ConfigManager constructor:, (*12)

$configManager = new ConfigManager(
    [
        function () { return [ConfigManager::ENABLE_CACHE => true]; },
        new PhpFileProvider('*.global.php'),
    ],
    'data/config-cache.php'
);

When cache file is specified, you will also need to add config_cache_enabled key to the config, and set it to TRUE (use ConfigManager::ENABLE_CACHE constant for convenience). This allows to enable cache during deployment by simply putting extra *.local.php file in config directory:, (*13)

return [
    ConfigManager::ENABLE_CACHE` => true,
];

When caching is enabled, ConfigManager does not iterate config providers. Because of that it is very fast, but after it is enabled you cannot do any changes to configuration without clearing the cache. Caching should be used only in production environment, and your deployment process should clear the cache., (*14)

Generators

Config providers can be written as generators. This way single callable can provide multiple configurations:, (*15)

$configManager = new ConfigManager(
    [
        function () { 
            foreach (Glob::glob('data/*.global.php', Glob::GLOB_BRACE) as $file) {
                yield include $file;
            } 
        }        
    ]
);
var_dump($configManager->getMergedConfig());

PhpFileProvider is implemented using generators., (*16)

Available config providers

PhpFileProvider

Loads configuration from PHP files returning arrays, like this one:, (*17)

return [
    'db' => [
        'dsn' => 'mysql:...',
    ],    
];

Wildcards are supported:, (*18)

$configManager = new ConfigManager(
    [
        new PhpFileProvider('config/*.global.php'),        
    ]
);

Example above will merge all matching files from config directory - if you have files like app.global.php, database.global.php, they will be loaded using this few lines of code., (*19)

Internally, ZendStdlib\Glob is used for resolving wildcards, meaning that you can use more complex patterns (for instance: 'config/autoload/{{,*.}global,{,*.}local}.php'), that will work even on Windows platform., (*20)

ZendConfigProvider

Sometimes using plain PHP files may be not enough - you may want to build your configuration from multiple files of different formats: INI, YAML, or XML. For this purpose you can leverage ZendConfigProvider:, (*21)

$configManager = new ConfigManager(
    [
        new ZendConfigProvider('*.global.json'),
        new ZendConfigProvider('database.local.ini'),
    ]
);

ZendConfigProvider accepts wildcards and autodetects config type based on file extension., (*22)

ZendConfigProvider requires two packages to be installed: zendframework/zend-config and zendframework/zend-servicemanager. Some config readers (JSON, YAML) may need additional dependencies - please refer to Zend Config Manual for more details., (*23)

The Versions

13/03 2017

dev-master

9999999-dev

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

21/04 2016

dev-feature/service_providers

dev-feature/service_providers

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

17/03 2016

0.4.0

0.4.0.0

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

08/02 2016

dev-default_config

dev-default_config

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

24/01 2016

0.3.1

0.3.1.0

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || ^7.0

 

The Development Requires

24/01 2016

0.3.0

0.3.0.0

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || 7.0

 

The Development Requires

24/01 2016

dev-remove-zend-stdlib-dependency

dev-remove-zend-stdlib-dependency

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

  • php ^5.5 || 7.0

 

The Development Requires

23/12 2015

dev-cleanup

dev-cleanup

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

19/12 2015

0.2.0

0.2.0.0

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

19/12 2015

dev-rename_glob_file_provider

dev-rename_glob_file_provider

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

19/12 2015

dev-zend_config

dev-zend_config

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

10/12 2015

0.1.0

0.1.0.0

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

10/12 2015

dev-dont_return_object

dev-dont_return_object

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

04/12 2015

dev-generator_providers

dev-generator_providers

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires

03/12 2015

dev-invokable_providers

dev-invokable_providers

Lightweight library for merging and caching application config

  Sources   Download

BSD 3-Clause

The Requires

 

The Development Requires