Skip to content

Commit

Permalink
chore: reorder where functions are defined
Browse files Browse the repository at this point in the history
  • Loading branch information
josegonzalez committed Jan 3, 2017
1 parent b07b572 commit ff3461f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 56 deletions.
28 changes: 14 additions & 14 deletions src/josegonzalez/Dotenv/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ class Loader

protected $skip = array(
'define' => false,
'putenv' => false,
'toEnv' => false,
'toServer' => false,
'putenv' => false
);

public function __construct($filepaths = null)
Expand Down Expand Up @@ -66,9 +66,9 @@ public static function load($options = null)
'prefix',
'expect',
'define',
'putenv',
'toEnv',
'toServer',
'putenv',
);
foreach ($methods as $method) {
if (array_key_exists($method, $options)) {
Expand Down Expand Up @@ -252,45 +252,45 @@ public function define()
return $this;
}

public function toEnv($overwrite = false)
public function putenv($overwrite = false)
{
$this->requireParse('toEnv');
$this->requireParse('putenv');
foreach ($this->environment as $key => $value) {
$prefixedKey = $this->prefixed($key);
if (isset($_ENV[$prefixedKey]) && !$overwrite) {
if ($this->skip['toEnv']) {
if (getenv($prefixedKey) && !$overwrite) {
if ($this->skip['putenv']) {
continue;
}

return $this->raise(
'LogicException',
sprintf('Key "%s" has already been defined in $_ENV', $prefixedKey)
sprintf('Key "%s" has already been defined in getenv()', $prefixedKey)
);
}

$_ENV[$prefixedKey] = $value;
putenv($prefixedKey . '=' . $value);
}

return $this;
}

public function putenv($overwrite = false)
public function toEnv($overwrite = false)
{
$this->requireParse('putenv');
$this->requireParse('toEnv');
foreach ($this->environment as $key => $value) {
$prefixedKey = $this->prefixed($key);
if (getenv($prefixedKey) && !$overwrite) {
if ($this->skip['putenv']) {
if (isset($_ENV[$prefixedKey]) && !$overwrite) {
if ($this->skip['toEnv']) {
continue;
}

return $this->raise(
'LogicException',
sprintf('Key "%s" has already been defined in getenv()', $prefixedKey)
sprintf('Key "%s" has already been defined in $_ENV', $prefixedKey)
);
}

putenv($prefixedKey . '=' . $value);
$_ENV[$prefixedKey] = $value;
}

return $this;
Expand Down
84 changes: 42 additions & 42 deletions tests/josegonzalez/Dotenv/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -580,48 +580,6 @@ public function testDefineException()
$this->Loader->define();
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
*/
public function testToEnv()
{
$this->Loader->parse();
$this->Loader->toEnv(false);

$this->assertEquals('bar', $_ENV['FOO']);
$this->assertEquals('baz', $_ENV['BAR']);
$this->assertEquals('with spaces', $_ENV['SPACED']);
$this->assertEquals('pgsql:host=localhost;dbname=test', $_ENV['EQUALS']);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
*/
public function testToEnvSkip()
{
$this->Loader->parse();
$this->Loader->skipExisting('toEnv');
$this->Loader->toEnv(false);
$this->Loader->toEnv(false);

$this->assertEquals('bar', $_ENV['FOO']);
$this->assertEquals('baz', $_ENV['BAR']);
$this->assertEquals('with spaces', $_ENV['SPACED']);
$this->assertEquals('pgsql:host=localhost;dbname=test', $_ENV['EQUALS']);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
* @expectedException LogicException
* @expectedExceptionMessage Key "FOO" has already been defined in $_ENV
*/
public function testToEnvException()
{
$this->Loader->parse();
$this->Loader->toEnv(false);
$this->Loader->toEnv(false);
}

/**
* @covers \josegonzalez\Dotenv\Loader::putenv
*/
Expand Down Expand Up @@ -664,6 +622,48 @@ public function testToPutenvException()
$this->Loader->putenv(false);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
*/
public function testToEnv()
{
$this->Loader->parse();
$this->Loader->toEnv(false);

$this->assertEquals('bar', $_ENV['FOO']);
$this->assertEquals('baz', $_ENV['BAR']);
$this->assertEquals('with spaces', $_ENV['SPACED']);
$this->assertEquals('pgsql:host=localhost;dbname=test', $_ENV['EQUALS']);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
*/
public function testToEnvSkip()
{
$this->Loader->parse();
$this->Loader->skipExisting('toEnv');
$this->Loader->toEnv(false);
$this->Loader->toEnv(false);

$this->assertEquals('bar', $_ENV['FOO']);
$this->assertEquals('baz', $_ENV['BAR']);
$this->assertEquals('with spaces', $_ENV['SPACED']);
$this->assertEquals('pgsql:host=localhost;dbname=test', $_ENV['EQUALS']);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toEnv
* @expectedException LogicException
* @expectedExceptionMessage Key "FOO" has already been defined in $_ENV
*/
public function testToEnvException()
{
$this->Loader->parse();
$this->Loader->toEnv(false);
$this->Loader->toEnv(false);
}

/**
* @covers \josegonzalez\Dotenv\Loader::toServer
*/
Expand Down

0 comments on commit ff3461f

Please sign in to comment.