Skip to content

Commit 736602f

Browse files
Merge pull request #321 from GrahamCampbell/direct-load
Support direct loading with a string
2 parents 13be13d + 2e03131 commit 736602f

File tree

6 files changed

+117
-32
lines changed

6 files changed

+117
-32
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"extra": {
2626
"branch-alias": {
27-
"dev-master": "3.1-dev"
27+
"dev-master": "3.2-dev"
2828
}
2929
}
3030
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Dotenv\Environment\Adapter;
4+
5+
use PhpOption\None;
6+
use PhpOption\Some;
7+
8+
class ArrayAdapter implements AdapterInterface
9+
{
10+
/**
11+
* The variables and their values.
12+
*
13+
* @return array<string|null>
14+
*/
15+
private $variables = [];
16+
17+
/**
18+
* Determines if the adapter is supported.
19+
*
20+
* @return bool
21+
*/
22+
public function isSupported()
23+
{
24+
return true;
25+
}
26+
27+
/**
28+
* Get an environment variable, if it exists.
29+
*
30+
* @param string $name
31+
*
32+
* @return \PhpOption\Option
33+
*/
34+
public function get($name)
35+
{
36+
if (array_key_exists($name, $this->variables)) {
37+
return Some::create($this->variables[$name]);
38+
}
39+
40+
return None::create();
41+
}
42+
43+
/**
44+
* Set an environment variable.
45+
*
46+
* @param string $name
47+
* @param string|null $value
48+
*
49+
* @return void
50+
*/
51+
public function set($name, $value = null)
52+
{
53+
$this->variables[$name] = $value;
54+
}
55+
56+
/**
57+
* Clear an environment variable.
58+
*
59+
* @param string $name
60+
*
61+
* @return void
62+
*/
63+
public function clear($name)
64+
{
65+
unset($this->variables[$name]);
66+
}
67+
}

src/Loader.php

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,45 @@ public function setImmutable($immutable = false)
7979
}
8080

8181
/**
82-
* Load `.env` file in given directory.
82+
* Load the environment file from disk.
8383
*
8484
* @throws \Dotenv\Exception\InvalidPathException|\Dotenv\Exception\InvalidFileException
8585
*
8686
* @return array<string|null>
8787
*/
8888
public function load()
89+
{
90+
return $this->loadDirect(
91+
self::findAndRead($this->filePaths)
92+
);
93+
}
94+
95+
/**
96+
* Directly load the given string.
97+
*
98+
* @param string $content
99+
*
100+
* @throws \Dotenv\Exception\InvalidFileException
101+
*
102+
* @return array<string|null>
103+
*/
104+
public function loadDirect($content)
89105
{
90106
return $this->processEntries(
91-
Lines::process(self::readLines($this->filePaths))
107+
Lines::process(preg_split("/(\r\n|\n|\r)/", $content))
92108
);
93109
}
94110

95111
/**
96-
* Attempt to read the lines from the files in order.
112+
* Attempt to read the files in order.
97113
*
98114
* @param string[] $filePaths
99115
*
100116
* @throws \Dotenv\Exception\InvalidPathException
101117
*
102118
* @return string[]
103119
*/
104-
private static function readLines(array $filePaths)
120+
private static function findAndRead(array $filePaths)
105121
{
106122
if ($filePaths === []) {
107123
throw new InvalidPathException('At least one environment file path must be provided.');
@@ -120,20 +136,17 @@ private static function readLines(array $filePaths)
120136
}
121137

122138
/**
123-
* Read from the file, auto detecting line endings.
139+
* Read the given file.
124140
*
125141
* @param string $filePath
126142
*
127143
* @return \PhpOption\Option
128144
*/
129145
private static function readFromFile($filePath)
130146
{
131-
$autodetect = ini_get('auto_detect_line_endings');
132-
ini_set('auto_detect_line_endings', '1');
133-
$lines = @file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
134-
ini_set('auto_detect_line_endings', $autodetect);
147+
$content = @file_get_contents($filePath);
135148

136-
return Option::fromValue($lines, false);
149+
return Option::fromValue($content, false);
137150
}
138151

139152
/**

tests/Dotenv/LinesTest.php

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,9 @@
55

66
class LinesTest extends TestCase
77
{
8-
/**
9-
* @var string|false
10-
*/
11-
protected $autodetect;
12-
13-
public function setUp()
14-
{
15-
$this->autodetect = ini_get('auto_detect_line_endings');
16-
ini_set('auto_detect_line_endings', '1');
17-
}
18-
19-
public function tearDown()
20-
{
21-
ini_set('auto_detect_line_endings', $this->autodetect);
22-
}
23-
248
public function testProcess()
259
{
26-
$content = file(
27-
dirname(__DIR__).'/fixtures/env/assertions.env',
28-
FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES
29-
);
10+
$content = file_get_contents(dirname(__DIR__).'/fixtures/env/assertions.env');
3011

3112
$expected = [
3213
'ASSERTVAR1=val1',
@@ -40,6 +21,6 @@ public function testProcess()
4021
"ASSERTVAR9=\"\n\"",
4122
];
4223

43-
$this->assertSame($expected, Lines::process($content));
24+
$this->assertSame($expected, Lines::process(preg_split("/(\r\n|\n|\r)/", $content)));
4425
}
4526
}

tests/Dotenv/LoaderTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
22

3+
use Dotenv\Environment\Adapter\ArrayAdapter;
34
use Dotenv\Environment\DotenvFactory;
45
use Dotenv\Loader;
56
use PHPUnit\Framework\TestCase;
@@ -124,4 +125,24 @@ public function testLoaderWithOneGoodPath()
124125

125126
$this->assertCount(4, $loader->load());
126127
}
128+
129+
public function testLoaderWithNoAdapters()
130+
{
131+
$loader = (new Loader([], new DotenvFactory([])));
132+
133+
$content = "NVAR1=\"Hello\"\nNVAR2=\"World!\"\nNVAR3=\"{\$NVAR1} {\$NVAR2}\"\nNVAR4=\"\${NVAR1} \${NVAR2}\"";
134+
$expected = ['NVAR1' => 'Hello', 'NVAR2' => 'World!', 'NVAR3' => '{$NVAR1} {$NVAR2}', 'NVAR4' => '${NVAR1} ${NVAR2}'];
135+
136+
$this->assertSame($expected, $loader->loadDirect($content));
137+
}
138+
139+
public function testLoaderWithArrayAdapter()
140+
{
141+
$loader = (new Loader([], new DotenvFactory([new ArrayAdapter()])));
142+
143+
$content = "NVAR1=\"Hello\"\nNVAR2=\"World!\"\nNVAR3=\"{\$NVAR1} {\$NVAR2}\"\nNVAR4=\"\${NVAR1} \${NVAR2}\"";
144+
$expected = ['NVAR1' => 'Hello', 'NVAR2' => 'World!', 'NVAR3' => '{$NVAR1} {$NVAR2}', 'NVAR4' => 'Hello World!'];
145+
146+
$this->assertSame($expected, $loader->loadDirect($content));
147+
}
127148
}

tests/fixtures/env/assertions.env

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
ASSERTVAR1=val1
22

33
ASSERTVAR2=""
4+
45
ASSERTVAR3="val3 "
56
ASSERTVAR4="0" # empty looking value
67
ASSERTVAR5=#foo
78
ASSERTVAR6="val1
89
val2"
910
ASSERTVAR7="
1011
val3" #
12+
13+
1114
ASSERTVAR8="val3
1215
"
1316
ASSERTVAR9="

0 commit comments

Comments
 (0)