From 9a5eb1711642732098b6ebd065d5f19a754c67f1 Mon Sep 17 00:00:00 2001 From: Jaume Date: Sun, 7 Feb 2021 11:24:20 +0100 Subject: [PATCH 1/3] Added process of booleans --- src/DotEnv.php | 67 +++++++++++++++++++++++++++++--- tests/DotenvTest.php | 59 ++++++++++++++++++++++++++-- tests/env/.env.boolean | 10 +++++ tests/{.env => env/.env.default} | 0 tests/env/.env.return | 1 + 5 files changed, 129 insertions(+), 8 deletions(-) create mode 100644 tests/env/.env.boolean rename tests/{.env => env/.env.default} (100%) create mode 100644 tests/env/.env.return diff --git a/src/DotEnv.php b/src/DotEnv.php index 39f9a48..e7bc3bc 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -4,6 +4,19 @@ class DotEnv { + /** + * Convert true and false to booleans, instead of: + * + * VARIABLE=false -> ['VARIABLE' => 'false'] + * + * it will be + * + * VARIABLE=false -> ['VARIABLE' => false] + * + * default = true + */ + const PROCESS_BOOLEANS = 'PROCESS_BOOLEANS'; + /** * The directory where the .env file can be located. * @@ -11,20 +24,45 @@ class DotEnv */ protected $path; - public function __construct(string $path) + /** + * Configure the options on which the parsed will act + * + * @var array + */ + protected $options = []; + + public function __construct(string $path, array $options = []) { - if(!file_exists($path)) { + if (!file_exists($path)) { throw new \InvalidArgumentException(sprintf('%s does not exist', $path)); } + $this->path = $path; + + $this->processOptions($options); } - public function load() :void + private function processOptions(array $options) + { + $this->options = array_merge([ + static::PROCESS_BOOLEANS => true + ], $options); + } + + /** + * Processes the $path of the instances and parses the values into $_SERVER and $_ENV, also returns all the data that has been read. + * Skips empty and commented lines. + * + * @return array The values that have been loaded into ENV + */ + public function load() :array { if (!is_readable($this->path)) { throw new \RuntimeException(sprintf('%s file is not readable', $this->path)); } + $loaded = []; + $lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { @@ -34,13 +72,32 @@ public function load() :void list($name, $value) = explode('=', $line, 2); $name = trim($name); - $value = trim($value); + $value = $this->processValue($value); if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) { putenv(sprintf('%s=%s', $name, $value)); $_ENV[$name] = $value; $_SERVER[$name] = $value; + $loaded[$name] = $value; + } + } + + return $loaded; + } + + private function processValue($value) { + $trimmedValue = trim($value); + + if (!empty($this->options[static::PROCESS_BOOLEANS])) { + $loweredValue = strtolower($trimmedValue); + + $isBoolean = in_array($loweredValue, ['true', 'false'], true); + + if ($isBoolean) { + return $loweredValue === 'true'; } } + + return $trimmedValue; } -} +} \ No newline at end of file diff --git a/tests/DotenvTest.php b/tests/DotenvTest.php index 62e3698..b8f7bc8 100644 --- a/tests/DotenvTest.php +++ b/tests/DotenvTest.php @@ -2,15 +2,21 @@ namespace Test\DevCoder; - use DevCoder\DotEnv; use PHPUnit\Framework\TestCase; class DotenvTest extends TestCase { + private function env(string $file) + { + return __DIR__ . DIRECTORY_SEPARATOR . 'env' . DIRECTORY_SEPARATOR . $file; + } + /** + * @runInSeparateProcess + */ public function testLoad() { - (new DotEnv(__DIR__ . '/.env'))->load(); + (new DotEnv($this->env('.env.default')))->load(); $this->assertEquals('dev', getenv('APP_ENV')); $this->assertEquals('mysql:host=localhost;dbname=test;', getenv('DATABASE_DNS')); $this->assertEquals('root', getenv('DATABASE_USER')); @@ -35,6 +41,53 @@ public function testLoad() { public function testFileNotExist() { $this->expectException(\InvalidArgumentException::class); - (new DotEnv(__DIR__ . '/.env.local'))->load(); + (new DotEnv($this->env('.env.not-exists')))->load(); + } + + /** + * @runInSeparateProcess + */ + public function testLoadReturnsValues() + { + $loaded = (new DotEnv($this->env('.env.return')))->load(); + + $this->assertEquals('returned', $loaded['VALUE']); + $this->assertEquals('returned', $_ENV['VALUE']); + } + + /** + * @runInSeparateProcess + */ + public function testProcessBoolean() + { + (new DotEnv($this->env('.env.boolean'), [ + DotEnv::PROCESS_BOOLEANS => true + ]))->load(); + + $this->assertEquals(false, $_ENV['FALSE1']); + $this->assertEquals(false, $_ENV['FALSE2']); + $this->assertEquals(false, $_ENV['FALSE3']); + $this->assertEquals("'false'", $_ENV['FALSE4']); + $this->assertEquals('0', $_ENV['FALSE5']); + + $this->assertEquals(true, $_ENV['TRUE1']); + $this->assertEquals(true, $_ENV['TRUE2']); + $this->assertEquals(true, $_ENV['TRUE3']); + $this->assertEquals("'true'", $_ENV['TRUE4']); + $this->assertEquals('1', $_ENV['TRUE5']); + } + + /** + * @runInSeparateProcess + */ + public function testDontProcessBoolean() + { + (new DotEnv($this->env('.env.boolean'), [ + DotEnv::PROCESS_BOOLEANS => false + ]))->load(); + + $this->assertEquals('false', $_ENV['FALSE1']); + + $this->assertEquals('true', $_ENV['TRUE1']); } } diff --git a/tests/env/.env.boolean b/tests/env/.env.boolean new file mode 100644 index 0000000..e30ad05 --- /dev/null +++ b/tests/env/.env.boolean @@ -0,0 +1,10 @@ +FALSE1=false +FALSE2= false +FALSE3=FALSE +FALSE4='false' +FALSE5=0 +TRUE1=true +TRUE2= true +TRUE3=TRUE +TRUE4='true' +TRUE5=1 \ No newline at end of file diff --git a/tests/.env b/tests/env/.env.default similarity index 100% rename from tests/.env rename to tests/env/.env.default diff --git a/tests/env/.env.return b/tests/env/.env.return new file mode 100644 index 0000000..8ba2093 --- /dev/null +++ b/tests/env/.env.return @@ -0,0 +1 @@ +VALUE=returned \ No newline at end of file From df89dde4aeda2ae3b4e79b42ec864d81428823db Mon Sep 17 00:00:00 2001 From: Jaume Date: Sat, 13 Feb 2021 10:09:41 +0100 Subject: [PATCH 2/3] Branch refactor --- README.md | 2 +- src/DotEnv.php | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index f295d41..41f2a16 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ use DevCoder\DotEnv; echo getenv('APP_ENV'); // dev -echo getenv('DATABASE_DNS') +echo getenv('DATABASE_DNS'); // mysql:host=localhost;dbname=test; ``` Ideal for small project diff --git a/src/DotEnv.php b/src/DotEnv.php index e7bc3bc..7c630af 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -42,7 +42,7 @@ public function __construct(string $path, array $options = []) $this->processOptions($options); } - private function processOptions(array $options) + private function processOptions(array $options) : void { $this->options = array_merge([ static::PROCESS_BOOLEANS => true @@ -52,10 +52,8 @@ private function processOptions(array $options) /** * Processes the $path of the instances and parses the values into $_SERVER and $_ENV, also returns all the data that has been read. * Skips empty and commented lines. - * - * @return array The values that have been loaded into ENV */ - public function load() :array + public function load() : void { if (!is_readable($this->path)) { throw new \RuntimeException(sprintf('%s file is not readable', $this->path)); @@ -81,11 +79,9 @@ public function load() :array $loaded[$name] = $value; } } - - return $loaded; } - private function processValue($value) { + private function processValue(string $value) { $trimmedValue = trim($value); if (!empty($this->options[static::PROCESS_BOOLEANS])) { From fa6f7923de6868d3672b9b6579fbceb88e09ae9f Mon Sep 17 00:00:00 2001 From: Jaume Date: Mon, 15 Feb 2021 08:34:01 +0100 Subject: [PATCH 3/3] Removed unused var --- src/DotEnv.php | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/DotEnv.php b/src/DotEnv.php index 7c630af..8fae94c 100644 --- a/src/DotEnv.php +++ b/src/DotEnv.php @@ -59,8 +59,6 @@ public function load() : void throw new \RuntimeException(sprintf('%s file is not readable', $this->path)); } - $loaded = []; - $lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); foreach ($lines as $line) { @@ -76,7 +74,6 @@ public function load() : void putenv(sprintf('%s=%s', $name, $value)); $_ENV[$name] = $value; $_SERVER[$name] = $value; - $loaded[$name] = $value; } } } @@ -96,4 +93,4 @@ private function processValue(string $value) { return $trimmedValue; } -} \ No newline at end of file +}