How to save application.ini paths using Zend_Config_Writer_Ini

I am currently working on a build system in Phing that accepts a Zend Framework project template and customizes it according to the Phing settings. One problem I am facing is using Zend_Config_Writer_Ini.

My Phing task takes a pre-populated file from the repo called application.default.ini and modifies it with Zend_Config_Ini to add parameters from the build file (db data, etc.). He then writes it to application.ini, ready for use by the project. A simplified version of the corresponding task code looks something like this:

$appConfig = new Zend_Config_Ini( $appDefaultConfigPath, null, array( 'skipExtends' => true, 'allowModifications' => true ) ); $appConfig->production->resources->db->params->host = $buildProperties->db->host; $appConfig->production->resources->db->params->username = $buildProperties->db->username; $appConfig->production->resources->db->params->password = $buildProperties->db->password; $appConfig->production->resources->db->params->dbname = $buildProperties->db->dbname; $writer = new Zend_Config_Writer_Ini(); $writer->setConfig($appConfig) ->setFilename($appConfigPath) ->write(); 

This works fine considering database credentials, but when it comes to pre-populated paths that contain certain constants, something goes wrong. For example:

 bootstrap.path = APPLICATION_PATH "/Bootstrap.php" 

becomes:

 bootstrap.path = "APPLICATION_PATH/Bootstrap.php" 

Is there a way to save these configuration lines when reading / writing in different ini files, or do I need to rebuild the assembly file to copy the file before starting the task and change only the ini lines that I need to change?

+3
source share
4 answers

When loading an existing configuration, all constants are already translated, i.e. if you look at an object with print_r, you will no longer find your constants. Therefore, the full path is written with the author, not constants.

In your case, I assume that constants do not exist in your environment and therefore print as they are.

Refresh . To be more specific. Zend_Config_Ini::_parseIniFile() uses parse_ini_file() to read an ini file that loads constants as real paths. See php.net doc Example # 2

+1
source

Straight from this php.net comment :

Constants in ini files do not expand if they are combined with strings quoted with single quotes, should only be in double quotes to expand the constants.

Example:

define ('APP_PATH', '/ some / path');

mypath = APP_PATH '/ config' // The constant will not be expanded: [mypath] => APP_PATH '/ config'

mypath = APP_PATH "/ config" // The constant will be expanded: [mypath] => / Some / path / configuration

So, you can rewrite your templates with single quotes ... bootstrap.path = APPLICATION_PATH '/Bootstrap.php'

... and later replace all occurrences of APPLICATION_PATH '*' with double quotes (a simple Regex should be executed).

+1
source

Alternatively, you can use the Phing Filter to replace tokens in the configuration template.

Example task:

 <target name="setup-config" description="setup configuration"> <copy file="application/configs/application.ini.dist" tofile="application/configs/application.ini" overwrite="true"> <filterchain> <replacetokens begintoken="##" endtoken="##"> <token key="DB_HOSTNAME" value="${db.host}"/> <token key="DB_USERNAME" value="${db.user}"/> <token key="DB_PASSWORD" value="${db.pass}"/> <token key="DB_DATABASE" value="${db.name}"/> </replacetokens> </filterchain> </copy> </target> 

This task copies application/configs/application.ini.dist to application/configs/application.ini and replaces tokens of type ##DB_HOSTNAME## value from the phing ${db.host}

+1
source

I needed to use Zend_Config while retaining the ability to use the APPLICATION_PATH constant, so I ended up fixing the file with a simple regular expression after saving the Zend_Config_Writer file.

 $writer->write(); // Zend_Config_Writer messes up the settings that contain APPLICATION_PATH $content = file_get_contents($filename); file_put_contents($filename, preg_replace('/"APPLICATION_PATH(.*)/', 'APPLICATION_PATH "$1', $content)); 
0
source

All Articles