Skip to content

Commit

Permalink
Merge pull request #414 from tristanbes/feature/database-as-dsn
Browse files Browse the repository at this point in the history
Allow to use a database DSN instead of all the database variables
  • Loading branch information
austinpray authored Feb 6, 2019
2 parents 785c64f + 6590c06 commit 718f5f7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 5 deletions.
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:[email protected]: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: 15 additions & 1 deletion config/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
}
}

/**
Expand Down Expand Up @@ -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
*/
Expand Down

0 comments on commit 718f5f7

Please sign in to comment.