Skip to content

Commit e1cb790

Browse files
Fixed Dotenv::getDatabaseName method to use site name if no database specified in DATABASE_URL. Updating change log. Removing older unnecessary methods. Updating documentation.
1 parent 7261814 commit e1cb790

File tree

3 files changed

+32
-48
lines changed

3 files changed

+32
-48
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.1.8] - 2021-12-14
8+
9+
### Fixed
10+
11+
- Fixed `Dotenv::getDatabaseName` method to use site name if no database specified in `DATABASE_URL`.
12+
713
## [0.1.7] - 2021-12-01
814

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

5965
**Initial release!**
6066

61-
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.7...main
67+
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.8...main
68+
[0.1.7]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.7...0.1.8
6269
[0.1.7]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.6...0.1.7
6370
[0.1.6]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.5...0.1.6
6471
[0.1.5]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.4...0.1.5

README.md

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# Environment-Based Drupal Settings
22
This package extends Symfony's [symfony/dotenv](https://symfony.com/components/Dotenv)
3-
component to allow streamlined Drupal config via `.env` files. Please refer to the
4-
component's documentation about how `.env` files are used.
3+
component to allow streamlined Drupal config via [Environment Variables](https://en.wikipedia.org/wiki/Environment_variable).
4+
Please refer to the Symfony component's documentation about how `.env` files
5+
should be used. It is important to note that `.env` files are _ignored_ if the
6+
`APP_ENV` var has already been set by the server. For performance purposes,
7+
production environments should ideally rely on pre-configured environment variables,
8+
rather than environment variable values loaded from `.env` files.
59

610
## Installation
711

@@ -69,7 +73,11 @@ $dotenv = new Dotenv();
6973
$sites = $dotenv->getSites();
7074
```
7175

72-
#### Configuring Drupal via ENV Variables
76+
#### Installation Conclusion
77+
That's it! Drupal will now attempt to load essential connection information from
78+
Environment Variables.
79+
80+
### Configuring Drupal via ENV Variables
7381
This package will provide many default setting & configuration values based on the
7482
detected environment. Some of these values can be populated by environment variables.
7583

@@ -83,7 +91,7 @@ configuration.
8391
* [SOLR_URL](#solr_url)
8492
* More configuration options coming soon!
8593

86-
##### DATABASE_URL
94+
#### DATABASE_URL
8795
The default database connection can be configured via a [DSN](https://en.wikipedia.org/wiki/Data_source_name):
8896

8997
```dotenv
@@ -96,27 +104,27 @@ For example:
96104
DATABASE_URL=mysql://foo:bar@host:3306/baz
97105
```
98106

99-
For multi-site installations, do _not_ specify a database name in the ENV file(s):
107+
For multi-site installations, do _not_ specify a database name in the `DATABASE_URL` variable:
100108

101109
```dotenv
102110
DATABASE_URL=mysql://foo:bar@host:3306
103111
```
104112

105-
##### DOMAINS
113+
#### DOMAINS
106114
A CSV list of domains used by the given environment:
107115

108116
```dotenv
109117
DOMAINS=foo.example,bar.example,baz.example
110118
```
111119

112-
##### SITES
120+
#### SITES
113121
A CSV list of Drupal "sites" (e.g. "subdomains") used by the given environment:
114122

115123
```dotenv
116124
SITES=foo,bar,baz,qux
117125
```
118126

119-
##### SOLR_URL
127+
#### SOLR_URL
120128
The default Solr connection can be configured via a [DSN](https://en.wikipedia.org/wiki/Data_source_name):
121129

122130
```dotenv
@@ -136,8 +144,8 @@ so the shorter DSN can be used.
136144

137145
Streamlined environment-dependent configuration of _one_ Solr core is supported at this time.
138146

139-
##### Supported Placeholders
140-
* `{{app_path}}`: The path where Drupal is located.
141-
* `{{project_path}}`: The path where the project is located.
142-
* `{{site_name}}`: The name of the Drupal site. Defaults to `default`.
143-
* `{{virtual_host}}`: The value of `$_SERVER['VIRTUAL_HOST']`.
147+
#### Configuration Conclusion
148+
With these few Environment Variables, you will be able to configure Drupal in a streamlined
149+
fashion similar to the way Symfony is configured. Support for integration of many more common
150+
integrations can be expected soon. Please consider creating a Pull Request with integration(s)
151+
you would like to see added.

src/Dotenv.php

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public function getDatabaseName(): ?string
170170
return $this->databaseName;
171171
}
172172
$result = parse_url($_SERVER['DATABASE_URL'], PHP_URL_PATH);
173-
return (FALSE === $result) ? $this->getSiteName() : substr($result, 1);
173+
return (FALSE === $result || '/' === $result) ? $this->getSiteName() : substr($result, 1);
174174
}
175175

176176
function setDatabaseName(string $database)
@@ -225,7 +225,8 @@ public function getSettings(): array
225225
* @return array
226226
* The Drupal-multi-site $sites array, based on environment variables.
227227
*/
228-
public function getSites(): array {
228+
public function getSites(): array
229+
{
229230
$domains = explode(',', $_SERVER['DOMAINS'] ?? 'default.example');
230231
$sites = explode(',', $_SERVER['SITES'] ?? 'default');
231232
foreach ($sites as $site) {
@@ -236,24 +237,6 @@ public function getSites(): array {
236237
return $sites;
237238
}
238239

239-
/**
240-
* Gets the absolute path for the given path.
241-
*
242-
* @param string $path
243-
* The path to resolve.
244-
*
245-
* @return string
246-
* The absolute version of the path.
247-
*/
248-
function buildPath(string $path): string
249-
{
250-
$path = $this->replacePlaceholders($path);
251-
if (str_starts_with($path, '/')) {
252-
return $path;
253-
}
254-
return realpath(DRUPAL_ROOT . '/../' . $path);
255-
}
256-
257240
public function getAppPath(): string
258241
{
259242
return DRUPAL_ROOT;
@@ -279,18 +262,4 @@ public function getConfigSyncPath(): string
279262
return $this->getProjectPath() . '/drupal/config/sync';
280263
}
281264

282-
private function replacePlaceholders(string $string): string
283-
{
284-
return str_replace([
285-
'{{app_path}}',
286-
'{{project_path}}',
287-
'{{site_name}}',
288-
'{{virtual_host}}',
289-
], [
290-
$this->getAppPath(),
291-
$this->getProjectPath(),
292-
$this->getSiteName(),
293-
$_SERVER['VIRTUAL_HOST'] ?? NULL,
294-
], $string);
295-
}
296265
}

0 commit comments

Comments
 (0)