如何使用 Zend_Config_Writer_Ini 保留 application.ini 路径
我目前正在 Phing 中开发一个构建系统,该系统采用 Zend Framework 项目模板并根据 Phing 参数对其进行配置。我遇到的一个问题是使用 Zend_Config_Writer_Ini 时。
我的 Phing 任务从存储库中获取一个名为 application.default.ini 的预填充文件,并使用 Zend_Config_Ini 修改该文件以添加构建文件中的参数(数据库详细信息等)。然后将其写入 application.ini 供项目使用。相关任务代码的简化版本如下所示:
$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();
就数据库凭据而言,这工作得很好,但当涉及到包含已定义常量的预填充路径时,就会出现问题。例如:
bootstrap.path = APPLICATION_PATH "/Bootstrap.php"
变成:
bootstrap.path = "APPLICATION_PATH/Bootstrap.php"
在读取/写入不同的 ini 文件时是否有任何方法可以保留这些配置行,或者我应该重构构建文件以在运行任务之前复制文件,并且只修改我需要更改的 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.
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"
becomes:
bootstrap.path = "APPLICATION_PATH/Bootstrap.php"
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
当您加载现有配置时,所有常量都已翻译,即,如果您使用 print_r 查看对象,您将不再找到常量。因此,对于编写器来说,会打印完整路径而不是常量。
在您的情况下,我猜您的环境中不存在常量,因此按原样打印。
更新:更具体。
Zend_Config_Ini::_parseIniFile()
使用parse_ini_file()
读取 ini 文件,该文件将常量加载为真实路径。请参阅 php.net 文档示例#2When 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.
Update: To be more specific.
Zend_Config_Ini::_parseIniFile()
usesparse_ini_file()
to read the ini file which loads the constants as real paths. See php.net doc Example #2直接来自此 php.net 评论:
所以你可以用单引号重写你的路径......
bootstrap.path = APPLICATION_PATH '/Bootstrap.php'
...然后用双引号替换所有出现的
APPLICATION_PATH '*'
(一个简单的正则表达式应该可以)。Straight from this php.net comment:
So you could rewrite your pathes with single quotes ...
bootstrap.path = APPLICATION_PATH '/Bootstrap.php'
... and later replace all occurrences of
APPLICATION_PATH '*'
with double quotes (a simple Regex should do).作为替代方案,您可以使用 Phing 的 Filter 来替换配置模板中的令牌。
示例任务:
此任务将
application/configs/application.ini.dist
复制到application/configs/application.ini
并替换诸如##DB_HOSTNAME## 之类的标记
为 phing 属性${db.host}
的值As an alternative you could use Phing's Filter to replace tokens in your configuration template.
A example task:
This task copies
application/configs/application.ini.dist
toapplication/configs/application.ini
and replaces tokens like##DB_HOSTNAME##
with the value from the phing property${db.host}
我想要方便地使用 Zend_Config,同时保留使用 APPLICATION_PATH 常量的能力,因此在 Zend_Config_Writer 保存文件后,我最终使用简单的正则表达式修复了该文件。
I wanted the convenience of using Zend_Config while preserving the ability to use the APPLICATION_PATH constant so I ended up fixing the file with a simple regex after Zend_Config_Writer saves the file.