-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathDotEnv.php
117 lines (95 loc) · 3.12 KB
/
DotEnv.php
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
<?php
namespace DevCoder;
class DotEnv
{
/**
* The directory where the .env file can be located.
*
* @var string
*/
protected $path;
/**
* Configure the options on which the parsed will act
*
* @var array
*/
protected $options = [];
/**
* Process strings to obtain characteristics of it
*
* @var Processor
*/
protected $processor;
public function __construct(string $path, array $options = [])
{
if (!file_exists($path)) {
throw new \InvalidArgumentException(sprintf('%s does not exist', $path));
}
$this->path = $path;
$this->processOptions($options);
$this->processor = new Processor();
}
private function processOptions(array $options) : void
{
$this->options = array_merge([
Option::PROCESS_BOOLEANS => true,
Option::PROCESS_QUOTES => true
], $options);
}
/**
* Processes the $path of the instances and parses the values into $_SERVER and $_ENV, also returns all the data that has been read.
* Skips empty and commented lines.
*/
public function load() : void
{
if (!is_readable($this->path)) {
throw new \RuntimeException(sprintf('%s file is not readable', $this->path));
}
$lines = file($this->path, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
foreach ($lines as $line) {
if (strpos(trim($line), '#') === 0) {
continue;
}
list($name, $value) = explode('=', $line, 2);
$name = trim($name);
$value = $this->processValue($value);
if (!array_key_exists($name, $_SERVER) && !array_key_exists($name, $_ENV)) {
putenv(sprintf('%s=%s', $name, $value));
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
}
}
}
private function processValue(string $value)
{
/**
* First trim spaces and quotes if configured
*/
$preprocessedValue = $this->preprocessValue($value);
/**
* If the value is a boolean resolve it as such
*/
if (!empty($this->options[Option::PROCESS_BOOLEANS])) {
$isBoolean = $this->processor->isBoolean($preprocessedValue);
if ($isBoolean) {
return $this->processor->resolveAsBoolean($preprocessedValue);
}
}
/**
* Does not match any processor options, return as is
*/
return $preprocessedValue;
}
private function preprocessValue(string $value) : string
{
$trimmedValue = trim($value);
if (!empty($this->options[Option::PROCESS_QUOTES])) {
$wrappedBySingleQuotes = $this->processor->isWrappedByChar($trimmedValue, '\'');
$wrappedByDoubleQuotes = $this->processor->isWrappedByChar($trimmedValue, '"');
if ($wrappedBySingleQuotes || $wrappedByDoubleQuotes) {
return $this->processor->removeFirstAndLastChar($trimmedValue);
}
}
return $trimmedValue;
}
}