Skip to content

Commit

Permalink
Added the list of environment variable declared inside the 'env' file (
Browse files Browse the repository at this point in the history
  • Loading branch information
jclebreton authored and GrahamCampbell committed Jul 1, 2018
1 parent 2ecccbc commit 6ae3e2e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/Dotenv.php
Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions src/Loader.php
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 8 additions & 0 deletions tests/Dotenv/DotenvTest.php
Expand Up @@ -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());
}
}
5 changes: 5 additions & 0 deletions tests/Dotenv/LoaderTest.php
Expand Up @@ -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()
Expand All @@ -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));
}
}

0 comments on commit 6ae3e2e

Please sign in to comment.