Skip to content
This repository was archived by the owner on Apr 15, 2025. It is now read-only.

Commit 013ccaf

Browse files
authored
Merge pull request #1 from rootinc/develop
Develop
2 parents afdd4df + 99a3cf1 commit 013ccaf

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

README.md

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,25 @@ This will overwrite database connection values for `host`, `port`, `username`, `
2626

2727

2828
## Customization
29-
Override that by publishing the config file and setting it's values in `config/db-uri.php`.
29+
Override default behaviour by publishing the config file and setting values.
3030

31-
`php artisan vendor:publish --tag=db-uri`
31+
`$ php artisan vendor:publish --tag=db-uri`
3232

33+
Set any database connections by supplying `default` or the key path, like `connections.pgsql`, or `redis`
34+
to have a URL mapped onto the connection at that key path.
35+
36+
config/db-uri.php
3337
```
34-
// config/db-uri.php
3538
return [
36-
'default' => 'DATABASE_URL', // default
37-
'mysql' => 'OTHER_MYSQL_URL', // custom override for the mysql driver
39+
'default' => 'SOME_DATABASE_URL', // "default" resolves key path from default key
40+
'connections.pgsql' => 'OTHER_PGSQL_URL', // Set the "pgsql" driver with different URL. Same when "default" set to "pgsql"
41+
'connections.mysql' => 'OTHER_MYSQL_URL', // Set the "mysql" driver with different URL
3842
];
3943
```
44+
.env
45+
```
46+
DATABASE_URL=postgresql://username:password@localhost:5432/database-name
47+
OTHER_MYSQL_URL=mysql://username:password@localhost:3306/db_example
48+
```
49+
4050

src/LaravelDbUriServiceProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,23 @@ public function register()
4444
$connections = config('db-uri');
4545

4646
// Loop and set each connection
47-
foreach($connections as $driver => $connection_key) {
47+
foreach($connections as $driver_path => $connection_key) {
4848

4949
// If "default" driver, look up the value for the actual connection
50-
$driver = $driver === 'default' ? config('database.default') : $driver;
50+
// "default" resolves to something like "connections.pgsql"
51+
$driver_path = $driver_path === 'default' ? 'connections.' . config('database.default') : $driver_path;
5152

5253
// Get the DATABASE_URL env value or skip out
53-
if(empty($url = env($connection_key))) return;
54+
if(empty($url = env($connection_key))) continue;
5455

5556
// Try to parse it
5657
if(!$components = parse_url($url)) throw new \Exception('Database URL may be malformed.');
5758

5859
// Set each config
5960
foreach($this->config_map as $component_key => $config_key) {
60-
config(["database.connections.{$driver}.{$config_key}" => $this->clean($component_key, $components[$component_key])]);
61+
// Skip setting when no value
62+
if(empty($components[$component_key])) continue;
63+
config(["database.{$driver_path}.{$config_key}" => $this->clean($component_key, $components[$component_key])]);
6164
}
6265
}
6366
}

0 commit comments

Comments
 (0)