Pragmatism in the real world

Local config files and Zend_Application

A friend of mine recently had a requirement where she wanted to have two config files loaded into Zend_Application, so that the specific settings for the server were not stored in the version control system.

Hence she has two config files: application.ini and local.ini where local.ini is different on each server.

Update:
As takeshin points out with ZF 1.10 or later, an easier solution to this is to pass both ini files into Zend_Application:
$application = new Zend_Application(
APPLICATION_ENV,
array(
'config' => array(
APPLICATION_PATH . '/configs/application.ini',
APPLICATION_PATH . '/configs/local.ini'
)
)
);

Original content:

The obvious way to approach this problem is to load the two files within index.php, merge them and then pass the merged config file to Zend_Application.

The code to do this looks like this:

require_once 'Zend/Application.php';
require_once 'Zend/Config/Ini.php';

$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/application.ini', APPLICATION_ENV,
array('allowModifications'=>true));
$localConfig = new Zend_Config_Ini(APPLICATION_PATH . '/configs/local.ini', APPLICATION_ENV);
$config->merge($localConfig);
$config->setReadOnly();

// Create application, bootstrap, and run
$application = new Zend_Application(
APPLICATION_ENV,
$config
);
$application->bootstrap()
->run();

I’ve included the require_once 'Zend/Application.php'; call for context.

As you can see, we create two Zend_Config objects, one for application.ini and one for local.ini and we load up the correct APPLICATION_ENV section in both cases. This means that local.ini must have the same set of “master” environment sections that application.ini has.

For application.ini, we set the option “allowModifications’ so that we can then use the merge() method to override the $config with the new data within $localConfig.

Having merged the local config into the main one, we can then setReadOnly() to ensure that the config isn’t changed anymore.

Finally, when we instantiate Zend_Application, we pass in our $config object rather than the name of the config file and then Zend_Application does the right thing.

9 thoughts on “Local config files and Zend_Application

  1. Is it really needed?

    Maybe an array will do fine:

    $application = new Zend_Application(
        APPLICATION_ENV,
        array(
            'config' => array(
                APPLICATION_PATH . '/configs/application.ini',
                APPLICATION_PATH . '/configs/local.ini'
            )
        )
    );
  2. I've typically gone with a template file stored in the vcs with the actual config file ignored. The downside is that it's easy to forget to add your changes back into the template file.

  3. I agree with takeshin. Providing an array of config files looks much neater and has been working terribly well for me in production.

  4. I recently needed to do this same, but the approach I took was simply convert both the configs into arrays and use array_merge – Didn't realize there was a merge function in the Zend_Config_Object, think I'll change it to use it instead.

  5. I really like the idea of having these two configs, but wonder about the performance issues. For every page load you have to load and merge the two configs. Has anyone looked into caching the merged config?

  6. I've recently had a similar requirement and I ended up implementing the same solution !
    The only difference was as different section for the second file (used to store server options).

    Another solution might be extending the Zend_Application class and adding a method to merge two files. However, since the Zend_Application supports options in Zend_Config format, the easiest solution I found was just merging the files in the index.php

  7. Is it really needed? Maybe an array will do fine: $application = new Zend_Application( APPLICATION_ENV, array( 'config' => array( APPLICATION_PATH . '/configs/application.ini', APPLICATION_PATH . '/configs/local.ini' ) ) );

  8. I recently needed to do this same, but the approach I took was simply convert both the configs into arrays and use array_merge – Didn't realize there was a merge function in the Zend_Config_Object, think I'll change it to use it instead.

Comments are closed.