如何使用Zend_Config_Writer_Ini保留application.ini路径 [英] How to preserve application.ini paths using Zend_Config_Writer_Ini

查看:89
本文介绍了如何使用Zend_Config_Writer_Ini保留application.ini路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在Phing中构建一个构建系统,该系统采用Zend Framework项目模板并根据Phing参数对其进行配置.我遇到的一个问题是使用Zend_Config_Writer_Ini时.

I'm currently working on a build system in Phing that takes a Zend Framework project template and configures it according to Phing parameters. One problem that I've come across is when using Zend_Config_Writer_Ini.

我的Phing任务从回购中获取一个预填充的文件,名为application.default.ini,并使用Zend_Config_Ini对其进行修改,以从构建文件中添加参数(数据库详细信息等).然后将其写入application.ini,以供项目使用.相关任务代码的简化版本如下所示:

My Phing task takes a pre-populated file from the repo called application.default.ini and modifies this using Zend_Config_Ini to add parameters from the build file (db details, etc). It then writes it to application.ini ready for the project to use. A simplified version of the related 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 as far as the database credentials go but when it comes to pre-populated paths that include defined constants something goes wrong. For example:

bootstrap.path = APPLICATION_PATH "/Bootstrap.php"

成为:

bootstrap.path = "APPLICATION_PATH/Bootstrap.php"

在读取/写入不同的ini文件时是否有任何方法可以保留这些配置行,还是应该在运行任务之前重组我的构建文件以复制该文件并仅修改我需要更改的ini行?

Is there any way to preserve these config lines when reading/writing to different ini files or should I restructure my build file to copy the file before running the task and only modify the ini lines that I need to change?

推荐答案

在加载现有配置时,所有常量都已转换,即,如果您用print_r查看对象,您将不再找到常量.因此,使用writer可以打印完整的路径,而不是常量.

When you load the existing config all constants are already translated, i.e. if you look at the object with print_r you won't find your constants anymore. Hence, with the writer the full path is printed instead of the constants.

在您的情况下,我猜想常量在您的环境中不存在,因此按原样打印.

In your case I guess that the constants don't exist in your environment and therefore are printed as is.

更新:更具体. Zend_Config_Ini::_parseIniFile()使用parse_ini_file()读取ini文件,该文件将常量加载为真实路径.参见 php.net文档示例#2

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

这篇关于如何使用Zend_Config_Writer_Ini保留application.ini路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆