Skip to content

Commit

Permalink
Merge pull request #4 from unleashedtech/feature/database-name-var
Browse files Browse the repository at this point in the history
Adding support for `DATABASE_NAME` variables.
  • Loading branch information
dl-unleashed-technologies authored May 24, 2022
2 parents 7a2036c + 788ae50 commit 055dfec
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 9 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi

## [Unreleased][unreleased]

## [0.3.1] - 2022-05-19

### Changed

- Added support for `DATABASE_NAME` variables.

## [0.3.0] - 2022-05-17

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

**Initial release!**

[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.3.0...main
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.3.1...main
[0.3.1]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.1...0.3.1
[0.3.0]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.1...0.3.0
[0.2.1]: https://github.com/unleashedtech/dotenv-drupal/compare/0.2.0...0.2.1
[0.2.0]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.17...0.2.0
Expand Down
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ $dotenv->setAppName('earth');
// ...
```

The database name for a site can be overridden via definition of a `{{ app }}__{{ site }}__DATABASE_NAME`
environment variable:
```dotenv
EARTH__ANTARCTICA__DATABASE_NAME=ant
```

#### FILE_PUBLIC_PATH
Allows you to override the default `$settings['file_public_path']` value:

Expand Down
20 changes: 12 additions & 8 deletions src/Dotenv.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,18 @@ public function getDatabaseName(): string
return $this->databaseName;
}
$result = parse_url($this->get('database_url'), PHP_URL_PATH);
if (NULL === $result || trim($result) === '/') {
// Multi-site configuration detected. Use the site name.
$result = $this->getSiteName();
if ($result === 'default' && !$this->isMultiSiteDefaultSiteAllowed()) {
if (PHP_SAPI === 'cli') {
throw new \Exception('The "default" site in this multi-site install is not allowed. Please run something like `drush -l {{site}}` instead.');
} else {
header("HTTP/1.1 401 Unauthorized");
if (NULL === $result || trim($result) === '/' || trim($result) === '') {
// Multi-site configuration detected. Try to use the DATABASE_NAME variable.
$result = $this->get('database_name');
if (! $result) {
// Fall back to using the site name.
$result = $this->getSiteName();
if ($result === 'default' && !$this->isMultiSiteDefaultSiteAllowed()) {
if (PHP_SAPI === 'cli') {
throw new \DomainException('The "default" site in this multi-site install is not allowed. Please run something like `drush -l {{site}}` instead.');
}

\header('HTTP/1.1 401 Unauthorized');
die('Unauthorized');
}
}
Expand Down

0 comments on commit 055dfec

Please sign in to comment.