Let's imagine that you have an application, extended by 2 modules.
./
├── application.php
├── configs/
│ ├── production/
│ │ ├── application.yml
│ │ └── mysql.yml
│ ├── application.yml.dist
│ ├── sqlite.yml.dist
│ ├── mysql.yml.dist
│ └── mysql.yml
├── modules/
│ ├── module1/
│ │ ├── application.yml
│ │ └── def.php
│ └── module2/
│ ├── application.yml
│ ├── def.php
│ └── mysql.yml
└── vendor/
└── ...
Your application configurations are stores in the configs directory and your production specific config in the directory configs/production. Each module have their configurations in their main directory
database:
engine: mysql
debug: false
cache: redis
host: 192.168.0.1
username: mysql_user
password: TheProductionPassword
db_name: production
database:
engine: sqlite
debug: true
cache: files
path: %PROJECT%/db.sqlite
host: localhost
username: root
password: root
db_name: application_skeleton
port: 3306
host: localhost
username: root
password: rootroot
db_name: my_application
port: 3306
cache: apc
cache_version: apcu
db_name: my_app
require 'vendor/autoload.php';
define('PROJECT_ROOT', __DIR__);
define('ENV', 'production');
$config = new \MacFJA\ChainConfig\Config();
$config->appendPath(__DIR__ . DIRECTORY_SEPARATOR . 'configs');
$config->appendReader(new \MacFJA\ChainConfig\Reader\YamlReader());
// ...
$config->prependPath(__DIR__ . DIRECTORY_SEPARATOR . 'configs' . DIRECTORY_SEPARATOR . ENV);
$config->appendKeyCallback(function(key) use ($config) {
if (str_pos('{database_engine}', $key) !== false) {
return str_replace('{database_engine}', $config->get('application.database.engine'), $key);
}
return $key;
});
$config->appendValueCallback(function(key, $value) {
return str_replace('%PROJECT%', PROJECT_ROOT, $value);
});
// ...
// Load your module
// ...
$databaseLayer = new YourDatabaseAbstractionLayer($config->get('{database_engine}'));
$config->appendPath(__DIR__, 'module1');
$config->setGroupPosition('module1', \MacFJA\ChainConfig\Collection\OrderedGroup::DEFAULT_GROUP);
$config->appendPath(__DIR__, 'module2');
$config->setGroupPosition('module1', \MacFJA\ChainConfig\Collection\OrderedGroup::DEFAULT_GROUP, 'module1');
application:
database:
engine: mysql
debug: false
cache: apc
cache_version: apcu
mysql:
host: 192.168.0.1
username: mysql_user
password: TheProductionPassword
db_name: my_app
port: 3306