Tricky Configuration Setup, Could Use Some Help

Hi all, I’ve tried searching the forums but haven’t yet found an answer that quite matches my problem.

In our application, we have three levels of config files, or more. main.php is the production environment, qa.php is for pointing our qa environments to our qa database, and dev.php swaps out lots of features for their local or dummy versions. We set up simple inheritance by using CMap::mergeArray(), to have qa.php and dev.php inherit most settings from main.php

Our problem is arising when we try to swap out a different component in dev.php for their main.php counterparts. For example, we have set up memcache in main.php:




return array(

    // application components

    'components' => array(

        'cache' => array(

            'class' => 'CMemCache',

            'servers' => array(

                array(

                    'host' => 'x.x.x.x',

                    'port' => '11211',

                    'weight' => '100',

                )

            )

        )

    )

);



And in dev.php we override that with a CDummyCache, because we don’t want to use caching locally:




return array(

    // application components

    'components' => array(

        'cache' => array(

            'class' => 'CDummyCache'

        )

    )

);



We combine the two in our index.php with the following:




// setup config settings

$config = require dirname(__FILE__) . '/protected/config/main.php';

if ($_SERVER['APPLICATION_ENV'] == 'local') {

    $configServer = require dirname(__FILE__) . '/protected/config/dev.php';

    $config = CMap::mergeArray($config, $configServer);

}

$app = Yii::createWebApplication($config)->run();



Because of the recursive nature of the mergeArray method, our final configuration on our local machines ends up with a mishmash of both the main and dev definitions for the cache, with no way to completely override main’s definition of the cache component short of unsetting it before we do the merge.

Ultimately, this causes Yii to crash locally, because it tries to create a CDummyCache and then set its ‘servers’ attribute, which of course isn’t defined in CDummyCache.

Any ideas for the best way to solve situations like this?

We’re using Yii 1.1.10

First thing that comes to mind is that your main.config is for config data that is common to all setups. So if memcache isn’t common to all your setups don’t put it in the common config. Instead put it in a production.config that is specific to the environments where it’s relevant.

That said I’d also be interested to hear if there is an easy way to completely override a config section.