Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use standard Drupal/Backdrop default.settings.php as settings.php, fixes #1097 #2500

Merged
merged 4 commits into from
Sep 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/ddev/cmd/packrd/packed-packr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

97 changes: 3 additions & 94 deletions pkg/ddevapp/backdrop.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
"path/filepath"
"text/template"

"io/ioutil"

"github.com/drud/ddev/pkg/archive"
"github.com/drud/ddev/pkg/fileutil"
"github.com/drud/ddev/pkg/output"
Expand Down Expand Up @@ -54,30 +52,6 @@ func NewBackdropSettings(app *DdevApp) *BackdropSettings {
}
}

// backdropMainSettingsTemplate defines the template that will become settings.php in
// the event that one does not already exist.
const backdropMainSettingsTemplate = `<?php
{{ $config := . }}
// {{ $config.Signature }}: Automatically generated Backdrop settings file.

// Providing these in the main settings file is most likely to match the default config_directories.
$database = 'mysql://user:pass@localhost/database_name';
$config_directories['active'] = 'files/config_' . md5($database) . '/active';
$config_directories['staging'] = 'files/config_' . md5($database) . '/staging';
if (file_exists(__DIR__ . '/{{ $config.SiteSettingsDdev }}') && getenv('IS_DDEV_PROJECT') == 'true')) {
include __DIR__ . '/{{ $config.SiteSettingsDdev }}';
}
`

// backdropSettingsAppendTemplate defines the template that will be appended to
// settings.php in the event that one exists.
const backdropSettingsAppendTemplate = `{{ $config := . }}
// Automatically generated include for settings managed by ddev.
if (file_exists(__DIR__ . '/{{ $config.SiteSettingsDdev }}') && getenv('IS_DDEV_PROJECT') == 'true') {
include __DIR__ . '/{{ $config.SiteSettingsDdev }}';
}
`

// BackdropDdevSettingsTemplate defines the template that will become settings.ddev.php.
const BackdropDdevSettingsTemplate = `<?php
{{ $config := . }}
Expand Down Expand Up @@ -112,7 +86,7 @@ func createBackdropSettingsFile(app *DdevApp) (string, error) {

if !fileutil.FileExists(app.SiteSettingsPath) {
output.UserOut.Printf("No %s file exists, creating one", settings.SiteSettings)
if err := writeBackdropMainSettingsFile(settings, app.SiteSettingsPath); err != nil {
if err := writeDrupalSettingsFile(app.SiteSettingsPath, app.Type); err != nil {
return "", err
}
}
Expand All @@ -127,7 +101,7 @@ func createBackdropSettingsFile(app *DdevApp) (string, error) {
} else {
output.UserOut.Printf("Existing %s file does not include %s, modifying to include ddev settings", settings.SiteSettings, settings.SiteSettingsDdev)

if err = appendIncludeToBackdropSettingsFile(settings, app.SiteSettingsPath); err != nil {
if err = appendIncludeToDrupalSettingsFile(app.SiteSettingsPath, app.Type); err != nil {
return "", fmt.Errorf("failed to include %s in %s: %v", settings.SiteSettingsDdev, settings.SiteSettings, err)
}
}
Expand All @@ -139,37 +113,6 @@ func createBackdropSettingsFile(app *DdevApp) (string, error) {
return app.SiteDdevSettingsFile, nil
}

// writeBackdropMainSettingsFile dynamically produces a valid settings.php file by
// combining a configuration object with a data-driven template.
func writeBackdropMainSettingsFile(settings *BackdropSettings, filePath string) error {
tmpl, err := template.New("settings").Funcs(getTemplateFuncMap()).Parse(backdropMainSettingsTemplate)
if err != nil {
return err
}

// Ensure target directory exists and is writable
dir := filepath.Dir(filePath)
if err = os.Chmod(dir, 0755); os.IsNotExist(err) {
if err = os.MkdirAll(dir, 0755); err != nil {
return err
}
} else if err != nil {
return err
}

file, err := os.Create(filePath)
if err != nil {
return err
}
defer util.CheckClose(file)

if err := tmpl.Execute(file, settings); err != nil {
return err
}

return nil
}

// writeBackdropDdevSettingsFile dynamically produces a valid settings.ddev.php file
// by combining a configuration object with a data-driven template.
func writeBackdropDdevSettingsFile(settings *BackdropSettings, filePath string) error {
Expand All @@ -186,7 +129,7 @@ func writeBackdropDdevSettingsFile(settings *BackdropSettings, filePath string)
return nil
}
}
tmpl, err := template.New("settings").Funcs(getTemplateFuncMap()).Parse(BackdropDdevSettingsTemplate)
tmpl, err := template.New("settings.ddev.php").Funcs(getTemplateFuncMap()).Parse(BackdropDdevSettingsTemplate)
if err != nil {
return err
}
Expand Down Expand Up @@ -254,40 +197,6 @@ func backdropPostImportDBAction(app *DdevApp) error {
return nil
}

// appendIncludeToBackdropSettingsFile modifies the settings.php file to include the settings.ddev.php
// file, which contains ddev-specific configuration.
func appendIncludeToBackdropSettingsFile(settings *BackdropSettings, siteSettingsPath string) error {
// Check if file is empty
contents, err := ioutil.ReadFile(siteSettingsPath)
if err != nil {
return err
}

// If the file is empty, write the complete settings template and return
if len(contents) == 0 {
return writeBackdropMainSettingsFile(settings, siteSettingsPath)
}

// The file is not empty, open it for appending
file, err := os.OpenFile(siteSettingsPath, os.O_RDWR|os.O_APPEND, 0644)
if err != nil {
return err
}
defer util.CheckClose(file)

tmpl, err := template.New("settings").Funcs(getTemplateFuncMap()).Parse(backdropSettingsAppendTemplate)
if err != nil {
return err
}

// Write the template to the file
if err := tmpl.Execute(file, settings); err != nil {
return err
}

return nil
}

// backdropImportFilesAction defines the Backdrop workflow for importing project files.
// The Backdrop workflow is currently identical to the Drupal import-files workflow.
func backdropImportFilesAction(app *DdevApp, importPath, extPath string) error {
Expand Down
89 changes: 20 additions & 69 deletions pkg/ddevapp/drupal.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"github.com/drud/ddev/pkg/dockerutil"
"github.com/drud/ddev/pkg/nodeps"
"github.com/gobuffalo/packr/v2"

"github.com/drud/ddev/pkg/output"
"github.com/drud/ddev/pkg/util"
Expand Down Expand Up @@ -64,54 +65,15 @@ func NewDrupalSettings(app *DdevApp) *DrupalSettings {
}
}

// drupal8SettingsTemplate defines the template that will become a Drupal 8 app's settings.php
// in the event that one does not already exist.
const drupal8SettingsTemplate = `<?php
{{ $config := . }}
// {{ $config.Signature }}: Automatically generated Drupal settings file.
if (file_exists($app_root . '/' . $site_path . '/{{ $config.SiteSettingsDdev }}') && getenv('IS_DDEV_PROJECT') == 'true') {
include $app_root . '/' . $site_path . '/{{ $config.SiteSettingsDdev }}';
}
`

// drupal8SettingsAppendTemplate defines the template that will be appended to
// a Drupal 8 app's settings.php in the event that one exists.
const drupal8SettingsAppendTemplate = `{{ $config := . }}
// settingsIncludeStanza defines the template that will be appended to
// a project's settings.php in the event that the file already exists.
const settingsIncludeStanza = `
// Automatically generated include for settings managed by ddev.
if (file_exists($app_root . '/' . $site_path . '/{{ $config.SiteSettingsDdev }}') && getenv('IS_DDEV_PROJECT') == 'true') {
include $app_root . '/' . $site_path . '/{{ $config.SiteSettingsDdev }}';
}
`

// drupal7SettingsTemplate defines the template that will become a Drupal 7
// app's settings.php in the event that one does not already exist.
const drupal7SettingsTemplate = `<?php
{{ $config := . }}
// {{ $config.Signature }}: Automatically generated Drupal settings file.
$ddev_settings = dirname(__FILE__) . '/{{ $config.SiteSettingsDdev }}';
$ddev_settings = dirname(__FILE__) . '/settings.ddev.php';
if (is_readable($ddev_settings) && getenv('IS_DDEV_PROJECT') == 'true') {
require $ddev_settings;
}
`

// drupal7SettingsAppendTemplate defines the template that will be appended to
// a Drupal 7 app's settings.php in the event that one exists.
const drupal7SettingsAppendTemplate = `{{ $config := . }}
// Automatically generated include for settings managed by ddev.
$ddev_settings = dirname(__FILE__) . '/{{ $config.SiteSettingsDdev }}';
if (is_readable($ddev_settings) && getenv('IS_DDEV_PROJECT') == 'true') {
require $ddev_settings;
}
`

// drupal6SettingsTemplate defines the template that will become a Drupal 6
// app's settings.php in the event that one does not already exist.
const drupal6SettingsTemplate = drupal7SettingsTemplate

// drupal7SettingsAppendTemplate defines the template that will be appended to
// a Drupal 7 app's settings.php in the event that one exists.
const drupal6SettingsAppendTemplate = drupal7SettingsAppendTemplate

const (
drupal8DdevSettingsTemplate = `<?php
{{ $config := . }}
Expand Down Expand Up @@ -226,7 +188,7 @@ $db_url = "{{ $config.DatabaseDriver }}://{{ $config.DatabaseUsername }}:{{ $con
)

// manageDrupalSettingsFile will direct inspecting and writing of settings.php.
func manageDrupalSettingsFile(app *DdevApp, drupalConfig *DrupalSettings, settingsTemplate, appendTemplate string) error {
func manageDrupalSettingsFile(app *DdevApp, drupalConfig *DrupalSettings, appType string) error {
// We'll be writing/appending to the settings files and parent directory, make sure we have permissions to do so
if err := drupalEnsureWritePerms(app); err != nil {
return err
Expand All @@ -235,7 +197,7 @@ func manageDrupalSettingsFile(app *DdevApp, drupalConfig *DrupalSettings, settin
if !fileutil.FileExists(app.SiteSettingsPath) {
output.UserOut.Printf("No %s file exists, creating one", drupalConfig.SiteSettings)

if err := writeDrupalSettingsFile(drupalConfig, app.SiteSettingsPath, settingsTemplate); err != nil {
if err := writeDrupalSettingsFile(app.SiteSettingsPath, appType); err != nil {
return fmt.Errorf("failed to write: %v", err)
}
}
Expand All @@ -250,18 +212,18 @@ func manageDrupalSettingsFile(app *DdevApp, drupalConfig *DrupalSettings, settin
} else {
output.UserOut.Printf("Existing %s file does not include %s, modifying to include ddev settings", drupalConfig.SiteSettings, drupalConfig.SiteSettingsDdev)

if err := appendIncludeToDrupalSettingsFile(drupalConfig, app.SiteSettingsPath, appendTemplate); err != nil {
if err := appendIncludeToDrupalSettingsFile(app.SiteSettingsPath, app.Type); err != nil {
return fmt.Errorf("failed to include %s in %s: %v", drupalConfig.SiteSettingsDdev, drupalConfig.SiteSettings, err)
}
}

return nil
}

// writeDrupalSettingsFile creates the app's settings.php or equivalent,
// which does nothing more than import the ddev-managed settings.ddev.php.
func writeDrupalSettingsFile(drupalConfig *DrupalSettings, filePath string, versionTemplate string) error {
tmpl, err := template.New("settings").Funcs(getTemplateFuncMap()).Parse(versionTemplate)
// writeDrupalSettingsFile creates the project's settings.php if it doesn't exist
func writeDrupalSettingsFile(filePath string, appType string) error {
box := packr.New("drupal_settings_packr_assets", "./drupal_settings_packr_assets")
content, err := box.Find(appType + "/settings.php")
if err != nil {
return err
}
Expand All @@ -277,15 +239,10 @@ func writeDrupalSettingsFile(drupalConfig *DrupalSettings, filePath string, vers
}

// Create file
file, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0644)
err = ioutil.WriteFile(filePath, content, 0755)
if err != nil {
return err
}
defer util.CheckClose(file)

if err := tmpl.Execute(file, drupalConfig); err != nil {
return err
}

return nil
}
Expand All @@ -298,7 +255,7 @@ func createDrupal7SettingsFile(app *DdevApp) (string, error) {
// we may want to do some kind of customization in the future.
drupalConfig := NewDrupalSettings(app)

if err := manageDrupalSettingsFile(app, drupalConfig, drupal7SettingsTemplate, drupal7SettingsAppendTemplate); err != nil {
if err := manageDrupalSettingsFile(app, drupalConfig, app.Type); err != nil {
return "", err
}

Expand All @@ -317,7 +274,7 @@ func createDrupal8SettingsFile(app *DdevApp) (string, error) {
// we may want to do some kind of customization in the future.
drupalConfig := NewDrupalSettings(app)

if err := manageDrupalSettingsFile(app, drupalConfig, drupal8SettingsTemplate, drupal8SettingsAppendTemplate); err != nil {
if err := manageDrupalSettingsFile(app, drupalConfig, app.Type); err != nil {
return "", err
}

Expand All @@ -343,7 +300,7 @@ func createDrupal6SettingsFile(app *DdevApp) (string, error) {
// mysqli is required in latest D6LTS and works fine in ddev in old D6
drupalConfig.DatabaseDriver = "mysqli"

if err := manageDrupalSettingsFile(app, drupalConfig, drupal6SettingsTemplate, drupal6SettingsAppendTemplate); err != nil {
if err := manageDrupalSettingsFile(app, drupalConfig, app.Type); err != nil {
return "", err
}

Expand Down Expand Up @@ -751,16 +708,16 @@ func settingsHasInclude(drupalConfig *DrupalSettings, siteSettingsPath string) (

// appendIncludeToDrupalSettingsFile modifies the settings.php file to include the settings.ddev.php
// file, which contains ddev-specific configuration.
func appendIncludeToDrupalSettingsFile(drupalConfig *DrupalSettings, siteSettingsPath string, appendTemplate string) error {
func appendIncludeToDrupalSettingsFile(siteSettingsPath string, appType string) error {
// Check if file is empty
contents, err := ioutil.ReadFile(siteSettingsPath)
if err != nil {
return err
}

// If the file is empty, write the complete settings template and return
// If the file is empty, write the complete settings file and return
if len(contents) == 0 {
return writeDrupalSettingsFile(drupalConfig, siteSettingsPath, appendTemplate)
return writeDrupalSettingsFile(siteSettingsPath, appType)
}

// The file is not empty, open it for appending
Expand All @@ -770,16 +727,10 @@ func appendIncludeToDrupalSettingsFile(drupalConfig *DrupalSettings, siteSetting
}
defer util.CheckClose(file)

tmpl, err := template.New("settings").Funcs(getTemplateFuncMap()).Parse(appendTemplate)
_, err = file.Write([]byte(settingsIncludeStanza))
if err != nil {
return err
}

// Write the template to the file
if err := tmpl.Execute(file, drupalConfig); err != nil {
return err
}

return nil
}

Expand Down