github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/pkg/ddevapp/laravel.go (about)

     1  package ddevapp
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/drud/ddev/pkg/fileutil"
     6  	"github.com/drud/ddev/pkg/nodeps"
     7  	"github.com/drud/ddev/pkg/util"
     8  	"os"
     9  	"path/filepath"
    10  )
    11  
    12  const (
    13  	// WarnTypeAbsent warns if type is absent
    14  	WarnTypeAbsent = iota
    15  	// WarnTypeNotConfigured warns if type not configured
    16  	WarnTypeNotConfigured = iota
    17  )
    18  
    19  // isLaravelApp returns true if the app is of type laravel
    20  func isLaravelApp(app *DdevApp) bool {
    21  	return fileutil.FileExists(filepath.Join(app.AppRoot, "artisan"))
    22  }
    23  
    24  func laravelPostStartAction(app *DdevApp) error {
    25  	// We won't touch env if disable_settings_management: true
    26  	if app.DisableSettingsManagement {
    27  		return nil
    28  	}
    29  	envFilePath := filepath.Join(app.AppRoot, ".env")
    30  	_, envText, err := ReadProjectEnvFile(envFilePath)
    31  	if err != nil && !os.IsNotExist(err) {
    32  		return fmt.Errorf("Unable to read .env file: %v", err)
    33  	}
    34  	if os.IsNotExist(err) {
    35  		err = fileutil.CopyFile(filepath.Join(app.AppRoot, ".env.example"), filepath.Join(app.AppRoot, ".env"))
    36  		if err != nil {
    37  			util.Debug("laravel: .env.example does not exist yet, not trying to process it")
    38  			return nil
    39  		}
    40  		_, envText, err = ReadProjectEnvFile(envFilePath)
    41  		if err != nil {
    42  			return err
    43  		}
    44  	}
    45  	port := "3306"
    46  	dbConnection := "mysql"
    47  	if app.Database.Type == nodeps.Postgres {
    48  		dbConnection = "pgsql"
    49  		port = "5432"
    50  	}
    51  	envMap := map[string]string{
    52  		"APP_URL":       app.GetPrimaryURL(),
    53  		"DB_HOST":       "db",
    54  		"DB_PORT":       port,
    55  		"DB_DATABASE":   "db",
    56  		"DB_USERNAME":   "db",
    57  		"DB_PASSWORD":   "db",
    58  		"DB_CONNECTION": dbConnection,
    59  	}
    60  	err = WriteProjectEnvFile(envFilePath, envMap, envText)
    61  	if err != nil {
    62  		return err
    63  	}
    64  
    65  	return nil
    66  }