Skip to content

Commit 7261814

Browse files
Created Dotenv::getSites method. Related documentation updates. Updating change log to prepare for version 0.1.7.
1 parent c2a0f39 commit 7261814

File tree

3 files changed

+69
-9
lines changed

3 files changed

+69
-9
lines changed

CHANGELOG.md

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

55
## [Unreleased][unreleased]
66

7+
## [0.1.7] - 2021-12-01
8+
9+
### Changed
10+
11+
- Added more documentation concerning Drupal multi-site configuration.
12+
- Added documentation for `DOMAINS` environment variable.
13+
- Added documentation for `SITES` environment variable.
14+
- Added Dotenv::getSites method to return Drupal-compatible list of sites.
15+
16+
### Fixed
17+
18+
- Inaccurate multi-site configuration examples in documentation.
19+
720
## [0.1.6] - 2021-12-01
821

922
### Fixed
@@ -45,10 +58,11 @@ Updates should follow the [Keep a CHANGELOG](https://keepachangelog.com/) princi
4558

4659
**Initial release!**
4760

48-
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.6...main
49-
[0.1.3]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.5...0.1.6
50-
[0.1.3]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.4...0.1.5
51-
[0.1.3]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.3...0.1.4
61+
[unreleased]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.7...main
62+
[0.1.7]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.6...0.1.7
63+
[0.1.6]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.5...0.1.6
64+
[0.1.5]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.4...0.1.5
65+
[0.1.4]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.3...0.1.4
5266
[0.1.3]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.2...0.1.3
5367
[0.1.2]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.1...0.1.2
5468
[0.1.1]: https://github.com/unleashedtech/dotenv-drupal/compare/0.1.0...0.1.1

README.md

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ component's documentation about how `.env` files are used.
1010
### Configuring Drupal
1111
First, you'll need to configure Drupal to use this package.
1212

13-
#### Drupal Settings Files
13+
#### Drupal Settings & Sites Files
1414
Drupal is typically configured via `settings.php` files in various directories.
1515
To use this package with Drupal, some code will need to be added to the top of
1616
the relevant `settings.php` file(s):
@@ -36,8 +36,8 @@ empty to allow automatic database name selection based on the site name.
3636
<?php
3737
use UnleashedTech\Drupal\Dotenv\Dotenv;
3838
$dotenv = new Dotenv();
39-
$dotenv->setSiteName(__DIR__);
40-
require __DIR__ . '../default/settings.php';
39+
$dotenv->setSiteName(basename(__DIR__));
40+
require __DIR__ . '/../default/settings.php';
4141
```
4242

4343
If you'd like, you can manually set the database name for each site via the
@@ -47,15 +47,28 @@ If you'd like, you can manually set the database name for each site via the
4747
<?php
4848
use UnleashedTech\Drupal\Dotenv\Dotenv;
4949
$dotenv = new Dotenv();
50-
$dotenv->setSiteName(__DIR__);
50+
$dotenv->setSiteName(basename(__DIR__));
5151
$dotenv->setDatabaseName('foo');
52-
require __DIR__ . '../default/settings.php';
52+
require __DIR__ . '/../default/settings.php';
5353
```
5454

5555
If conditional logic is required for a given site, such logic is still supported.
5656
This package will auto-load various `settings.{{environment}}.php` or
5757
`config.{{environment}}.php` files, if they exist.
5858

59+
###### Sites Files
60+
This package also provides functionality to configure Drupal's `$sites` variable
61+
via `sites.php`. The `$sites` variable is built from data in the [DOMAINS](#domains)
62+
& [SITES](#sites) environment variables. You will need to add the following code
63+
to `sites.php`:
64+
65+
```php
66+
<?php
67+
use UnleashedTech\Drupal\Dotenv\Dotenv;
68+
$dotenv = new Dotenv();
69+
$sites = $dotenv->getSites();
70+
```
71+
5972
#### Configuring Drupal via ENV Variables
6073
This package will provide many default setting & configuration values based on the
6174
detected environment. Some of these values can be populated by environment variables.
@@ -65,6 +78,8 @@ For production environments, environment variables should ideally be defined via
6578
configuration.
6679

6780
* [DATABASE_URL](#database_url)
81+
* [DOMAINS](#domains)
82+
* [SITES](#sites)
6883
* [SOLR_URL](#solr_url)
6984
* More configuration options coming soon!
7085

@@ -87,6 +102,20 @@ For multi-site installations, do _not_ specify a database name in the ENV file(s
87102
DATABASE_URL=mysql://foo:bar@host:3306
88103
```
89104

105+
##### DOMAINS
106+
A CSV list of domains used by the given environment:
107+
108+
```dotenv
109+
DOMAINS=foo.example,bar.example,baz.example
110+
```
111+
112+
##### SITES
113+
A CSV list of Drupal "sites" (e.g. "subdomains") used by the given environment:
114+
115+
```dotenv
116+
SITES=foo,bar,baz,qux
117+
```
118+
90119
##### SOLR_URL
91120
The default Solr connection can be configured via a [DSN](https://en.wikipedia.org/wiki/Data_source_name):
92121

src/Dotenv.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,23 @@ public function getSettings(): array
219219
return $settings;
220220
}
221221

222+
/**
223+
* Gets the Drupal-multi-site $sites array, based on environment variables.
224+
*
225+
* @return array
226+
* The Drupal-multi-site $sites array, based on environment variables.
227+
*/
228+
public function getSites(): array {
229+
$domains = explode(',', $_SERVER['DOMAINS'] ?? 'default.example');
230+
$sites = explode(',', $_SERVER['SITES'] ?? 'default');
231+
foreach ($sites as $site) {
232+
foreach ($domains as $domain) {
233+
$sites[$site . '.' . $domain] = $site;
234+
}
235+
}
236+
return $sites;
237+
}
238+
222239
/**
223240
* Gets the absolute path for the given path.
224241
*

0 commit comments

Comments
 (0)