Skip to content

Commit 08b7fbc

Browse files
authored
Merge pull request #215 from arifmahmudrana/remove-double-instantiation-loader-class-and-add-set-immutable-method
remove double instantiation of loader class
2 parents e5a077e + 597f578 commit 08b7fbc

File tree

3 files changed

+46
-3
lines changed

3 files changed

+46
-3
lines changed

src/Dotenv.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ protected function getFilePath($path, $file)
8686
*/
8787
protected function loadData($overload = false)
8888
{
89-
$this->loader = new Loader($this->filePath, !$overload);
90-
91-
return $this->loader->load();
89+
return $this->loader->setImmutable(!$overload)->load();
9290
}
9391

9492
/**

src/Loader.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,29 @@ public function __construct($filePath, $immutable = false)
4242
$this->immutable = $immutable;
4343
}
4444

45+
/**
46+
* Set immutable value.
47+
*
48+
* @param bool $immutable
49+
* @return $this
50+
*/
51+
public function setImmutable($immutable = false)
52+
{
53+
$this->immutable = $immutable;
54+
55+
return $this;
56+
}
57+
58+
/**
59+
* Set immutable value.
60+
*
61+
* @return bool
62+
*/
63+
public function getImmutable()
64+
{
65+
return $this->immutable;
66+
}
67+
4568
/**
4669
* Load `.env` file in given directory.
4770
*

tests/Dotenv/LoaderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ protected function value()
7474
return reset($keyVal);
7575
}
7676

77+
public function testMutableLoaderSetUnsetImmutable()
78+
{
79+
$immutable = $this->mutableLoader->getImmutable();
80+
81+
// Set Immutable.
82+
$this->mutableLoader->setImmutable(!$immutable);
83+
$this->assertSame(!$immutable, $this->mutableLoader->getImmutable());
84+
$this->mutableLoader->setImmutable($immutable);
85+
$this->assertSame($immutable, $this->mutableLoader->getImmutable());
86+
}
87+
7788
public function testMutableLoaderClearsEnvironmentVars()
7889
{
7990
// Set an environment variable.
@@ -87,6 +98,17 @@ public function testMutableLoaderClearsEnvironmentVars()
8798
$this->assertSame(false, isset($_SERVER[$this->key()]));
8899
}
89100

101+
public function testImmutableLoaderSetUnsetImmutable()
102+
{
103+
$immutable = $this->immutableLoader->getImmutable();
104+
105+
// Set Immutable.
106+
$this->immutableLoader->setImmutable(!$immutable);
107+
$this->assertSame(!$immutable, $this->immutableLoader->getImmutable());
108+
$this->immutableLoader->setImmutable($immutable);
109+
$this->assertSame($immutable, $this->immutableLoader->getImmutable());
110+
}
111+
90112
public function testImmutableLoaderCannotClearEnvironmentVars()
91113
{
92114
// Set an environment variable.

0 commit comments

Comments
 (0)