php-config
, (*1)
A simple class supporting different configuration values based on the environment, (*2)
Installation
The package can be installed via Composer by adding to the composer.json
require block., (*3)
{
"require": {
"mkomorowski/php-config": "1.0"
}
}
Then update application packages by running the command:, (*4)
php composer.phar update
Configuration
Examples of files with application configuration options., (*5)
local.php, (*6)
return array(
'debug' => true,
'database' => array(
'host' => '127.0.0.1',
'password' => 'password',
),
);
production.php, (*7)
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., (*8)
$loader = new mKomorowski\Config\Loader('/app/config');
Next we are defining the environment settings:, (*9)
$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., (*10)
$config = new mKomorowski\Config\Config($loader, $environment, 'stage');
We can change the default environment later by:, (*11)
$config->setDefaultEnvironment('production');
Usage
To retrieve the settings just use:, (*12)
$config->get('debug');
Accessing nested values is possible with dotted notation, (*13)
$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., (*14)