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

Allow to use a database DSN instead of all the database variables #414

Merged
merged 9 commits into from
Feb 6, 2019
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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_
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 12 additions & 4 deletions config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,18 @@
/**
* DB settings
*/
Config::define('DB_NAME', env('DB_NAME'));
Config::define('DB_USER', env('DB_USER'));
Config::define('DB_PASSWORD', env('DB_PASSWORD'));
Config::define('DB_HOST', env('DB_HOST') ?: 'localhost');
$hasDsn = env('DATABASE_URL') !== null ? true : false;
tristanbes marked this conversation as resolved.
Show resolved Hide resolved

if (true === $hasDsn) {
tristanbes marked this conversation as resolved.
Show resolved Hide resolved
$databaseDsn = parse_url(env('DATABASE_URL'));
tristanbes marked this conversation as resolved.
Show resolved Hide resolved
$dbName = substr($databaseDsn['path'], 1);
$dbHost = isset($databaseDsn['port']) ? $databaseDsn['host'] . ":" . $databaseDsn['port'] : $databaseDsn['host'];
}

Config::define('DB_NAME', $hasDsn ? $dbName : env('DB_NAME'));
tristanbes marked this conversation as resolved.
Show resolved Hide resolved
Config::define('DB_USER', $hasDsn ? $databaseDsn['user'] : env('DB_USER'));
Config::define('DB_PASSWORD', $hasDsn ? ($databaseDsn['pass'] ?? null) : env('DB_PASSWORD'));
Config::define('DB_HOST', $hasDsn ? $dbHost : env('DB_HOST') ?: 'localhost');
Config::define('DB_CHARSET', 'utf8mb4');
Config::define('DB_COLLATE', '');
$table_prefix = env('DB_PREFIX') ?: 'wp_';
Expand Down