Skip to content

Commit 7d1e192

Browse files
author
F. Michel
authored
Merge pull request #1 from jaumarar/process-booleans
Added process of booleans
2 parents 669fed8 + fa6f792 commit 7d1e192

File tree

6 files changed

+122
-8
lines changed

6 files changed

+122
-8
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use DevCoder\DotEnv;
1818

1919
echo getenv('APP_ENV');
2020
// dev
21-
echo getenv('DATABASE_DNS')
21+
echo getenv('DATABASE_DNS');
2222
// mysql:host=localhost;dbname=test;
2323
```
2424
Ideal for small project

src/DotEnv.php

+54-4
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,56 @@
44

55
class DotEnv
66
{
7+
/**
8+
* Convert true and false to booleans, instead of:
9+
*
10+
* VARIABLE=false -> ['VARIABLE' => 'false']
11+
*
12+
* it will be
13+
*
14+
* VARIABLE=false -> ['VARIABLE' => false]
15+
*
16+
* default = true
17+
*/
18+
const PROCESS_BOOLEANS = 'PROCESS_BOOLEANS';
19+
720
/**
821
* The directory where the .env file can be located.
922
*
1023
* @var string
1124
*/
1225
protected $path;
1326

14-
public function __construct(string $path)
27+
/**
28+
* Configure the options on which the parsed will act
29+
*
30+
* @var array
31+
*/
32+
protected $options = [];
33+
34+
public function __construct(string $path, array $options = [])
1535
{
16-
if(!file_exists($path)) {
36+
if (!file_exists($path)) {
1737
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
1838
}
39+
1940
$this->path = $path;
41+
42+
$this->processOptions($options);
2043
}
2144

22-
public function load() :void
45+
private function processOptions(array $options) : void
46+
{
47+
$this->options = array_merge([
48+
static::PROCESS_BOOLEANS => true
49+
], $options);
50+
}
51+
52+
/**
53+
* Processes the $path of the instances and parses the values into $_SERVER and $_ENV, also returns all the data that has been read.
54+
* Skips empty and commented lines.
55+
*/
56+
public function load() : void
2357
{
2458
if (!is_readable($this->path)) {
2559
throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
@@ -34,7 +68,7 @@ public function load() :void
3468

3569
list($name, $value) = explode('=', $line, 2);
3670
$name = trim($name);
37-
$value = trim($value);
71+
$value = $this->processValue($value);
3872

3973
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
4074
putenv(sprintf('%s=%s', $name, $value));
@@ -43,4 +77,20 @@ public function load() :void
4377
}
4478
}
4579
}
80+
81+
private function processValue(string $value) {
82+
$trimmedValue = trim($value);
83+
84+
if (!empty($this->options[static::PROCESS_BOOLEANS])) {
85+
$loweredValue = strtolower($trimmedValue);
86+
87+
$isBoolean = in_array($loweredValue, ['true', 'false'], true);
88+
89+
if ($isBoolean) {
90+
return $loweredValue === 'true';
91+
}
92+
}
93+
94+
return $trimmedValue;
95+
}
4696
}

tests/DotenvTest.php

+56-3
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace Test\DevCoder;
44

5-
65
use DevCoder\DotEnv;
76
use PHPUnit\Framework\TestCase;
87

98
class DotenvTest extends TestCase
109
{
10+
private function env(string $file)
11+
{
12+
return __DIR__ . DIRECTORY_SEPARATOR . 'env' . DIRECTORY_SEPARATOR . $file;
13+
}
1114

15+
/**
16+
* @runInSeparateProcess
17+
*/
1218
public function testLoad() {
13-
(new DotEnv(__DIR__ . '/.env'))->load();
19+
(new DotEnv($this->env('.env.default')))->load();
1420
$this->assertEquals('dev', getenv('APP_ENV'));
1521
$this->assertEquals('mysql:host=localhost;dbname=test;', getenv('DATABASE_DNS'));
1622
$this->assertEquals('root', getenv('DATABASE_USER'));
@@ -35,6 +41,53 @@ public function testLoad() {
3541

3642
public function testFileNotExist() {
3743
$this->expectException(\InvalidArgumentException::class);
38-
(new DotEnv(__DIR__ . '/.env.local'))->load();
44+
(new DotEnv($this->env('.env.not-exists')))->load();
45+
}
46+
47+
/**
48+
* @runInSeparateProcess
49+
*/
50+
public function testLoadReturnsValues()
51+
{
52+
$loaded = (new DotEnv($this->env('.env.return')))->load();
53+
54+
$this->assertEquals('returned', $loaded['VALUE']);
55+
$this->assertEquals('returned', $_ENV['VALUE']);
56+
}
57+
58+
/**
59+
* @runInSeparateProcess
60+
*/
61+
public function testProcessBoolean()
62+
{
63+
(new DotEnv($this->env('.env.boolean'), [
64+
DotEnv::PROCESS_BOOLEANS => true
65+
]))->load();
66+
67+
$this->assertEquals(false, $_ENV['FALSE1']);
68+
$this->assertEquals(false, $_ENV['FALSE2']);
69+
$this->assertEquals(false, $_ENV['FALSE3']);
70+
$this->assertEquals("'false'", $_ENV['FALSE4']);
71+
$this->assertEquals('0', $_ENV['FALSE5']);
72+
73+
$this->assertEquals(true, $_ENV['TRUE1']);
74+
$this->assertEquals(true, $_ENV['TRUE2']);
75+
$this->assertEquals(true, $_ENV['TRUE3']);
76+
$this->assertEquals("'true'", $_ENV['TRUE4']);
77+
$this->assertEquals('1', $_ENV['TRUE5']);
78+
}
79+
80+
/**
81+
* @runInSeparateProcess
82+
*/
83+
public function testDontProcessBoolean()
84+
{
85+
(new DotEnv($this->env('.env.boolean'), [
86+
DotEnv::PROCESS_BOOLEANS => false
87+
]))->load();
88+
89+
$this->assertEquals('false', $_ENV['FALSE1']);
90+
91+
$this->assertEquals('true', $_ENV['TRUE1']);
3992
}
4093
}

tests/env/.env.boolean

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
FALSE1=false
2+
FALSE2= false
3+
FALSE3=FALSE
4+
FALSE4='false'
5+
FALSE5=0
6+
TRUE1=true
7+
TRUE2= true
8+
TRUE3=TRUE
9+
TRUE4='true'
10+
TRUE5=1

tests/.env tests/env/.env.default

File renamed without changes.

tests/env/.env.return

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VALUE=returned

0 commit comments

Comments
 (0)