github.com/ddev/ddev@v1.23.2-0.20240519125000-d824ffe36ff3/pkg/ddevapp/laravel.go (about)

     1  package ddevapp
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"github.com/ddev/ddev/pkg/fileutil"
     9  	"github.com/ddev/ddev/pkg/nodeps"
    10  	"github.com/ddev/ddev/pkg/util"
    11  )
    12  
    13  const (
    14  	// WarnTypeAbsent warns if type is absent
    15  	WarnTypeAbsent = iota
    16  	// WarnTypeNotConfigured warns if type not configured
    17  	WarnTypeNotConfigured = iota
    18  )
    19  
    20  // isLaravelApp returns true if the app is of type laravel
    21  func isLaravelApp(app *DdevApp) bool {
    22  	return fileutil.FileExists(filepath.Join(app.AppRoot, "artisan"))
    23  }
    24  
    25  func laravelPostStartAction(app *DdevApp) error {
    26  	// We won't touch env if disable_settings_management: true
    27  	if app.DisableSettingsManagement {
    28  		return nil
    29  	}
    30  	envFilePath := filepath.Join(app.AppRoot, ".env")
    31  	_, envText, err := ReadProjectEnvFile(envFilePath)
    32  	if err != nil && !os.IsNotExist(err) {
    33  		return fmt.Errorf("unable to read .env file: %v", err)
    34  	}
    35  	if os.IsNotExist(err) {
    36  		err = fileutil.CopyFile(filepath.Join(app.AppRoot, ".env.example"), filepath.Join(app.AppRoot, ".env"))
    37  		if err != nil {
    38  			util.Debug("Laravel: .env.example does not exist yet, not trying to process it")
    39  			return nil
    40  		}
    41  		_, envText, err = ReadProjectEnvFile(envFilePath)
    42  		if err != nil {
    43  			return err
    44  		}
    45  	}
    46  	port := "3306"
    47  	dbConnection := "mariadb"
    48  	if app.Database.Type == nodeps.MariaDB {
    49  		hasMariaDbDriver, _ := fileutil.FgrepStringInFile(filepath.Join(app.AppRoot, "config/database.php"), "mariadb")
    50  		if !hasMariaDbDriver {
    51  			// Older versions of Laravel (before 11) use "mysql" driver for MariaDB
    52  			// This change is required to prevent this error on "php artisan migrate":
    53  			// InvalidArgumentException Database connection [mariadb] not configured
    54  			dbConnection = "mysql"
    55  		}
    56  	} else if app.Database.Type == nodeps.MySQL {
    57  		dbConnection = "mysql"
    58  	} else if app.Database.Type == nodeps.Postgres {
    59  		dbConnection = "pgsql"
    60  		port = "5432"
    61  	}
    62  	envMap := map[string]string{
    63  		"APP_URL":       app.GetPrimaryURL(),
    64  		"DB_HOST":       "db",
    65  		"DB_PORT":       port,
    66  		"DB_DATABASE":   "db",
    67  		"DB_USERNAME":   "db",
    68  		"DB_PASSWORD":   "db",
    69  		"DB_CONNECTION": dbConnection,
    70  		"MAIL_MAILER":   "smtp",
    71  		"MAIL_HOST":     "127.0.0.1",
    72  		"MAIL_PORT":     "1025",
    73  	}
    74  	err = WriteProjectEnvFile(envFilePath, envMap, envText)
    75  	if err != nil {
    76  		return err
    77  	}
    78  
    79  	return nil
    80  }
    81  
    82  // laravelConfigOverrideAction would require PHP8.2
    83  // but that is now the default for DDEV v1.23+
    84  //func laravelConfigOverrideAction(app *DdevApp) error {
    85  //	app.PHPVersion = nodeps.PHP82
    86  //	return nil
    87  //}