true]; foreach ($content['services'] as &$service) { // 确保所有服务都有网络配置 $service['networks'] = [$networkName]; // 处理现有的volumes配置 if (isset($service['volumes'])) { $service['volumes'] = array_map(function($volume) use ($appName) { if (str_starts_with($volume, './') || str_starts_with($volume, '../')) { // 替换相对路径为绝对路径 return '${HOST_PWD}/docker/apps/' . $appName . '/' . ltrim($volume, './'); } return $volume; }, $service['volumes']); } } // 生成YAML内容 $yamlContent = Yaml::dump($content, 4, 2); // 替换${XXX}格式变量 $yamlContent = preg_replace_callback('/\$\{(.*?)\}/', function($matches) use ($params) { return $params[$matches[1]] ?? $matches[0]; }, $yamlContent); // 写回文件 file_put_contents($savePath ?? $filePath, $yamlContent); return true; } catch (ParseException) { // YAML解析错误 return false; } catch (\Exception) { // 其他错误 return false; } } /** * 执行docker-compose up|down命令 * @param string $appName * @param string $command * @return array */ public static function dockerComposeUp(string $appName, string $command = 'up'): array { $url = "http://host.docker.internal:" . env("APPS_PORT") . "/apps/{$command}/{$appName}"; $extra = [ 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . env('APP_KEY'), ]; $res = Ihttp::ihttp_request($url, [], $extra); if (Base::isError($res)) { return Base::retError("请求错误", $res); } $resData = Base::json2array($res['data']); if ($resData['code'] != 200) { return Base::retError("请求失败", $resData); } return Base::retSuccess("success", $resData['data']); } }