在谷歌搜索后,我发现我们可以用 PHP 编辑我们的application.ini。
代码如下:
$config = parse_ini_file(
APPLICATION_PATH . "/configs/application.ini",
TRUE,
INI_SCANNER_RAW
);
$config["production"]["$store.resources.layout.layout"] = "layout";
$layoutPath = 'APPLICATION_PATH "/modules/' . $store . '/layouts/scripts/"';
$config["production"]["$store.resources.layout.layoutPath"] = $layoutPath;
$result = Helper_common::write_ini_file(
$config,
APPLICATION_PATH . "/configs/application.ini",
TRUE
);
这里parse_ini_file 用于从application.ini 文件中检索内容带有常量。
write_ini_file 是我调用来重写application.ini 文件的函数。
这里是write_ini_file 函数:
public static function write_ini_file($assoc_arr, $path, $has_sections=FALSE) {
$content = "";
if ($has_sections) {
foreach ($assoc_arr as $key=>$elem) {
$content .= "[".$key."]\n";
foreach ($elem as $key2=>$elem2) {
if(is_array($elem2))
{
for($i=0;$i<count($elem2);$i++)
{
$content .= $key2."[] = ".$elem2[$i]."\n";
}
}
else if($elem2=="") $content .= $key2." = \n";
else $content .= $key2." = ".$elem2."\n";
}
}
}
else {
foreach ($assoc_arr as $key=>$elem) {
if(is_array($elem))
{
for($i=0;$i<count($elem);$i++)
{
$content .= $key2."[] = ".$elem[$i]."\n";
}
}
else if($elem=="") $content .= $key2." = \n";
else $content .= $key2." = ".$elem."\n";
}
}
if (!$handle = fopen($path, 'w')) {
return false;
}
if (!fwrite($handle, $content)) {
return false;
}
fclose($handle);
return true;
}