mkomorowski/php-config

Simple class supporting different configuration values based on the environment

1.0 2015-06-14 09:02 UTC

This package is not auto-updated.

Last update: 2024-11-09 16:32:24 UTC


README

Latest Stable Version Build Status Coverage Status

A simple class supporting different configuration values based on the environment

Installation

The package can be installed via Composer by adding to the composer.json require block.

{
    "require": {
        "mkomorowski/php-config": "1.0"
    }
}

Then update application packages by running the command:

php composer.phar update

Configuration

Examples of files with application configuration options.

local.php

return array(
        'debug' => true,
        'database' => array(
            'host' => '127.0.0.1',
            'password' => 'password',
        ),
);

production.php

return array(
        'debug' => false,
        'database' => array(
            'host' => 'rds.amazon.com',
            'password' => 'password',
        ),
);

In the settings loader we are specifying the path to the directory with config files.

$loader = new mKomorowski\Config\Loader('/app/config');

Next we are defining the environment settings:

$environment = new mKomorowski\Config\Environments(array(
    'local' => array('local', 'MacBook.local'),
    'stage' => array('cent_os_stage')
));

Finally we initialize the Config class, passing settings and environments. The third paramater is an optional default environment.

$config = new mKomorowski\Config\Config($loader, $environment, 'stage');

We can change the default environment later by:

$config->setDefaultEnvironment('production');

Usage

To retrieve the settings just use:

$config->get('debug');

Accessing nested values is possible with dotted notation

$config->get('database.hostname');

If your hosts is signed to a specific environment it will return the appropriate value. If not it will look for default environment settings or return null if the key is not set.