diff --git a/.env.example b/.env.example index 992ef5529a..6b12f4c40e 100644 --- a/.env.example +++ b/.env.example @@ -2,6 +2,10 @@ DB_NAME=database_name DB_USER=database_user DB_PASSWORD=database_password +# Or you can use a DSN (database source name) +# In this case you can leave empty variables DB_NAME, DB_USER, DB_PASSWORD, DB_HOST +# DATABASE_URL=mysql://database_user:database_password@database_host:database_port/database_name + # Optional variables # DB_HOST=localhost # DB_PREFIX=wp_ diff --git a/README.md b/README.md index 6472445590..8bc0b56155 100644 --- a/README.md +++ b/README.md @@ -27,10 +27,12 @@ Much of the philosophy behind Bedrock is inspired by the [Twelve-Factor App](htt $ composer create-project roots/bedrock ``` 2. Update environment variables in the `.env` file: - * `DB_NAME` - Database name - * `DB_USER` - Database user - * `DB_PASSWORD` - Database password - * `DB_HOST` - Database host + * Database variables + * `DB_NAME` - Database name + * `DB_USER` - Database user + * `DB_PASSWORD` - Database password + * `DB_HOST` - Database host + * You can also use a variable `DATABASE_URL` instead of using all the database variables above, that contains a DSN (ex: `mysql://user:password@127.0.0.1:3306/db_name`) * `WP_ENV` - Set to environment (`development`, `staging`, `production`) * `WP_HOME` - Full URL to WordPress home (https://example.com) * `WP_SITEURL` - Full URL to WordPress including subdirectory (https://example.com/wp) diff --git a/config/application.php b/config/application.php index bb06227d15..a4144d7655 100644 --- a/config/application.php +++ b/config/application.php @@ -27,7 +27,10 @@ $dotenv = new Dotenv\Dotenv($root_dir); if (file_exists($root_dir . '/.env')) { $dotenv->load(); - $dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD', 'WP_HOME', 'WP_SITEURL']); + $dotenv->required(['WP_HOME', 'WP_SITEURL']); + if (!env('DATABASE_URL')) { + $dotenv->required(['DB_NAME', 'DB_USER', 'DB_PASSWORD']); + } } /** @@ -60,6 +63,17 @@ Config::define('DB_COLLATE', ''); $table_prefix = env('DB_PREFIX') ?: 'wp_'; +if (env('DATABASE_URL')) { + $database_dsn = parse_url(env('DATABASE_URL')); + $db_name = substr($database_dsn['path'], 1); + $db_host = isset($database_dsn['port']) ? $database_dsn['host'] . ":" . $database_dsn['port'] : $database_dsn['host']; + + Config::define('DB_NAME', $db_name); + Config::define('DB_USER', $database_dsn['user']); + Config::define('DB_PASSWORD', $database_dsn['pass'] ?? null); + Config::define('DB_HOST', $db_host); +} + /** * Authentication Unique Keys and Salts */