createCodeIgniterSettingsFile creates/updates the .env file for CodeIgniter 4
(app *DdevApp)
| 14 | |
| 15 | // createCodeIgniterSettingsFile creates/updates the .env file for CodeIgniter 4 |
| 16 | func createCodeIgniterSettingsFile(app *DdevApp) (string, error) { |
| 17 | envFilePath := filepath.Join(app.AppRoot, app.ComposerRoot, ".env") |
| 18 | |
| 19 | _, envText, err := ReadProjectEnvFile(envFilePath) |
| 20 | if err != nil && !os.IsNotExist(err) { |
| 21 | return "", fmt.Errorf("unable to read .env file: %v", err) |
| 22 | } |
| 23 | |
| 24 | // If .env doesn't exist, try to copy from env or .env.example |
| 25 | if os.IsNotExist(err) { |
| 26 | envExamplePath := filepath.Join(app.AppRoot, app.ComposerRoot, "env") |
| 27 | if !fileutil.FileExists(envExamplePath) { |
| 28 | envExamplePath = filepath.Join(app.AppRoot, app.ComposerRoot, ".env.example") |
| 29 | } |
| 30 | if fileutil.FileExists(envExamplePath) { |
| 31 | if err := fileutil.CopyFile(envExamplePath, envFilePath); err != nil { |
| 32 | return "", fmt.Errorf("failed to copy %s to .env: %v", envExamplePath, err) |
| 33 | } |
| 34 | _, envText, err = ReadProjectEnvFile(envFilePath) |
| 35 | if err != nil { |
| 36 | return "", err |
| 37 | } |
| 38 | } else { |
| 39 | util.Debug("CodeIgniter: env file does not exist yet, not trying to process it") |
| 40 | return "", nil |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | // Build env map with CodeIgniter settings |
| 45 | envMap := map[string]string{ |
| 46 | "CI_ENVIRONMENT": "development", |
| 47 | "app.baseURL": app.GetPrimaryURL(), |
| 48 | } |
| 49 | |
| 50 | // Only set database configuration if db container is not omitted |
| 51 | if !slices.Contains(app.OmitContainers, "db") { |
| 52 | driver := "MySQLi" |
| 53 | port := "3306" |
| 54 | charset := "utf8mb4" |
| 55 | if app.Database.Type == nodeps.Postgres { |
| 56 | driver = "Postgre" |
| 57 | port = "5432" |
| 58 | charset = "utf8" |
| 59 | } |
| 60 | |
| 61 | envMap["database.default.hostname"] = "db" |
| 62 | envMap["database.default.database"] = "db" |
| 63 | envMap["database.default.username"] = "db" |
| 64 | envMap["database.default.password"] = "db" |
| 65 | envMap["database.default.DBDriver"] = driver |
| 66 | envMap["database.default.port"] = port |
| 67 | envMap["database.default.charset"] = charset |
| 68 | } |
| 69 | |
| 70 | err = WriteProjectEnvFile(envFilePath, envMap, envText) |
| 71 | if err != nil { |
| 72 | return "", err |
| 73 | } |
nothing calls this directly
no test coverage detected