Skip to content

Commit b56a067

Browse files
Backport Dotenv::parse()
1 parent c1e1038 commit b56a067

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

src/Dotenv.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
use Dotenv\Exception\InvalidPathException;
66
use Dotenv\Loader\Loader;
77
use Dotenv\Loader\LoaderInterface;
8+
use Dotenv\Repository\Adapter\ArrayAdapter;
89
use Dotenv\Repository\RepositoryBuilder;
910
use Dotenv\Repository\RepositoryInterface;
1011
use Dotenv\Store\FileStore;
1112
use Dotenv\Store\StoreBuilder;
13+
use Dotenv\Store\StringStore;
1214

1315
class Dotenv
1416
{
@@ -102,6 +104,47 @@ public static function createImmutable($paths, $names = null, $shortCircuit = tr
102104
return self::create($repository, $paths, $names, $shortCircuit);
103105
}
104106

107+
/**
108+
* Create a new dotenv instance with an array backed repository.
109+
*
110+
* @param string|string[] $paths
111+
* @param string|string[]|null $names
112+
* @param bool $shortCircuit
113+
*
114+
* @return \Dotenv\Dotenv
115+
*/
116+
public static function createArrayBacked($paths, $names = null, $shortCircuit = true)
117+
{
118+
$adapter = new ArrayAdapter();
119+
120+
$repository = RepositoryBuilder::create()->withReaders([$adapter])->withWriters([$adapter])->make();
121+
122+
return self::create($repository, $paths, $names, $shortCircuit);
123+
}
124+
125+
/**
126+
* Parse the given content and resolve nested variables.
127+
*
128+
* This method behaves just like load(), only without mutating your actual
129+
* environment. We do this by using an array backed repository.
130+
*
131+
* @param string $content
132+
*
133+
* @throws \Dotenv\Exception\InvalidFileException
134+
*
135+
* @return array<string,string|null>
136+
*/
137+
public static function parse($content)
138+
{
139+
$adapter = new ArrayAdapter();
140+
141+
$repository = RepositoryBuilder::create()->withReaders([$adapter])->withWriters([$adapter])->make();
142+
143+
$phpdotenv = new self(new Loader(), $repository, new StringStore($content));
144+
145+
return $phpdotenv->load();
146+
}
147+
105148
/**
106149
* Read and load environment file(s).
107150
*

src/Store/StringStore.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Dotenv\Store;
4+
5+
final class StringStore implements StoreInterface
6+
{
7+
/**
8+
* The file content.
9+
*
10+
* @var string
11+
*/
12+
private $content;
13+
14+
/**
15+
* Create a new string store instance.
16+
*
17+
* @param string $content
18+
*
19+
* @return void
20+
*/
21+
public function __construct($content)
22+
{
23+
$this->content = $content;
24+
}
25+
26+
/**
27+
* Read the content of the environment file(s).
28+
*
29+
* @return string
30+
*/
31+
public function read()
32+
{
33+
return $this->content;
34+
}
35+
}

tests/Dotenv/DotenvTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,4 +308,31 @@ public function testLatestConstructor()
308308
'NULL' => '',
309309
], $dotenv->load());
310310
}
311+
312+
public function testDotenvParseExample1()
313+
{
314+
$output = Dotenv::parse(
315+
"BASE_DIR=\"/var/webroot/project-root\"\nCACHE_DIR=\"\${BASE_DIR}/cache\"\nTMP_DIR=\"\${BASE_DIR}/tmp\"\n"
316+
);
317+
318+
self::assertSame($output, [
319+
'BASE_DIR' => '/var/webroot/project-root',
320+
'CACHE_DIR' => '/var/webroot/project-root/cache',
321+
'TMP_DIR' => '/var/webroot/project-root/tmp',
322+
]);
323+
}
324+
325+
public function testDotenvParseExample2()
326+
{
327+
$output = Dotenv::parse("FOO=Bar\nBAZ=\"Hello \${FOO}\"");
328+
329+
self::assertSame($output, ['FOO' => 'Bar', 'BAZ' => 'Hello Bar']);
330+
}
331+
332+
public function testDotenvParseEmptyCase()
333+
{
334+
$output = Dotenv::parse('');
335+
336+
self::assertSame($output, []);
337+
}
311338
}

0 commit comments

Comments
 (0)