From f1008e8730cb81ba8c1bef6bffebb0020feb4e3a Mon Sep 17 00:00:00 2001 From: Daniel Love Date: Thu, 19 May 2022 09:43:44 -0600 Subject: [PATCH 1/2] Adding support for `DATABASE_NAME` variables. Updating documentation & change log. --- CHANGELOG.md | 9 ++++++++- README.md | 6 ++++++ src/Dotenv.php | 18 +++++++++++------- 3 files changed, 25 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c0fc9ea..8175a6e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 @@ -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 diff --git a/README.md b/README.md index a9dbaba..950eda6 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/src/Dotenv.php b/src/Dotenv.php index b3281fc..453680e 100644 --- a/src/Dotenv.php +++ b/src/Dotenv.php @@ -255,13 +255,17 @@ public function getDatabaseName(): string } $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"); + // 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'); } } From 788ae50b319c91fd1a12c1f8cef6a325c5dd1234 Mon Sep 17 00:00:00 2001 From: Daniel Love Date: Tue, 24 May 2022 08:31:22 -0600 Subject: [PATCH 2/2] Fixing empty database url result bug in DotEnv::getDatabaseName method. --- src/Dotenv.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Dotenv.php b/src/Dotenv.php index 453680e..f220c92 100644 --- a/src/Dotenv.php +++ b/src/Dotenv.php @@ -254,7 +254,7 @@ public function getDatabaseName(): string return $this->databaseName; } $result = parse_url($this->get('database_url'), PHP_URL_PATH); - if (NULL === $result || trim($result) === '/') { + 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) {