Obullo / Config

Build Status Software License

Configuration file loader built on zend/config package that comes with environment support.

Install

$ composer require obullo/config

Requirements

The following versions of PHP are supported by this version.

Testing

$ vendor/bin/phpunit

Quick start

Global configuration

require 'vendor/autoload.php';

define('ROOT', '/var/www/myproject/');
define('CONFIG_CACHE_FILE', 'cache/config.php');

use Zend\ServiceManager\ServiceManager;
use Symfony\Component\Yaml\Yaml as SymfonyYaml;
use Zend\Config\Config;
use Zend\Config\Factory;
use Zend\Config\Reader\Yaml as YamlReader;

use Zend\ConfigAggregator\ArrayProvider;
use Zend\ConfigAggregator\ConfigAggregator;
use Zend\ConfigAggregator\ZendConfigProvider;

$container = new ServiceManager;
$container->setService('yaml', new YamlReader([SymfonyYaml::class, 'parse']));

Factory::registerReader('yaml', $container->get('yaml'));
Factory::setReaderPluginManager($container);

$aggregator = new ConfigAggregator(
    [
        new ArrayProvider([ConfigAggregator::ENABLE_CACHE => true]),
        new ZendConfigProvider(ROOT.'config/autoload/{,*.}{json,yaml,php}'),
    ],
    CONFIG_CACHE_FILE
);
$config = $aggregator->getMergedConfig();

Create global config object

$container->setService('config', new Config($config, true));  

Create local config object as loader

use Obullo\Config\ConfigLoader;

$loader = new ConfigLoader(
    $config,
    CONFIG_CACHE_FILE
);
$container->setService('loader', $loader);

Reading files globally

$container->get('config')->foo->bar; // value

Reading files locally

$amqp = $container->get('loader')
        ->load(ROOT, '/config/amqp.yaml')
        ->amqp;

echo $amqp->host; // 127.0.0.1

Readers

If you want to add new reader you need define first at the top.

$container = new ServiceManager;
$container->setService('json', new JsonReader);
$container->setService('yaml', new YamlReader([SymfonyYaml::class, 'parse']));

Factory::registerReader('json', $container->get('json'));
Factory::registerReader('yaml', $container->get('yaml'));
Factory::setReaderPluginManager($container);

Reading json file

$amqp = $container->get('loader')
        ->load(ROOT, '/config/amqp.json')
        ->amqp;

echo $amqp->host; // 127.0.0.1

For php files no needs any definition.

$amqp = $container->get('loader')
        ->load(ROOT, '/config/amqp.php')
        ->amqp;

echo $amqp->host; // 127.0.0.1

Environment variable

An example .yaml configuration file.

# amqp
# 

amqp:
    host: 127.0.0.1
    port: 5672
    username: 'env(AMQP_USERNAME)'
    password: 'env(AMQP_PASSWORD)'
    vhost: /

Fill in sample environment variables.

putenv('AMQP_USERNAME', 'guest');
putenv('AMQP_PASSWORD', 'guest');

Add env processor to read env values.

use Obullo\Config\Processor\Env as EnvProcessor;
$loader = $container->get('loader');
$loader->addProcessor(new EnvProcessor);

$amqp = $loader->load(ROOT, '/config/amqp.yaml')
        ->amqp;

echo $amqp->username;  // guest
echo $amqp->password;  // guest

If you use '%s' in a folder path, this variable is changed with the value 'APP_ENV'.

/config/%s/amqp.yaml
/config/dev/amqp.yaml  // after replacement

The environment variable can be set with the setEnv method.

$loader = $container->get('loader');
$loader->setEnv(getenv('APP_ENV'));
$loader->addProcessor(new EnvProcessor);

$amqp = $loader->load(ROOT, '/config/%s/amqp.yaml')
        ->amqp;

echo $amqp->password;  // guest

getenv() function

Every time for the 'env()' functions defined in the file, the native php getenv() method is executed.

# amqp
# 

amqp:
    host: 127.0.0.1
    port: 5672
    username: 'env(AMQP_USERNAME)'
    password: 'env(AMQP_PASSWORD)'
    vhost: /

You can use the putenv('VARIABLE=VALUE') method to assign Env variables, or the more comprehensive vlucas/phpdotenv package for this method .

Constant processor

In some cases it may be necessary to use php constants in a configuration file as follows.

# cache
# 

dir: 'ROOT/var/cache/'

In this case, you need to use Constant processor like below.

use Zend\Config\Processor\Constant as ConstantProcessor;

$loader = $container->get('loader');
$loader->setEnv(getenv('APP_ENV'));
$loader->addProcessor(new ConstantProcessor);

$cache = $loader->load(ROOT, '/config/%s/cache.yaml')
        ->cache;

echo $cache->dir; // /var/www/myproject/var/cache/