Repository navigation
Expand file tree
/
Copy pathLoader.php
More file actions
118 lines (108 loc) · 3.42 KB
/
Copy pathLoader.php
File metadata and controls
118 lines (108 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace Dotenv\Loader;
use Dotenv\Regex\Regex;
use Dotenv\Repository\RepositoryInterface;
use PhpOption\Option;
class Loader implements LoaderInterface
{
/**
* The variable name whitelist.
*
* @var string[]|null
*/
protected $whitelist;
/**
* Create a new loader instance.
*
* @param string[]|null $whitelist
*
* @return void
*/
public function __construct(array $whitelist = null)
{
$this->whitelist = $whitelist;
}
/**
* Load the given environment file content into the repository.
*
* @param \Dotenv\Repository\RepositoryInterface $repository
* @param string $content
*
* @throws \Dotenv\Exception\InvalidFileException
*
* @return array<string,string|null>
*/
public function load(RepositoryInterface $repository, $content)
{
return $this->processEntries(
$repository,
Lines::process(Regex::split("/(\r\n|\n|\r)/", $content)->getSuccess())
);
}
/**
* Process the environment variable entries.
*
* We'll fill out any nested variables, and acually set the variable using
* the underlying environment variables instance.
*
* @param \Dotenv\Repository\RepositoryInterface $repository
* @param string[] $entries
*
* @throws \Dotenv\Exception\InvalidFileException
*
* @return array<string,string|null>
*/
private function processEntries(RepositoryInterface $repository, array $entries)
{
$vars = [];
foreach ($entries as $entry) {
list($name, $value) = Parser::parse($entry);
if ($this->whitelist === null || in_array($name, $this->whitelist, true)) {
$vars[$name] = self::resolveNestedVariables($repository, $value);
$repository->set($name, $vars[$name]);
}
}
return $vars;
}
/**
* Resolve the nested variables.
*
* Look for ${varname} patterns in the variable value and replace with an
* existing environment variable.
*
* @param \Dotenv\Repository\RepositoryInterface $repository
* @param \Dotenv\Loader\Value|null $value
*
* @return string|null
*/
private static function resolveNestedVariables(RepositoryInterface $repository, Value $value = null)
{
return Option::fromValue($value)
->map(function (Value $v) use ($repository) {
return array_reduce($v->getVars(), function ($s, $i) use ($repository) {
return substr($s, 0, $i).self::resolveNestedVariable($repository, substr($s, $i));
}, $v->getChars());
})
->getOrElse(null);
}
/**
* Resolve a single nested variable.
*
* @param \Dotenv\Repository\RepositoryInterface $repository
* @param string $str
*
* @return string
*/
private static function resolveNestedVariable(RepositoryInterface $repository, $str)
{
return Regex::replaceCallback(
'/\A\${([a-zA-Z0-9_.]+)}/',
function (array $matches) use ($repository) {
return Option::fromValue($repository->get($matches[1]))
->getOrElse($matches[0]);
},
$str,
1
)->success()->getOrElse($str);
}
}