Skip to content

Commit 592499c

Browse files
committed
reading env from phpdotenv
1 parent f80d64f commit 592499c

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/DotEnv.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@
22

33
namespace Tkui;
44

5+
use Dotenv\Dotenv as DotenvDotenv;
56
use RuntimeException;
6-
use M1\Env\Parser as EnvParser;
77

88
/**
99
* .env file environment loader.
1010
*/
1111
final class DotEnv implements Environment
1212
{
13-
private array $data;
1413
private string $path;
1514
private string $filename;
1615

@@ -22,7 +21,6 @@ public function __construct(string $path, string $filename = '.env')
2221
{
2322
$this->path = $path;
2423
$this->filename = $filename;
25-
$this->data = [];
2624
}
2725

2826
/**
@@ -32,8 +30,6 @@ public function __construct(string $path, string $filename = '.env')
3230
*/
3331
public function load(): void
3432
{
35-
$this->data = [];
36-
3733
$file = $this->path . DIRECTORY_SEPARATOR . $this->filename;
3834
if (! file_exists($file)) {
3935
return;
@@ -43,10 +39,8 @@ public function load(): void
4339
throw new RuntimeException(sprintf('File "%s" is not readable.', $file));
4440
}
4541

46-
if (($buf = @file_get_contents($file)) === false) {
47-
throw new RuntimeException("Couldn't read file: " . $file);
48-
}
49-
$this->data = EnvParser::parse($buf);
42+
$dotenv = \Dotenv\Dotenv::createImmutable($this->path, $this->filename);
43+
$dotenv->safeLoad();
5044
}
5145

5246
/**
@@ -55,15 +49,24 @@ public function load(): void
5549
public function loadAndMergeWith(array $override): void
5650
{
5751
$this->load();
58-
$this->data = array_merge($this->data, $override);
52+
foreach ($override as $k => $v) {
53+
$_ENV[$k] = $v;
54+
}
5955
}
6056

6157
/**
6258
* @inheritdoc
6359
*/
6460
public function getValue(string $param, $default = null)
6561
{
66-
return $this->data[$param] ?? $default;
62+
$value = $_ENV[$param] ?? $default;
63+
switch (strtolower($value)) {
64+
case 'true':
65+
return true;
66+
case 'false':
67+
return false;
68+
}
69+
return $value;
6770
}
6871

6972
/**

0 commit comments

Comments
 (0)