composer require eve-in-ua/multiconf
OR
{
"require": {
"eve-in-ua/multiconf": "^v1.0.0"
}
}
composer test
{ENV_ROOT}/.env
ENV=DEV
DB_HOST=localhost
DB_USER=user_name
DB_PASS=password
DB_NAME=database_name
{ENV_ROOT}/.env.default
ENV=PROD
DB_HOST=
DB_PORT=3306
DB_USER=
DB_PASS=
DB_NAME=
TABLE_PREFIX=
{CONFIG_ROOT}/config/example.php
<?php
return [
'foo' => 'bar',
'zoo' => [
'baz', // 0 => 'baz'
],
];
{CONFIG_ROOT}/config/example.default.php
<?php
return [
'foo' => 'baz',
'def' => 'def',
];
example.php
will recursively override values from example.default.php
config - so in example.php
you can specify
only those configs which you want to override.
{CONFIG_ROOT}/config/a-first-config.php
<?php
$multiConf = new \EveInUa\MultiConf\Config(); // version 1
$multiConf = new \MultiConf(); // version 2
$multiConf = \MultiConf::instance(); // version 3 - best
// Wait for `example2` to load:
if ($multiConf->waitFor('a-first-config', ['env', 'example2'])) { return null; }
return [
'foo' => 'baz',
'def' => 'def',
'example2-data' => $multiConf->config('example2'),
];
<?php
// You can set directories manually for library using $_SERVER['DOCUMENT_ROOT'] as CONFIG_ROOT and ENV_ROOT
define('CONFIG_ROOT', __DIR__); // optional, will use DOCUMENT_ROOT instead
define('ENV_ROOT', __DIR__); // optional, will use DOCUMENT_ROOT instead
require_once __DIR__ . '/vendor/autoload.php';
$multiConf = \MultiConf::instance();
$result = [
$multiConf->env(), // DEV - from ENV_ROOT/.env
$multiConf->env('DB_HOST'), // localhost - from ENV_ROOT/.env
$multiConf->env('DB_PORT'), // 3306 - from ENV_ROOT/.env.default
$multiConf->config('example.foo'), // bar - from CONFIG_ROOT/config/example.php
$multiConf->config('example.def'), // def - from CONFIG_ROOT/config/example.default.php
];
var_dump($result);
Output:
array(5) {
[0]=> string(3) "DEV"
[1]=> string(9) "localhost"
[2]=> int(3306)
[3]=> string(3) "bar"
[4]=> string(3) "def"
}