Environment based application configuration

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 :)

Great solution! :)

Interesting approach. I personally use config.ini.dist approach with gitignore to real config.ini. I'm kind of guy that think enviroment config should be just in the enviroment and should not go out to another enviroment.

BTW: I hope in gist, there is not your real madrill api key ;p

edited Sep '15

Dear @nexik Yes it's not my real mandrill API key. I've Changed some charcters :)

Good to know.



12.9k

page not found

@kisb2 sorry for the problem. I've updated the post.

page not found