github.com/drud/ddev@v1.21.5-alpha1.0.20230226034409-94fcc4b94453/pkg/ddevapp/magento.go (about) 1 package ddevapp 2 3 import ( 4 "fmt" 5 "github.com/drud/ddev/pkg/archive" 6 "github.com/drud/ddev/pkg/fileutil" 7 "github.com/drud/ddev/pkg/nodeps" 8 "github.com/drud/ddev/pkg/output" 9 "github.com/drud/ddev/pkg/util" 10 "os" 11 "path/filepath" 12 ) 13 14 // isMagentoApp returns true if the app is of type magento 15 func isMagentoApp(app *DdevApp) bool { 16 ism1, err := fileutil.FgrepStringInFile(filepath.Join(app.AppRoot, app.Docroot, "README.md"), `Magento - Long Term Support`) 17 if err == nil && ism1 { 18 return true 19 } 20 return false 21 } 22 23 // isMagento2App returns true if the app is of type magento2 24 func isMagento2App(app *DdevApp) bool { 25 ism2, err := fileutil.FgrepStringInFile(filepath.Join(app.AppRoot, app.Docroot, "..", "SECURITY.md"), `https://hackerone.com/magento`) 26 if err == nil && ism2 { 27 return true 28 } 29 return false 30 } 31 32 // createMagentoSettingsFile manages creation and modification of local.xml. 33 func createMagentoSettingsFile(app *DdevApp) (string, error) { 34 35 if fileutil.FileExists(app.SiteSettingsPath) { 36 // Check if the file is managed by ddev. 37 signatureFound, err := fileutil.FgrepStringInFile(app.SiteSettingsPath, nodeps.DdevFileSignature) 38 if err != nil { 39 return "", err 40 } 41 42 // If the signature wasn't found, warn the user and return. 43 if !signatureFound { 44 util.Warning("%s already exists and is managed by the user.", app.SiteSettingsPath) 45 return "", nil 46 } 47 } else { 48 output.UserOut.Printf("No %s file exists, creating one", app.SiteSettingsPath) 49 50 content, err := bundledAssets.ReadFile("magento/local.xml") 51 if err != nil { 52 return "", err 53 } 54 templateVars := map[string]interface{}{"DBHostname": "db"} 55 err = fileutil.TemplateStringToFile(string(content), templateVars, app.SiteSettingsPath) 56 if err != nil { 57 return "", err 58 } 59 } 60 61 return app.SiteDdevSettingsFile, nil 62 } 63 64 // setMagentoSiteSettingsPaths sets the paths to settings.php for templating. 65 func setMagentoSiteSettingsPaths(app *DdevApp) { 66 app.SiteSettingsPath = filepath.Join(app.AppRoot, app.Docroot, "app", "etc", "local.xml") 67 } 68 69 // magentoImportFilesAction defines the magento workflow for importing project files. 70 func magentoImportFilesAction(app *DdevApp, importPath, extPath string) error { 71 destPath := app.GetHostUploadDirFullPath() 72 73 // parent of destination dir should exist 74 if !fileutil.FileExists(filepath.Dir(destPath)) { 75 return fmt.Errorf("unable to import to %s: parent directory does not exist", destPath) 76 } 77 78 // parent of destination dir should be writable. 79 if err := os.Chmod(filepath.Dir(destPath), 0755); err != nil { 80 return err 81 } 82 83 // If the destination path exists, remove it as was warned 84 if fileutil.FileExists(destPath) { 85 if err := os.RemoveAll(destPath); err != nil { 86 return fmt.Errorf("failed to cleanup %s before import: %v", destPath, err) 87 } 88 } 89 90 if isTar(importPath) { 91 if err := archive.Untar(importPath, destPath, extPath); err != nil { 92 return fmt.Errorf("failed to extract provided archive: %v", err) 93 } 94 95 return nil 96 } 97 98 if isZip(importPath) { 99 if err := archive.Unzip(importPath, destPath, extPath); err != nil { 100 return fmt.Errorf("failed to extract provided archive: %v", err) 101 } 102 103 return nil 104 } 105 106 //nolint: revive 107 if err := fileutil.CopyDir(importPath, destPath); err != nil { 108 return err 109 } 110 111 return nil 112 } 113 114 // getMagentoUploadDir will return a custom upload dir if defined, returning a default path if not. 115 func getMagentoUploadDir(app *DdevApp) string { 116 if app.UploadDir == "" { 117 return "media" 118 } 119 120 return app.UploadDir 121 } 122 123 // getMagento2UploadDir will return a custom upload dir if defined, returning a default path if not. 124 func getMagento2UploadDir(app *DdevApp) string { 125 if app.UploadDir == "" { 126 return "media" 127 } 128 129 return app.UploadDir 130 } 131 132 // createMagento2SettingsFile manages creation and modification of app/etc/env.php. 133 func createMagento2SettingsFile(app *DdevApp) (string, error) { 134 135 if fileutil.FileExists(app.SiteSettingsPath) { 136 // Check if the file is managed by ddev. 137 signatureFound, err := fileutil.FgrepStringInFile(app.SiteSettingsPath, nodeps.DdevFileSignature) 138 if err != nil { 139 return "", err 140 } 141 142 // If the signature wasn't found, warn the user and return. 143 if !signatureFound { 144 util.Warning("%s already exists and is managed by the user.", app.SiteSettingsPath) 145 return "", nil 146 } 147 } else { 148 output.UserOut.Printf("No %s file exists, creating one", app.SiteSettingsPath) 149 150 content, err := bundledAssets.ReadFile("magento/env.php") 151 if err != nil { 152 return "", err 153 } 154 155 templateVars := map[string]interface{}{"DBHostname": "db"} 156 err = fileutil.TemplateStringToFile(string(content), templateVars, app.SiteSettingsPath) 157 if err != nil { 158 return "", err 159 } 160 } 161 162 return app.SiteDdevSettingsFile, nil 163 } 164 165 // setMagento2SiteSettingsPaths sets the paths to settings.php for templating. 166 func setMagento2SiteSettingsPaths(app *DdevApp) { 167 app.SiteSettingsPath = filepath.Join(app.AppRoot, app.Docroot, "..", "app", "etc", "env.php") 168 } 169 170 func magentoConfigOverrideAction(app *DdevApp) error { 171 app.PHPVersion = nodeps.PHP74 172 return nil 173 } 174 175 // Latest magento2 requires php8.1 176 func magento2ConfigOverrideAction(app *DdevApp) error { 177 app.PHPVersion = nodeps.PHP81 178 return nil 179 }