Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 9a5eb17

Browse files
committed
Added process of booleans
1 parent 669fed8 commit 9a5eb17

File tree

5 files changed

+129
-8
lines changed

5 files changed

+129
-8
lines changed

‎src/DotEnv.php

Lines changed: 62 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,65 @@
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)
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+
* @return array The values that have been loaded into ENV
57+
*/
58+
public function load() :array
2359
{
2460
if (!is_readable($this->path)) {
2561
throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
2662
}
2763

64+
$loaded = [];
65+
2866
$lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
2967
foreach ($lines as $line) {
3068

@@ -34,13 +72,32 @@ public function load() :void
3472

3573
list($name, $value) = explode('=', $line, 2);
3674
$name = trim($name);
37-
$value = trim($value);
75+
$value = $this->processValue($value);
3876

3977
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
4078
putenv(sprintf('%s=%s', $name, $value));
4179
$_ENV[$name] = $value;
4280
$_SERVER[$name] = $value;
81+
$loaded[$name] = $value;
82+
}
83+
}
84+
85+
return $loaded;
86+
}
87+
88+
private function processValue($value) {
89+
$trimmedValue = trim($value);
90+
91+
if (!empty($this->options[static::PROCESS_BOOLEANS])) {
92+
$loweredValue = strtolower($trimmedValue);
93+
94+
$isBoolean = in_array($loweredValue, ['true', 'false'], true);
95+
96+
if ($isBoolean) {
97+
return $loweredValue === 'true';
4398
}
4499
}
100+
101+
return $trimmedValue;
45102
}
46-
}
103+
}

‎tests/DotenvTest.php

Lines changed: 56 additions & 3 deletions
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

Lines changed: 10 additions & 0 deletions
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
File renamed without changes.

‎tests/env/.env.return

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
VALUE=returned

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /