Skip to content

Commit 56c0e37

Browse files
committed
✨ can be used in config without callback after loading class
Signed-off-by: Bruno Meilick <[email protected]>
1 parent 3a1c02b commit 56c0e37

File tree

10 files changed

+128
-48
lines changed

10 files changed

+128
-48
lines changed

README.md

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,46 @@ echo env('APP_DEBUG'); // true
4949

5050
### Using getenv() in the Kirby config file
5151

52-
**site/config/config.php**
52+
**site/config/config.php (callbacks only)**
5353
```php
54+
<?php
5455
return [
5556
// ... other options
5657
'bnomei.cloudconvert.apikey' => function() {
57-
return env('CLOUDCONVERT_APIKEY');
58+
return getenv('CLOUDCONVERT_APIKEY');
5859
},
5960
'bnomei.instagram.token' => function() {
60-
return env('INSTAGRAM_TOKEN');
61+
return getenv('INSTAGRAM_TOKEN');
6162
},
6263
'bnomei.thumbimageoptim.apikey' => function() {
63-
return env('IMAGEOPTIM_APIKEY');
64+
return getenv('IMAGEOPTIM_APIKEY');
6465
},
6566
];
6667
```
6768

69+
**site/config/config.php (manual require class)**
70+
```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+
];
90+
```
91+
6892
#### No callback - no luck? 3 line are enough!
6993
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.
7094

classes/DotEnv.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Dotenv\Exception\InvalidPathException;
88
use Kirby\Toolkit\A;
9+
use Kirby\Toolkit\F;
910
use function getenv;
1011
use function option;
1112

@@ -16,27 +17,43 @@ final class DotEnv
1617
*/
1718
private $dotenv;
1819

19-
public function __construct(array $options = [])
20+
public function __construct(array $options = [], bool $fromConfig = true)
2021
{
2122
$defaults = [
22-
'dir' => option('bnomei.dotenv.dir', kirby()->roots()->index()),
23-
'required' => option('bnomei.dotenv.required'),
23+
'dir' => $fromConfig ?
24+
option('bnomei.dotenv.dir', kirby()->roots()->index()) :
25+
realpath(__DIR__ . '/../../../../') // try plugin > site > index
26+
,
27+
'file' => $fromConfig ?
28+
option('bnomei.dotenv.file', '.env') :
29+
'.env'
30+
,
31+
'required' => $fromConfig ?
32+
option('bnomei.dotenv.required', []) :
33+
[]
34+
,
2435
];
2536
$options = array_merge($defaults, $options);
2637

27-
$this->loadFromDir(A::get($options, 'dir'));
38+
$this->loadFromDir(A::get($options, 'dir'), A::get($options, 'file'));
2839
$this->addRequired(A::get($options, 'required'));
2940
}
3041

31-
private function loadFromDir($dir): bool
42+
private function loadFromDir($dir, $file = '.env'): bool
3243
{
3344
if (! $dir) {
3445
return false;
3546
}
3647
if (is_callable($dir)) {
3748
$dir = $dir();
3849
}
39-
$this->dotenv = new \Dotenv\Dotenv($dir);
50+
if (! $file) {
51+
return false;
52+
}
53+
if (is_callable($file)) {
54+
$file = $file();
55+
}
56+
$this->dotenv = new \Dotenv\Dotenv($dir, $file);
4057

4158
try {
4259
$this->dotenv->load();
@@ -61,17 +78,17 @@ public function addRequired(array $required = []): void
6178
}
6279

6380
private static $singleton;
64-
public static function load(array $options = []): bool
81+
public static function load(array $options = [], bool $fromConfig = true): bool
6582
{
6683
if (! self::$singleton) {
67-
self::$singleton = new self($options);
84+
self::$singleton = new self($options, $fromConfig);
6885
}
6986
return self::$singleton->isLoaded();
7087
}
7188

72-
public static function getenv(string $env)
89+
public static function getenv(string $env, array $options = [])
7390
{
74-
self::load();
91+
self::load($options, count($options) === 0);
7592
return getenv($env);
7693
}
7794
}

composer.json

Lines changed: 7 additions & 1 deletion
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.2.3",
4+
"version": "1.3.0",
55
"description": "Kirby 3 Plugin for environment variables from .env",
66
"license": "MIT",
77
"authors": [
@@ -42,6 +42,12 @@
4242
"dist": [
4343
"composer install --no-dev --optimize-autoloader",
4444
"git rm -rf --cached .; git add .;"
45+
],
46+
"kirby": [
47+
"composer install",
48+
"composer update",
49+
"composer install --working-dir=tests/kirby --no-dev --optimize-autoloader",
50+
"composer update --working-dir=tests/kirby"
4551
]
4652
},
4753
"require-dev": {

composer.lock

Lines changed: 7 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
stopOnFailure="false">
1111
<testsuites>
1212
<testsuite name="PLUGIN">
13-
<directory suffix="Test.php">./tests</directory>
13+
<file>./tests/DotEnvTest.php</file>
1414
</testsuite>
1515
</testsuites>
1616
<php>

tests/DotEnvTest.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ class DotEnvTest extends TestCase
99
{
1010
public function setUp(): void
1111
{
12-
copy(
13-
__DIR__ . '/.env.example',
14-
__DIR__ . '/.env'
15-
);
12+
if (!file_exists(__DIR__ . '/.env')) {
13+
copy(
14+
__DIR__ . '/.env.example',
15+
__DIR__ . '/.env'
16+
);
17+
}
1618
}
1719

18-
public function tearDown(): void
20+
public function removeDotEnvFile(): void
1921
{
2022
$dotenv = __DIR__ . '/.env';
2123
if (file_exists($dotenv)) {
@@ -35,18 +37,6 @@ public function testLoad()
3537
$this->assertTrue($dotenv->isLoaded());
3638
}
3739

38-
public function testLoadFailsIfMissingFile()
39-
{
40-
self::tearDown(); // early
41-
$dotenv = new Bnomei\DotEnv();
42-
$this->assertFalse($dotenv->isLoaded());
43-
44-
$dotenv = new Bnomei\DotEnv([
45-
'dir' => null
46-
]);
47-
$this->assertFalse($dotenv->isLoaded());
48-
}
49-
5040
public function testRequired()
5141
{
5242
$dotenv = new Bnomei\DotEnv([
@@ -70,4 +60,31 @@ public function testStaticLoad()
7060
{
7161
$this->assertTrue(Bnomei\DotEnv::load());
7262
}
63+
64+
public function testLoadedToPage()
65+
{
66+
$response = kirby()->render("/");
67+
$this->assertTrue($response->code() === 200);
68+
$this->assertMatchesRegularExpression('/(production)/', $response->body());
69+
}
70+
71+
public function testLoadedFromConfig()
72+
{
73+
$this->assertEquals('bnomei', kirby()->option('no_callback'));
74+
}
75+
76+
public function testLoadFailsIfMissingFile()
77+
{
78+
$this->removeDotEnvFile();
79+
80+
$dotenv = new Bnomei\DotEnv();
81+
$this->assertFalse($dotenv->isLoaded());
82+
83+
$dotenv = new Bnomei\DotEnv([
84+
'dir' => null,
85+
]);
86+
$this->assertFalse($dotenv->isLoaded());
87+
88+
$this->setUp();
89+
}
7390
}

tests/site/config/config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
// load dotenv plugins class
4+
require_once __DIR__ . '/../../../classes/DotEnv.php';
5+
6+
return [
7+
'no_callback' => \Bnomei\DotEnv::getenv(
8+
'KIRBY_API_USER',
9+
[
10+
'dir' => realpath(__DIR__ . '/../../'),
11+
'file' => '.env',
12+
],
13+
),
14+
];

tests/site/templates/default.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
<?php
2+
3+
echo $page->getenv('APP_MODE');

vendor/composer/InstalledVersions.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ class InstalledVersions
2727
private static $installed = array (
2828
'root' =>
2929
array (
30-
'pretty_version' => '1.2.3',
31-
'version' => '1.2.3.0',
30+
'pretty_version' => '1.3.0',
31+
'version' => '1.3.0.0',
3232
'aliases' =>
3333
array (
3434
),
@@ -39,8 +39,8 @@ class InstalledVersions
3939
array (
4040
'bnomei/kirby3-dotenv' =>
4141
array (
42-
'pretty_version' => '1.2.3',
43-
'version' => '1.2.3.0',
42+
'pretty_version' => '1.3.0',
43+
'version' => '1.3.0.0',
4444
'aliases' =>
4545
array (
4646
),

vendor/composer/installed.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php return array (
22
'root' =>
33
array (
4-
'pretty_version' => '1.2.3',
5-
'version' => '1.2.3.0',
4+
'pretty_version' => '1.3.0',
5+
'version' => '1.3.0.0',
66
'aliases' =>
77
array (
88
),
@@ -13,8 +13,8 @@
1313
array (
1414
'bnomei/kirby3-dotenv' =>
1515
array (
16-
'pretty_version' => '1.2.3',
17-
'version' => '1.2.3.0',
16+
'pretty_version' => '1.3.0',
17+
'version' => '1.3.0.0',
1818
'aliases' =>
1919
array (
2020
),

0 commit comments

Comments
 (0)