2012-04-23 52 views
1

我一直在Zend_Config_Writer玩弄格式化,虽然我可以把它做我想我找到了缺少格式化有点不安,因为什么:提供意见或以Zend_Config_Writer

[production : general] 
; 
; Production site configuration data. 
; 

locale           = sv_SE 
... 

变为

[production : general] 
locale           = sv_SE 
... 

我意识到“新”配置是基于Zend_Config对象中保存的值编写的,并且该对象不包含任何注释或平淡的行,但是这使得新配置非常难以阅读,特别是对于我的公司-工人。

这能解决吗?我想出的最好的方法是使用不同的部分以“级联”继承,但这看起来像一个愚蠢的想法

回答

0

经过一些尝试,我解决我的问题通过以下方式,并成功进行了测试。

  1. 将配置拆分为多个文件。在我的情况下,我有1个大的application.ini,它拥有几乎所有的配置和1个小版本.ini,它包含一些特定于版本的数据。
  2. 创建所有(在我的情况下为2)Zend_Config_ini对象分离但设置allowModification在一个
  3. 使用Zend_Config_Ini-> Merge()功能合并所有配置,然后将其设置为只读
  4. 要更新配置的任何部分,请从该特定ini文件创建一个新的Zend_Config_ini对象,并将其设置为允许修改并跳过扩展区
  5. 更新配置并使用Zend_Config_Writer_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 
    } 
0

正如你所说,Zend_Config_Writer将不会提供任何意见,因为它们不存储在Zend_Config目的。根据你想要渲染的ini文件的结构,你可以使用“级联”,至少清除冗余(它对我来说看起来并不那么愚蠢,即使在标准的application.ini配置文件中也是如此......) 。

当然,另一种解决方案可能是使用或创建其他的东西来写你的ini文件,但它可以矫枉过正。

希望帮助,

+0

是它在标准中使用,我用它在我自己的应用程序,但要取代我的部分意见,我需要有一个大致的部分,然后一个Zend配置会话继承一般情况下,然后一个PHP配置部分,许多不同的部分与我的应用程序连接到的不同数据库和服务的用户名/密码等。我最终会得到18个不同的部分,所有这些部分都应该继承他们之上的那个部分。现在我已经通过使用两个配置文件解决了这个问题,所以我可以将大部分注释保留在主文件中。 – Lobo 2012-04-24 21:57:42