Skip to content

Commit 6ae3e2e

Browse files
jclebretonGrahamCampbell
authored andcommitted
Added the list of environment variable declared inside the 'env' file (#199)
1 parent 2ecccbc commit 6ae3e2e

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

src/Dotenv.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,14 @@ public function required($variable)
117117
{
118118
return new Validator((array) $variable, $this->loader);
119119
}
120+
121+
/**
122+
* Get the list of environment variables declared inside the 'env' file.
123+
*
124+
* @return array
125+
*/
126+
public function getEnvironmentVariableNames()
127+
{
128+
return $this->loader->variableNames;
129+
}
120130
}

src/Loader.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,13 @@ class Loader
2828
*/
2929
protected $immutable;
3030

31+
/**
32+
* The list of environment variables declared inside the 'env' file.
33+
*
34+
* @var array
35+
*/
36+
public $variableNames = array();
37+
3138
/**
3239
* Create a new loader instance.
3340
*
@@ -357,6 +364,8 @@ public function setEnvironmentVariable($name, $value = null)
357364
{
358365
list($name, $value) = $this->normaliseEnvironmentVariable($name, $value);
359366

367+
$this->variableNames[] = $name;
368+
360369
// Don't overwrite existing environment variables if we're immutable
361370
// Ruby's dotenv does this with `ENV[key] ||= value`.
362371
if ($this->immutable && $this->getEnvironmentVariable($name) !== null) {

tests/Dotenv/DotenvTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,4 +336,12 @@ public function testDotenvRequiredCanBeUsedWithoutLoadingFile()
336336
$dotenv->required('REQUIRED_VAR')->notEmpty();
337337
$this->assertTrue(true);
338338
}
339+
340+
public function testGetEnvironmentVariablesList()
341+
{
342+
$dotenv = new Dotenv($this->fixturesFolder);
343+
$dotenv->load();
344+
$this->assertTrue(is_array($dotenv->getEnvironmentVariableNames()));
345+
$this->assertSame(array('FOO', 'BAR', 'SPACED', 'NULL'), $dotenv->getEnvironmentVariableNames());
346+
}
339347
}

tests/Dotenv/LoaderTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ public function testMutableLoaderClearsEnvironmentVars()
9797
$this->assertSame(false, getenv($this->key()));
9898
$this->assertSame(false, isset($_ENV[$this->key()]));
9999
$this->assertSame(false, isset($_SERVER[$this->key()]));
100+
$this->assertTrue(is_array($this->mutableLoader->variableNames));
101+
$this->assertFalse(empty($this->mutableLoader->variableNames));
102+
100103
}
101104

102105
public function testImmutableLoaderSetUnsetImmutable()
@@ -121,5 +124,7 @@ public function testImmutableLoaderCannotClearEnvironmentVars()
121124
$this->assertSame($this->value(), getenv($this->key()));
122125
$this->assertSame(true, isset($_ENV[$this->key()]));
123126
$this->assertSame(true, isset($_SERVER[$this->key()]));
127+
$this->assertTrue(is_array($this->immutableLoader->variableNames));
128+
$this->assertFalse(empty($this->immutableLoader->variableNames));
124129
}
125130
}

0 commit comments

Comments
 (0)