projectsPath = '/var/www/Project'; $this->envStoragePath = storage_path('app/env'); // 确保env存储目录存在 if (!File::exists($this->envStoragePath)) { File::makeDirectory($this->envStoragePath, 0755, true); } } /** * 获取所有项目列表 */ public function getProjects(): array { if (!File::exists($this->projectsPath)) { return []; } $projects = []; $directories = File::directories($this->projectsPath); foreach ($directories as $directory) { $projectName = basename($directory); $projects[] = [ 'name' => $projectName, 'path' => $directory, 'has_env' => File::exists($directory . '/.env'), 'envs' => $this->getProjectEnvs($projectName) ]; } return $projects; } /** * 获取指定项目的所有环境配置 */ public function getProjectEnvs(string $projectName): array { $projectEnvPath = $this->envStoragePath . '/' . $projectName; if (!File::exists($projectEnvPath)) { return []; } $envs = []; $files = File::files($projectEnvPath); foreach ($files as $file) { if ($file->getExtension() === 'env') { $envName = $file->getFilenameWithoutExtension(); $envs[] = [ 'name' => $envName, 'file_path' => $file->getPathname(), 'size' => $file->getSize(), 'modified_at' => Carbon::createFromTimestamp($file->getMTime())->format('Y-m-d H:i:s') ]; } } return $envs; } /** * 保存环境配置文件 */ public function saveEnv(string $projectName, string $env, string $content): bool { $projectEnvPath = $this->envStoragePath . '/' . $projectName; if (!File::exists($projectEnvPath)) { File::makeDirectory($projectEnvPath, 0755, true); } $filePath = $projectEnvPath . '/' . $env . '.env'; return File::put($filePath, $content) !== false; } /** * 获取环境配置文件内容 */ public function getEnvContent(string $projectName, string $env): string { $filePath = $this->envStoragePath . '/' . $projectName . '/' . $env . '.env'; if (!File::exists($filePath)) { throw new InvalidArgumentException("环境配置文件不存在: {$env}"); } return File::get($filePath); } /** * 应用环境配置到项目 */ public function applyEnv(string $projectName, string $env): bool { $projectPath = $this->projectsPath . '/' . $projectName; if (!File::exists($projectPath)) { throw new InvalidArgumentException("项目不存在: {$projectName}"); } $envContent = $this->getEnvContent($projectName, $env); $targetEnvPath = $projectPath . '/.env'; // 备份现有的.env文件 if (File::exists($targetEnvPath)) { $backupPath = $targetEnvPath . '.backup.' . Carbon::now()->format('Y-m-d-H-i-s'); File::copy($targetEnvPath, $backupPath); } return File::put($targetEnvPath, $envContent) !== false; } /** * 删除环境配置文件 */ public function deleteEnv(string $projectName, string $env): bool { $filePath = $this->envStoragePath . '/' . $projectName . '/' . $env . '.env'; if (!File::exists($filePath)) { return true; // 文件不存在,视为删除成功 } return File::delete($filePath); } /** * 从项目导入当前.env文件 */ public function importFromProject(string $projectName, string $env): bool { $projectEnvPath = $this->projectsPath . '/' . $projectName . '/.env'; if (!File::exists($projectEnvPath)) { throw new InvalidArgumentException("项目 {$projectName} 没有.env文件"); } $content = File::get($projectEnvPath); return $this->saveEnv($projectName, $env, $content); } /** * 获取项目当前.env文件内容 */ public function getCurrentProjectEnv(string $projectName): ?string { $projectEnvPath = $this->projectsPath . '/' . $projectName . '/.env'; if (!File::exists($projectEnvPath)) { return null; } return File::get($projectEnvPath); } /** * 获取存储路径 */ public function getStoragePath(): string { return $this->envStoragePath; } /** * 获取项目路径 */ public function getProjectsPath(): string { return $this->projectsPath; } /** * 创建新的环境配置文件 */ public function createNewEnv(string $projectName, string $envName): bool { $projectEnvPath = $this->envStoragePath . '/' . $projectName; if (!File::exists($projectEnvPath)) { File::makeDirectory($projectEnvPath, 0755, true); } $filePath = $projectEnvPath . '/' . $envName . '.env'; // 检查文件是否已存在 if (File::exists($filePath)) { throw new InvalidArgumentException("环境配置文件已存在: {$envName}"); } // 创建空的配置文件 $defaultContent = "# {$envName} 环境配置\n# 创建时间: " . Carbon::now()->format('Y-m-d H:i:s') . "\n\n"; return File::put($filePath, $defaultContent) !== false; } }