Php 使用Zend_Config_Writer添加注释或格式
我一直在玩Zend_Config_Writer,虽然我可以让它做我想做的事情,但我发现缺少格式有点令人不安,因为:Php 使用Zend_Config_Writer添加注释或格式,php,zend-framework,zend-config,Php,Zend Framework,Zend Config,我一直在玩Zend_Config_Writer,虽然我可以让它做我想做的事情,但我发现缺少格式有点令人不安,因为: [production : general] ; ; Production site configuration data. ; locale = sv_SE ... 变成 [production : general] locale
[production : general]
;
; Production site configuration data.
;
locale = sv_SE
...
变成
[production : general]
locale = sv_SE
...
我意识到“新”配置是基于Zend_Config对象中保存的值编写的,该对象不包含任何注释或平淡的行,但这使得新配置非常难以阅读,尤其是对于我的同事
这个问题能解决吗?我想到的最好的方法是使用具有“级联”继承的不同部分,但这似乎是一个愚蠢的想法正如您所说,
Zend\u Config\u Writer
不会呈现任何注释,因为它们不存储在Zend\u Config
对象中。根据要呈现的ini文件的结构,您可以使用“级联”,至少可以清除冗余(我觉得这并不愚蠢,即使在标准的application.ini
config文件中也是如此)
当然,另一种解决方案可能是使用或创建其他东西来编写ini文件,但这可能会有些过分
希望这能有所帮助,经过一些实验,我用以下方法解决了我的问题,并成功地进行了测试
/* 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
}
是的,它在标准中使用,我在自己的应用程序中使用,但要用节替换我的注释,我需要有一个常规节,然后是继承常规的Zend配置会话,然后是php配置节,我的应用程序连接到的不同数据库和服务的用户名/密码的许多不同部分,等等。我最终会有18个不同的部分,所有的部分都应该继承上面的部分。现在我已经通过使用两个配置文件解决了这个问题,这样我就可以在主文件中保留大部分注释。