phpzend-frameworkzend-config

Add comments or formatting with Zend_Config_Writer


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


Solution

  • 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
        }