Obullo / Config

Build Status Software License

It is a standalone package that assumes configuration management by reading the configuration files in your application.

Install

$ composer require obullo/config

Requirements

The following versions of PHP are supported by this version.

Testing

$ vendor/bin/phpunit

Quick start

An example .yaml configuration file.

# amqp
# 

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

Configuration

require '../vendor/autoload.php';

define('ROOT', '/var/www/');
putenv('AMQP_USERNAME', 'guest');
putenv('AMQP_PASSWORD', 'guest');

use Obullo\Config\Cache\FileHandler;
use Obullo\Config\Reader\YamlReader;
use Obullo\Config\Loader;

$cacheHandler = new FileHandler('/path/to/cache/folder');

Reading configuration file

$loader = new Loader;
$loader->registerReader('yaml', new YamlReader($cacheHandler));

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

echo $amqp->host; // 127.0.0.1
echo $amqp->port; // 5672
echo $amqp->username;  // guest
echo $amqp->password;  // guest
echo $amqp->vhost;  // "/"

Caching

Each uploaded file is cached with the specified cache handler and this cache is refreshed when you make changes. Thus, no parsing is done for these files every time.

Cache handlers

The default cache handler is FileHandler class. If the cache handler is FileHandler, you need to set write permission to write to the specified directory.

Environment variable

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 = new Loader;
$loader->setEnv(getenv('APP_ENV'));
$loader->registerReader('yaml', new YamlReader($cacheHandler));

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

echo $amqp->host; // 127.0.0.1
echo $amqp->port; // 5672
echo $amqp->username;  // guest
echo $amqp->password;  // guest
echo $amqp->vhost;  // "/"

getenv() function

Every time for the '%env()%' functions defined in the file, the natural 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 .

Configuration variables

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

# cache
# 

dir: '%root%/var/cache/'

In this case, you need to predefine these variables in your reader class with the addVariable() method.

$reader = new YamlReader($cacheHandler);
$reader->addVariable('%root%', ROOT);
$reader->addVariable('%foo%','bar');

Loading files

An example .yaml file.

# routes
#

home:
    method: GET
    path: /
    handler: App\Controller\DefaultController::index

Reading in array format,

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

echo $routes['home']['method']; // GET
echo $routes['home']['path']; // "/""
echo $routes['home']['handler']; // App\Controller\DefaultController::index

For object type you need to send true as the second parameter.

$routes = $loader->load(ROOT, '/config/routes.yaml', true)

echo $routes->home->method; // GET
echo $routes->home->path; // "/""
echo $routes->home->handler; // App\Controller\DefaultController::index

Loading multiple files

If you need to load some configuration files collectively when the application is started, you can follow this method instead of loading them individually.

$loader = new Loader;
$loader->setEnv(getenv('APP_ENV'));
$loader->registerReader('yaml', new YamlReader($cacheHandler));

// Put all config files here you want to load at bootstrap.

$config = $loader->loadFiles(
    [
        ROOT.'/config/%s/framework.yaml',
        ROOT.'/config/%s/database.yaml',
    ]
);