dootask/app/Module/Docker.php
2025-05-04 12:57:04 +08:00

112 lines
3.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace App\Module;
use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Yaml\Exception\ParseException;
class Docker
{
/**
* 生成docker-compose.yml文件配置
*
* @param string $filePath docker-compose.yml文件路径
* @param string|null $savePath 保存文件路径,为空则覆盖原文件
* @param array $params 可选参数可包含ports、volumes、container_name和config配置
* @return bool 是否生成成功
*/
public static function generateComposeYml(string $filePath, ?string $savePath = null, array $params = []): bool
{
// 应用名称
$appName = basename(dirname($filePath));
$appName = preg_replace('/(?<!^)([A-Z])/', '-$1', $appName);
$appName = 'dootask-app-' . strtolower($appName);
// 网络名称
$networkName = 'dootask-networks-' . env('APP_ID');
try {
// 解析YAML文件
$content = Yaml::parseFile($filePath);
// 确保services部分存在
if (!isset($content['services'])) {
return false;
}
// 设置服务名称
$content['name'] = $appName;
// 删除所有现有网络配置
unset($content['networks']);
// 添加网络配置
$content['networks'][$networkName] = ['external' => true];
// 查找要修改的服务
$mainService = null;
$firstService = null;
foreach ($content['services'] as $name => $service) {
// 记录第一个服务
if ($firstService === null) {
$firstService = $name;
}
// 检查是否有labels.main=true的服务
if (isset($service['labels']['main']) && $service['labels']['main'] === true) {
$mainService = $name;
break;
}
}
// 确定要修改的服务名称
$targetService = $mainService ?? $firstService;
if ($targetService !== null) {
$service = &$content['services'][$targetService];
// 更新网络配置
$service['networks'] = [$networkName];
// 处理ports参数
if (isset($params['ports'])) {
$service['ports'] = $params['ports'];
}
// 处理volumes参数
if (isset($params['volumes'])) {
$service['volumes'] = $params['volumes'];
}
// 处理container_name参数
if (isset($params['container_name'])) {
$service['container_name'] = $params['container_name'];
}
}
// 生成YAML内容
$yamlContent = Yaml::dump($content, 4, 2);
// 替换${xxx}格式变量
if (isset($params['config'])) {
$yamlContent = preg_replace_callback('/\$\{(.*?)\}/', function($matches) use ($params) {
$varName = $matches[1];
return $params['config'][$varName] ?? $matches[0];
}, $yamlContent);
}
// 写回文件
file_put_contents($savePath ?? $filePath, $yamlContent);
return true;
} catch (ParseException) {
// YAML解析错误
return false;
} catch (\Exception) {
// 其他错误
return false;
}
}
}