Configuration file loader built on zend/config package that comes with environment support.
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
src
tests
website
.gitignore
.travis.ini
.travis.yml
CHANGELOG.md
LICENSE.md
README.md
composer.json
phpunit.xml

README.md

Obullo / Config

Build Status Software License Total Downloads

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.

  • 7.0
  • 7.1
  • 7.2

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

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

Documentation

Documents are available at http://config.obullo.com/