/** * @NodeAnotation(title="更新类节点") * @FunctionName:putTex * @User: hdd * @Date: 2021/11/26 0026 15:04 */ public function putTex() { // 获取当前文件的上级目录 $con = dirname(__FILE__); // 扫描$con目录下的所有文件 $filename = scandir($con); // 定义一个数组接收文件名 $conname = array(); foreach($filename as $k=>$v){ // 跳过两个特殊目录 continue跳出循环 if($v=="." || $v==".."){continue;} //截取文件名,我只需要文件名不需要后缀;然后存入数组。如果你是需要后缀直接$v即可 $conname[] = substr($v,0,strpos($v,".")); } Db::connect('mysql_admin')->startTrans(); try { //TODO 删除实例 Db::connect('mysql_admin')->query('truncate table admin_class'); $newArr = []; foreach ($conname as $k => $v) { $newArr[$k]['class_name'] = $v; $newArr[$k]['news_url'] = "\app\backend\controller\\$v::class"; } //TODO 新增实例 $res = Db::connect('mysql_admin')->table('admin_class')->insertAll($newArr); if (!$res) { throw new Exception('数据新增失败'); } //TODO 写入实例 $result = []; foreach ($conname as $k => $v) { $result["$v"] = "\app\backend\controller\\$v::class,"; } $new_resilt = []; foreach ($result as $k => $v) { $new_resilt[$k] = trim($v, '"'); } $new_resilt = print_r($new_resilt, true); $new_resilt1 = str_replace('[', "'", $new_resilt); $new_resilt2 = str_replace(']', "'", $new_resilt1); $str = <<<EOT <?php /** * 通过反射获取实例 */ return $new_resilt2; EOT; $file = '/config/regNameSpase.php'; $res = file_put_contents(dirname(dirname(app_path())).$file, $str); if (!$res) { throw new Exception('写入实例失败'); } Db::connect('mysql_admin')->commit(); }catch (\Exception $e) { Db::connect('mysql_admin')->rollback(); return $this -> errorJson([], $e->getMessage()); } return $this -> successJson(); }