From 6ae3e2e6494bb5e58c2decadafc3de7f1453f70a Mon Sep 17 00:00:00 2001 From: Jean-Christophe Lebreton Date: Sun, 1 Jul 2018 12:25:50 +0200 Subject: [PATCH] Added the list of environment variable declared inside the 'env' file (#199) --- src/Dotenv.php | 10 ++++++++++ src/Loader.php | 9 +++++++++ tests/Dotenv/DotenvTest.php | 8 ++++++++ tests/Dotenv/LoaderTest.php | 5 +++++ 4 files changed, 32 insertions(+) diff --git a/src/Dotenv.php b/src/Dotenv.php index d32c7384..22fc0f18 100644 --- a/src/Dotenv.php +++ b/src/Dotenv.php @@ -117,4 +117,14 @@ public function required($variable) { return new Validator((array) $variable, $this->loader); } + + /** + * Get the list of environment variables declared inside the 'env' file. + * + * @return array + */ + public function getEnvironmentVariableNames() + { + return $this->loader->variableNames; + } } diff --git a/src/Loader.php b/src/Loader.php index 9aa0e288..327302db 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -28,6 +28,13 @@ class Loader */ protected $immutable; + /** + * The list of environment variables declared inside the 'env' file. + * + * @var array + */ + public $variableNames = array(); + /** * Create a new loader instance. * @@ -357,6 +364,8 @@ public function setEnvironmentVariable($name, $value = null) { list($name, $value) = $this->normaliseEnvironmentVariable($name, $value); + $this->variableNames[] = $name; + // Don't overwrite existing environment variables if we're immutable // Ruby's dotenv does this with `ENV[key] ||= value`. if ($this->immutable && $this->getEnvironmentVariable($name) !== null) { diff --git a/tests/Dotenv/DotenvTest.php b/tests/Dotenv/DotenvTest.php index daed85db..2e972945 100644 --- a/tests/Dotenv/DotenvTest.php +++ b/tests/Dotenv/DotenvTest.php @@ -336,4 +336,12 @@ public function testDotenvRequiredCanBeUsedWithoutLoadingFile() $dotenv->required('REQUIRED_VAR')->notEmpty(); $this->assertTrue(true); } + + public function testGetEnvironmentVariablesList() + { + $dotenv = new Dotenv($this->fixturesFolder); + $dotenv->load(); + $this->assertTrue(is_array($dotenv->getEnvironmentVariableNames())); + $this->assertSame(array('FOO', 'BAR', 'SPACED', 'NULL'), $dotenv->getEnvironmentVariableNames()); + } } diff --git a/tests/Dotenv/LoaderTest.php b/tests/Dotenv/LoaderTest.php index ee8b93d0..3915b2b1 100644 --- a/tests/Dotenv/LoaderTest.php +++ b/tests/Dotenv/LoaderTest.php @@ -97,6 +97,9 @@ public function testMutableLoaderClearsEnvironmentVars() $this->assertSame(false, getenv($this->key())); $this->assertSame(false, isset($_ENV[$this->key()])); $this->assertSame(false, isset($_SERVER[$this->key()])); + $this->assertTrue(is_array($this->mutableLoader->variableNames)); + $this->assertFalse(empty($this->mutableLoader->variableNames)); + } public function testImmutableLoaderSetUnsetImmutable() @@ -121,5 +124,7 @@ public function testImmutableLoaderCannotClearEnvironmentVars() $this->assertSame($this->value(), getenv($this->key())); $this->assertSame(true, isset($_ENV[$this->key()])); $this->assertSame(true, isset($_SERVER[$this->key()])); + $this->assertTrue(is_array($this->immutableLoader->variableNames)); + $this->assertFalse(empty($this->immutableLoader->variableNames)); } }