Skip to content

Commit 055dfec

Browse files
Merge pull request #4 from unleashedtech/feature/database-name-var
Adding support for `DATABASE_NAME` variables.
2 parents 7a2036c + 788ae50 commit 055dfec

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
44

55
## [Unreleased][unreleased]
66

7+
## [0.3.1] - 2022-05-19
8+
9+
### Changed
10+
11+
- Added support for `DATABASE_NAME` variables.
12+
713
## [0.3.0] - 2022-05-17
814

915
### Changed
@@ -149,7 +155,8 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
149155

150156
**Initial release!**
151157

152-
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.3.0...main
158+
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.3.1...main
159+
[0.3.1]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.1...0.3.1
153160
[0.3.0]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.1...0.3.0
154161
[0.2.1]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.0...0.2.1
155162
[0.2.0]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.17...0.2.0

README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,12 @@ $dotenv->setAppName('earth');
168168
// ...
169169
```
170170

171+
The database name for a site can be overridden via definition of a `{{ app }}__{{ site }}__DATABASE_NAME`
172+
environment variable:
173+
```dotenv
174+
EARTH__ANTARCTICA__DATABASE_NAME=ant
175+
```
176+
171177
#### FILE_PUBLIC_PATH
172178
Allows you to override the default `$settings['file_public_path']` value:
173179

src/Dotenv.php

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -254,14 +254,18 @@ public function getDatabaseName(): string
254254
return $this->databaseName;
255255
}
256256
$result = parse_url($this->get('database_url'), PHP_URL_PATH);
257-
if (NULL === $result || trim($result) === '/') {
258-
// Multi-site configuration detected. Use the site name.
259-
$result = $this->getSiteName();
260-
if ($result === 'default' && !$this->isMultiSiteDefaultSiteAllowed()) {
261-
if (PHP_SAPI === 'cli') {
262-
throw new \Exception('The "default" site in this multi-site install is not allowed. Please run something like `drush -l {{site}}` instead.');
263-
} else {
264-
header("HTTP/1.1 401 Unauthorized");
257+
if (NULL === $result || trim($result) === '/' || trim($result) === '') {
258+
// Multi-site configuration detected. Try to use the DATABASE_NAME variable.
259+
$result = $this->get('database_name');
260+
if (! $result) {
261+
// Fall back to using the site name.
262+
$result = $this->getSiteName();
263+
if ($result === 'default' && !$this->isMultiSiteDefaultSiteAllowed()) {
264+
if (PHP_SAPI === 'cli') {
265+
throw new \DomainException('The "default" site in this multi-site install is not allowed. Please run something like `drush -l {{site}}` instead.');
266+
}
267+
268+
\header('HTTP/1.1 401 Unauthorized');
265269
die('Unauthorized');
266270
}
267271
}

0 commit comments

Comments
 (0)