This is my environment based configuration implementation for PhalconPHP App. It overrides configs by machine hostname
.
You can override by any other parameter.
my config files:
\-config
|-config.php // main config file
|-portal.php // production config file
|-thinkpad.php // my laptop config file
|-...(1 config per hostname)
main config.php
is something like this:
<?php
$config = [
'application' => [
'name' => 'APP NAME',
'codename' => 'RONIN',
],
'database' => [
'adapter' => 'Postgresql',
'host' => 'DB_HOST',
'username' => 'DB_USER',
'password' => 'DB_PASS',
'name' => 'DB_NAME',
],
'cache' => [
'prefix' => 'PREFIX.'
],
'log' => [
'adapter' => 'redis',
'logLevel' => 7,
'limit' => 10,
'logFile' => '/tmp/ronin.log'
],
.... // ANY OTHER CONFIGURATION
];
function array_merge_recursive_replace ()
{
$arrays = func_get_args();
$base = array_shift($arrays);
foreach ($arrays as $array) {
reset($base);
while (list($key, $value) = @each($array)) {
if (is_array($value) && @is_array($base[$key])) {
$base[$key] = array_merge_recursive_replace($base[$key], $value);
}
else {
$base[$key] = $value;
}
}
}
return $base;
}
if (file_exists(__DIR__ . DIRECTORY_SEPARATOR . gethostname() . '.php')) {
$config = array_merge_recursive_replace($config, require(gethostname() . '.php'));
// it depends on machine hostname and override config.php by yourhostname.php // ****
}
return new \Phalcon\Config($config);
any other configuration file override main configs. for example if your production machine name is portal then create portal.php
file like below:
<?php
return [
'database' => [
'adapter' => 'Postgresql',
'host' => 'localhost',
'username' => 'postgres',
'password' => '1234',
'name' => 'real_portal_dbname',
],
'log' => [
'logLevel' => 2,
'logFile' => '/var/log/app/ronin.log'
],
];
init config in bootstrap:
$config = include ROOT . '/app/config/config.php';
$di = new \Phalcon\DI\FactoryDefault();
$di->setShared('config', $config);
don't forget to add all config files except config.php to gitignore
Other solutions are welcome :)