/**
* 修改扩展配置文件.
*
* @param array $arr 需要更新或添加的配置
* @param string $file 配置文件名(不需要后辍)
*
* @return bool
*/
function config_set($arr = [], $file = 'dome')
{
if (is_array($arr)) {
// 文件路径
$filepath = Env::get('config_path').$file.'.php';
// 检测是否存在,不存在新建
if (!file_exists($filepath)) {
$conf = '<?php return [];';
file_put_contents($filepath, $conf);
}
// 添加配置项
$conf = include $filepath;
foreach ($arr as $key => $value) {
$conf[$key] = $value;
}
// 修改配置项
$str = "<?php\r\nreturn [\r\n";
foreach ($conf as $key => $value) {
// dump(gettype($value));
switch (gettype($value)) {
case 'string':
$str .= "\t'$key' => '$value',"."\r\n";
break;
case 'number':
$str .= "\t'$key' => $value,"."\r\n";
break;
case 'boolean':
$str .= "\t'$key' => ".($value?'true':'false').","."\r\n";
break;
default:
# code...
break;
}
}
$str .= '];';
// 写入文件
// dump($str);exit;
file_put_contents($filepath, $str);
return true;
} else {
return false;
}
}