2021-03-12 17:58:11 +08:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Created by PhpStorm.
|
|
|
|
* User: TaoLer changlin_zhao@qq.com
|
|
|
|
* Date: 2021-03-12
|
|
|
|
* Time: 17:24
|
|
|
|
*/
|
|
|
|
namespace app\common\lib;
|
|
|
|
|
|
|
|
class SetConf
|
|
|
|
{
|
|
|
|
/**
|
2021-03-16 18:18:40 +08:00
|
|
|
* 修改配置
|
|
|
|
* @param string $file
|
|
|
|
* @param array $data
|
|
|
|
* @return \think\response\Json
|
2021-03-12 17:58:11 +08:00
|
|
|
*/
|
2021-03-16 18:18:40 +08:00
|
|
|
function setConfig(string $file,array $data=[])
|
2021-03-12 17:58:11 +08:00
|
|
|
{
|
|
|
|
if (is_array($data)){
|
|
|
|
$fileurl = app()->getConfigPath() . $file.".php";
|
|
|
|
//var_dump( $fileurl);
|
|
|
|
$string = file_get_contents($fileurl); //加载配置文件
|
|
|
|
foreach ($data as $key => $value) {
|
|
|
|
$pats = '/\'' . $key . '\'(.*?)\',/';
|
|
|
|
$reps = "'". $key. "'". " => " . "'".$value ."',";
|
|
|
|
$string = preg_replace($pats, $reps, $string); // 正则查找然后替换
|
|
|
|
}
|
2021-03-16 18:18:40 +08:00
|
|
|
|
|
|
|
try {
|
|
|
|
file_put_contents($fileurl, $string); // 写入配置文件
|
|
|
|
}
|
|
|
|
catch (\Exception $e) {
|
|
|
|
// 这是进行异常捕获
|
|
|
|
//$e->getMessage();
|
|
|
|
return json(['code'=>-1,'msg'=>$fileurl . '无写入权限']);
|
|
|
|
}
|
|
|
|
|
|
|
|
return json(['code'=>0,'msg'=>'配置修改成功']);
|
2021-03-12 17:58:11 +08:00
|
|
|
}else{
|
2021-03-16 18:18:40 +08:00
|
|
|
return json(['code'=>-1,'msg'=>'配置项错误!']);
|
2021-03-12 17:58:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|