1

I've been playing around with Zend_Config_Writer, and although I can make it do what I want I find the lack of formatting a bit disturbing since:

[production : general]
;
; Production site configuration data.
;

locale                                          = sv_SE
...

Becomes

[production : general]   
locale                                          = sv_SE
...

I realize that the "new" configuration is written based on the saved values in a Zend_Config object and this object doesn't contain any comments or bland rows, but this makes the new configuration very hard to read especially for my co-workers.

Can this be solved somehow? The best I've come up with is using different sections with a "cascading" inheritance, but that seems like a stupid idea

Lobo
  • 566
  • 1
  • 7
  • 20

2 Answers2

0

As you say, Zend_Config_Writer won't render any comments, since they are not stored in the Zend_Config object. Depending on the structure of the ini file you want to render, you might use the "cascading", at least to clear redundancies (it doesn't look so stupid to me, it's done even in the standard application.ini config file...).

Of course, another solution might be using or creating something else to write your ini files, but it can be overkill.

Hope that helps,

dinopmi
  • 2,683
  • 19
  • 24
  • Yes it is used in the standard and I use it in my own application, but to replace my comments with sections I would need to have a general section, then a Zend config session that inherits the general, then a php config section, a number of different sections with usernames/passwords for the different databasese and services my application connects to and so on. I would end up with 18 different sections or so where all of them should inherit the one "above" them. For now i've solved it by using two config files so I can keep most of my comments in the main file. – Lobo Apr 24 '12 at 21:57
0

After some experimenting I've solved my problem the following way and tested it successfully.

  1. Split the config into multiple files. In my case I have 1 large application.ini that hold almost all my config and 1 small version.ini that holds some version-specific data
  2. Create all (in my case 2) Zend_Config_ini objects separetly but set allowModification on one
  3. Use the Zend_Config_Ini->Merge() functionality to merge all configs and then set it to readonly
  4. To update any part of the config create a new Zend_Config_ini object from that specific ini file and set it to allow modifications and skip extents
  5. Update the config and write the changes with the Zend_Config_Writer_ini

Example code:

/* Load the config */    
//Get the application-config and set 'allowModifications' => true
$config = new Zend_Config_Ini('../application/configs/application.ini',$state, array('allowModifications' => true));

//Get the second config-file
$configVersion = new Zend_Config_Ini('../application/configs/version.ini');

//Merge both config-files and then set it to read-only
$config->merge($configVersion);
$config->setReadOnly();

/* Update a part of the config */
$configVersion = new Zend_Config_Ini(
        APPLICATION_PATH.'/configs/version.ini',
        null,
        array('skipExtends' => true, 'allowModifications' => true)
    );

//Change some data here
$configVersion->newData = "Some data";

//Write the updated ini
$writer = new Zend_Config_Writer_Ini(
        array('config' => $configVersion, 'filename' => 'Path_To_Config_files/version.ini')
    );
    try
    {
        $writer->write();
    }
    catch (Exception $e) {
        //Error handling
    }
Lobo
  • 566
  • 1
  • 7
  • 20