Skip to content

Commit

Permalink
Fixed handling multiple occurences of a variable
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Jun 15, 2019
1 parent ffcaf1d commit 5084b23
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 35 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"extra": {
"branch-alias": {
"dev-master": "3.3-dev"
"dev-master": "3.4-dev"
}
}
}
106 changes: 106 additions & 0 deletions src/Environment/AbstractVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

namespace Dotenv\Environment;

use Dotenv\Environment\Adapter\ArrayAdapter;
use InvalidArgumentException;

/**
* This is the abstract variables implementation.
*
Expand All @@ -16,6 +19,13 @@ abstract class AbstractVariables implements VariablesInterface
*/
private $immutable;

/**
* The record of loaded variables.
*
* @var \Dotenv\Environment\Adapter\ArrayAdapter
*/
private $loaded;

/**
* Create a new environment variables instance.
*
Expand All @@ -26,8 +36,104 @@ abstract class AbstractVariables implements VariablesInterface
public function __construct($immutable)
{
$this->immutable = $immutable;
$this->loaded = new ArrayAdapter();
}

/**
* Get an environment variable.
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

return $this->getInternal($name);
}

/**
* Get an environment variable.
*
* @param string $name
*
* @return string|null
*/
protected abstract function getInternal($name);

/**
* Set an environment variable.
*
* @param string $name
* @param string|null $value
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function set($name, $value = null)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

// Don't overwrite existing environment variables if we're immutable
// Ruby's dotenv does this with `ENV[key] ||= value`.
if ($this->isImmutable() && $this->get($name) !== null && $this->loaded->get($name)->isEmpty()) {
return;
}

$this->setInternal($name, $value);
$this->loaded->set($name, '');
}

/**
* Set an environment variable.
*
* @param string $name
* @param string|null $value
*
* @return void
*/
protected abstract function setInternal($name, $value = null);

/**
* Clear an environment variable.
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function clear($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

// Don't clear anything if we're immutable.
if ($this->isImmutable()) {
return;
}

$this->clearInternal($name);
}

/**
* Clear an environment variable.
*
* @param string $name
*
* @return void
*/
protected abstract function clearInternal($name);

/**
* Determine if the environment is immutable.
*
Expand Down
37 changes: 3 additions & 34 deletions src/Environment/DotenvVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace Dotenv\Environment;

use InvalidArgumentException;

/**
* The default implementation of the environment variables interface.
*/
Expand Down Expand Up @@ -37,16 +35,10 @@ public function __construct(array $adapters, $immutable)
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return string|null
*/
public function get($name)
protected function getInternal($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

foreach ($this->adapters as $adapter) {
$result = $adapter->get($name);
if ($result->isDefined()) {
Expand All @@ -61,22 +53,10 @@ public function get($name)
* @param string $name
* @param string|null $value
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function set($name, $value = null)
protected function setInternal($name, $value = null)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

// Don't overwrite existing environment variables if we're immutable
// Ruby's dotenv does this with `ENV[key] ||= value`.
if ($this->isImmutable() && $this->get($name) !== null) {
return;
}

foreach ($this->adapters as $adapter) {
$adapter->set($name, $value);
}
Expand All @@ -87,21 +67,10 @@ public function set($name, $value = null)
*
* @param string $name
*
* @throws \InvalidArgumentException
*
* @return void
*/
public function clear($name)
protected function clearInternal($name)
{
if (!is_string($name)) {
throw new InvalidArgumentException('Expected name to be a string.');
}

// Don't clear anything if we're immutable.
if ($this->isImmutable()) {
return;
}

foreach ($this->adapters as $adapter) {
$adapter->clear($name);
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Dotenv/DotenvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@ public function testLargeDotenvLoadsEnvironmentVars()
$this->assertNotEmpty(getenv('LARGE'));
}

public function testMultipleDotenvLoadsEnvironmentVars()
{
$dotenv = Dotenv::create($this->fixturesFolder, 'multiple.env');
$dotenv->load();
$this->assertSame('bar', getenv('MULTI1'));
$this->assertSame('foo', getenv('MULTI2'));
}

public function testExportedDotenvLoadsEnvironmentVars()
{
$dotenv = Dotenv::create($this->fixturesFolder, 'exported.env');
Expand Down
4 changes: 4 additions & 0 deletions tests/fixtures/env/multiple.env
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
MULTI1=foo
MULTI2=${MULTI1}
MULTI1=bar

0 comments on commit 5084b23

Please sign in to comment.