Skip to content

Commit ecc79e2

Browse files
committed
✨ support for multi-environment setups
⬆️ upgraded dependencies Signed-off-by: Bruno Meilick <[email protected]>
1 parent 56c0e37 commit ecc79e2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+9127
-950
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
.idea
44
*.cache
55

6+
/tests/.env
67
/tests/kirby
78
/tests/logs
89
/tests/site/accounts

README.md

+26-63
Original file line numberDiff line numberDiff line change
@@ -24,94 +24,57 @@ This plugin is free but if you use it in a commercial project please consider to
2424

2525
## Setup
2626

27-
**.env file**
27+
### .env file Examples
28+
**/.env**
2829
```
2930
APP_MODE=production
30-
APP_DEBUG=true
31+
APP_DEBUG=false
3132
ALGOLIA_APIKEY=12d7331a21d8a28b3069c49830f463e833e30f6d
3233
KIRBY_API_USER=bnomei
3334
KIRBY_API_PW=52d3a0edcc78be6c5645fdb7568f94d3d83d1c2a
3435
```
3536

36-
**plugin helper methods**
37-
```php
38-
echo env('APP_MODE'); // production
39-
// or
40-
echo $page->getenv('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d
37+
**/.env.staging**
4138
```
42-
43-
**plain php**
44-
```php
45-
Bnomei\DotEnv::load();
46-
echo getenv('APP_DEBUG'); // true
47-
echo env('APP_DEBUG'); // true
39+
APP_MODE=staging
40+
APP_DEBUG=true
41+
ALGOLIA_APIKEY=950306d052ec893b467f2ca088daf2964b9f9530
42+
KIRBY_API_USER=notBnomei
43+
KIRBY_API_PW=37e30ad867ff3a427317dcd1852abbd692b39ffc
4844
```
4945

50-
### Using getenv() in the Kirby config file
46+
## Usage everywhere but in Config files
5147

52-
**site/config/config.php (callbacks only)**
53-
```php
54-
<?php
55-
return [
56-
// ... other options
57-
'bnomei.cloudconvert.apikey' => function() {
58-
return getenv('CLOUDCONVERT_APIKEY');
59-
},
60-
'bnomei.instagram.token' => function() {
61-
return getenv('INSTAGRAM_TOKEN');
62-
},
63-
'bnomei.thumbimageoptim.apikey' => function() {
64-
return getenv('IMAGEOPTIM_APIKEY');
65-
},
66-
];
67-
```
48+
> ATTENTION: The PHP functions `getenv` or `putenv` are not supported by this plugin since v2. Do use super globals `$_ENV`, `$_SERVER` or the plugins helper `env()`.
6849
69-
**site/config/config.php (manual require class)**
50+
**on server**
7051
```php
71-
<?php
72-
// load dotenv plugins class
73-
require_once __DIR__ . '/../plugins/kirby3-dotenv/classes/DotEnv.php';
74-
75-
return [
76-
// ... other options
77-
'bnomei.cloudconvert.apikey' =>
78-
\Bnomei\DotEnv::getenv('CLOUDCONVERT_APIKEY'),
79-
'bnomei.instagram.token' =>
80-
\Bnomei\DotEnv::getenv('INSTAGRAM_TOKEN'),
81-
'bnomei.thumbimageoptim.apikey' =>
82-
\Bnomei\DotEnv::getenv('IMAGEOPTIM_APIKEY',
83-
// provide options if defaults of plugin are not valid
84-
[
85-
'dir' => __DIR__ . '/../',
86-
'file' => '.env.dev',
87-
]
88-
),
89-
];
52+
echo $_ENV['APP_MODE']; // production
53+
echo env('APP_DEBUG'); // false
54+
// or
55+
echo $page->getenv('ALGOLIA_APIKEY'); // 12d7331a21d8a28b3069c49830f463e833e30f6d
9056
```
9157

92-
#### No callback - no luck? 3 line are enough!
93-
Unless you preload the `Bnomei\DotEnv` class using an `include_once` statement yourself the class will not be available in the kirby config files. But some options take a `callback` not just a `string` value. If your desired option does not then consider reporting a github issue at **their** repository. Adding a callback for an option is 3 lines of work.
94-
95-
**code/in/another/plugin.php**
58+
**on staging server**
9659
```php
97-
public function thisIsWereAllConfigValuesAreLoaded()
98-
{
99-
$fancyOption = option('another.plugin.fancy');
100-
// add these 3 lines
101-
if (is_callable($fancyOptions)) {
102-
$fancyOption = $fancyOption();
103-
}
104-
}
60+
echo $_ENV['APP_MODE']; // staging
61+
echo env('APP_DEBUG'); // true
62+
// or
63+
echo $page->getenv('ALGOLIA_APIKEY'); // 37e30ad867ff3a427317dcd1852abbd692b39ffc
10564
```
10665

66+
## Usage in Config files
67+
68+
See [config examples](https://github.com/bnomei/kirby3-dotenv/tree/master/tests/site/config) on how to use this plugin in combination with kirbys config files. Since v2 this plugin support Kirbys [Multi-environment setup](https://getkirby.com/docs/guide/configuration#multi-environment-setup) used to merging multiple config files.
10769

10870
## Settings
10971

11072
| bnomei.dotenv. | Default | Description |
11173
|---------------------------|----------------|---------------------------|
11274
| dir | `callback` | returning `kirby()->roots()->index(). When installing Kirby 3 with Composer use a `function() { return realpath(kirby()->roots()->index() . '/../'); }` |
113-
| filename | `.env` | |
75+
| file | `.env` | |
11476
| required | `[]` | You can define required variables in the Settings using an array. If any of these is missing a `RuntimeException` will be thrown. |
77+
| setup | `callback` | perform additional tasks on raw dotenv class instance |
11578

11679
## Dependencies
11780

classes/DotEnv.php

+32-26
Original file line numberDiff line numberDiff line change
@@ -17,43 +17,48 @@ final class DotEnv
1717
*/
1818
private $dotenv;
1919

20-
public function __construct(array $options = [], bool $fromConfig = true)
20+
public function __construct(array $options = [], bool $useKirbyOptions = true)
2121
{
2222
$defaults = [
23-
'dir' => $fromConfig ?
24-
option('bnomei.dotenv.dir', kirby()->roots()->index()) :
23+
'dir' => $useKirbyOptions ?
24+
option('bnomei.dotenv.dir') :
2525
realpath(__DIR__ . '/../../../../') // try plugin > site > index
2626
,
27-
'file' => $fromConfig ?
28-
option('bnomei.dotenv.file', '.env') :
27+
'file' => $useKirbyOptions ?
28+
option('bnomei.dotenv.file') :
2929
'.env'
3030
,
31-
'required' => $fromConfig ?
32-
option('bnomei.dotenv.required', []) :
31+
'required' => $useKirbyOptions ?
32+
option('bnomei.dotenv.required') :
3333
[]
3434
,
35+
'setup' => $useKirbyOptions ?
36+
option('bnomei.dotenv.setup') :
37+
function ($dotenv) {
38+
return $dotenv;
39+
},
3540
];
3641
$options = array_merge($defaults, $options);
3742

43+
foreach (['dir', 'file'] as $key) {
44+
$value = A::get($options, $key);
45+
if ($value && is_callable($value) && ! is_string($value)) {
46+
$options[$key] = $value();
47+
}
48+
}
49+
50+
$this->dotenv = null;
3851
$this->loadFromDir(A::get($options, 'dir'), A::get($options, 'file'));
3952
$this->addRequired(A::get($options, 'required'));
53+
$this->dotenv = A::get($options, 'setup')($this->dotenv);
4054
}
4155

42-
private function loadFromDir($dir, $file = '.env'): bool
56+
private function loadFromDir(string $dir, string $file): bool
4357
{
44-
if (! $dir) {
45-
return false;
46-
}
47-
if (is_callable($dir)) {
48-
$dir = $dir();
49-
}
50-
if (! $file) {
58+
if (! $dir || ! $file) {
5159
return false;
5260
}
53-
if (is_callable($file)) {
54-
$file = $file();
55-
}
56-
$this->dotenv = new \Dotenv\Dotenv($dir, $file);
61+
$this->dotenv = \Dotenv\Dotenv::createMutable($dir, $file);
5762

5863
try {
5964
$this->dotenv->load();
@@ -78,17 +83,18 @@ public function addRequired(array $required = []): void
7883
}
7984

8085
private static $singleton;
81-
public static function load(array $options = [], bool $fromConfig = true): bool
86+
public static function load(array $options = []): bool
8287
{
83-
if (! self::$singleton) {
84-
self::$singleton = new self($options, $fromConfig);
85-
}
88+
// always load anew
89+
self::$singleton = new self($options, count($options) === 0);
8690
return self::$singleton->isLoaded();
8791
}
8892

89-
public static function getenv(string $env, array $options = [])
93+
public static function getenv(string $env)
9094
{
91-
self::load($options, count($options) === 0);
92-
return getenv($env);
95+
if (! self::$singleton) {
96+
self::load();
97+
}
98+
return A::get($_ENV, $env, null);
9399
}
94100
}

composer.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "bnomei/kirby3-dotenv",
33
"type": "kirby-plugin",
4-
"version": "1.3.0",
4+
"version": "2.0.0",
55
"description": "Kirby 3 Plugin for environment variables from .env",
66
"license": "MIT",
77
"authors": [
@@ -28,8 +28,8 @@
2828
},
2929
"require": {
3030
"php": ">=7.3.0",
31-
"vlucas/phpdotenv": "^2.6",
32-
"getkirby/composer-installer": "^1.1"
31+
"getkirby/composer-installer": "^1.1",
32+
"vlucas/phpdotenv": "^5.3"
3333
},
3434
"scripts": {
3535
"analyze": "phpstan analyse classes",

0 commit comments

Comments
 (0)