#add jira & message sync
This commit is contained in:
@@ -12,16 +12,23 @@ class EnvService
|
||||
{
|
||||
private string $projectsPath;
|
||||
private string $envStoragePath;
|
||||
private string $backupStoragePath;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->projectsPath = '/var/www/Project';
|
||||
$this->projectsPath = '/home/tradewind/Projects';
|
||||
$this->envStoragePath = storage_path('app/env');
|
||||
|
||||
$this->backupStoragePath = storage_path('app/env/backups');
|
||||
|
||||
// 确保env存储目录存在
|
||||
if (!File::exists($this->envStoragePath)) {
|
||||
File::makeDirectory($this->envStoragePath, 0755, true);
|
||||
}
|
||||
|
||||
// 确保备份存储目录存在
|
||||
if (!File::exists($this->backupStoragePath)) {
|
||||
File::makeDirectory($this->backupStoragePath, 0755, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -55,7 +62,7 @@ class EnvService
|
||||
public function getProjectEnvs(string $projectName): array
|
||||
{
|
||||
$projectEnvPath = $this->envStoragePath . '/' . $projectName;
|
||||
|
||||
|
||||
if (!File::exists($projectEnvPath)) {
|
||||
return [];
|
||||
}
|
||||
@@ -85,13 +92,13 @@ class EnvService
|
||||
{
|
||||
|
||||
$projectEnvPath = $this->envStoragePath . '/' . $projectName;
|
||||
|
||||
|
||||
if (!File::exists($projectEnvPath)) {
|
||||
File::makeDirectory($projectEnvPath, 0755, true);
|
||||
}
|
||||
|
||||
$filePath = $projectEnvPath . '/' . $env . '.env';
|
||||
|
||||
|
||||
return File::put($filePath, $content) !== false;
|
||||
}
|
||||
|
||||
@@ -125,10 +132,9 @@ class EnvService
|
||||
$envContent = $this->getEnvContent($projectName, $env);
|
||||
$targetEnvPath = $projectPath . '/.env';
|
||||
|
||||
// 备份现有的.env文件
|
||||
// 备份现有的.env文件到当前项目的storage目录
|
||||
if (File::exists($targetEnvPath)) {
|
||||
$backupPath = $targetEnvPath . '.backup.' . Carbon::now()->format('Y-m-d-H-i-s');
|
||||
File::copy($targetEnvPath, $backupPath);
|
||||
$this->backupCurrentEnv($projectName);
|
||||
}
|
||||
|
||||
return File::put($targetEnvPath, $envContent) !== false;
|
||||
@@ -172,7 +178,7 @@ class EnvService
|
||||
{
|
||||
|
||||
$projectEnvPath = $this->projectsPath . '/' . $projectName . '/.env';
|
||||
|
||||
|
||||
if (!File::exists($projectEnvPath)) {
|
||||
return null;
|
||||
}
|
||||
@@ -221,4 +227,113 @@ class EnvService
|
||||
|
||||
return File::put($filePath, $defaultContent) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 备份项目当前的.env文件到storage目录
|
||||
*/
|
||||
private function backupCurrentEnv(string $projectName): bool
|
||||
{
|
||||
$projectPath = $this->projectsPath . '/' . $projectName;
|
||||
$currentEnvPath = $projectPath . '/.env';
|
||||
|
||||
if (!File::exists($currentEnvPath)) {
|
||||
return true; // 没有.env文件,无需备份
|
||||
}
|
||||
|
||||
// 创建项目备份目录
|
||||
$projectBackupPath = $this->backupStoragePath . '/' . $projectName;
|
||||
if (!File::exists($projectBackupPath)) {
|
||||
File::makeDirectory($projectBackupPath, 0755, true);
|
||||
}
|
||||
|
||||
// 生成备份文件名
|
||||
$timestamp = Carbon::now()->format('Y-m-d-H-i-s');
|
||||
$backupFileName = "env-backup-{$timestamp}.env";
|
||||
$backupPath = $projectBackupPath . '/' . $backupFileName;
|
||||
|
||||
return File::copy($currentEnvPath, $backupPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取项目的所有备份文件
|
||||
*/
|
||||
public function getProjectBackups(string $projectName): array
|
||||
{
|
||||
$projectBackupPath = $this->backupStoragePath . '/' . $projectName;
|
||||
|
||||
if (!File::exists($projectBackupPath)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$backups = [];
|
||||
$files = File::files($projectBackupPath);
|
||||
|
||||
foreach ($files as $file) {
|
||||
if ($file->getExtension() === 'env' && str_starts_with($file->getFilename(), 'env-backup-')) {
|
||||
$backups[] = [
|
||||
'name' => $file->getFilenameWithoutExtension(),
|
||||
'file_path' => $file->getPathname(),
|
||||
'size' => $file->getSize(),
|
||||
'created_at' => Carbon::createFromTimestamp($file->getMTime())->format('Y-m-d H:i:s')
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
// 按创建时间倒序排列
|
||||
usort($backups, function ($a, $b) {
|
||||
return strcmp($b['created_at'], $a['created_at']);
|
||||
});
|
||||
|
||||
return $backups;
|
||||
}
|
||||
|
||||
/**
|
||||
* 恢复备份文件到项目
|
||||
*/
|
||||
public function restoreBackup(string $projectName, string $backupName): bool
|
||||
{
|
||||
$projectPath = $this->projectsPath . '/' . $projectName;
|
||||
|
||||
if (!File::exists($projectPath)) {
|
||||
throw new InvalidArgumentException("项目不存在: {$projectName}");
|
||||
}
|
||||
|
||||
$backupPath = $this->backupStoragePath . '/' . $projectName . '/' . $backupName . '.env';
|
||||
|
||||
if (!File::exists($backupPath)) {
|
||||
throw new InvalidArgumentException("备份文件不存在: {$backupName}");
|
||||
}
|
||||
|
||||
$targetEnvPath = $projectPath . '/.env';
|
||||
|
||||
// 在恢复前先备份当前的.env文件
|
||||
if (File::exists($targetEnvPath)) {
|
||||
$this->backupCurrentEnv($projectName);
|
||||
}
|
||||
|
||||
$backupContent = File::get($backupPath);
|
||||
return File::put($targetEnvPath, $backupContent) !== false;
|
||||
}
|
||||
|
||||
/**
|
||||
* 删除备份文件
|
||||
*/
|
||||
public function deleteBackup(string $projectName, string $backupName): bool
|
||||
{
|
||||
$backupPath = $this->backupStoragePath . '/' . $projectName . '/' . $backupName . '.env';
|
||||
|
||||
if (!File::exists($backupPath)) {
|
||||
return true; // 文件不存在,视为删除成功
|
||||
}
|
||||
|
||||
return File::delete($backupPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取备份存储路径
|
||||
*/
|
||||
public function getBackupStoragePath(): string
|
||||
{
|
||||
return $this->backupStoragePath;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user