1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace sixlive\DotenvEditor; |
4
|
|
|
|
5
|
|
|
use InvalidArgumentException; |
6
|
|
|
use sixlive\DotenvEditor\Support\Arr; |
7
|
|
|
|
8
|
|
|
class DotenvEditor |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var array |
12
|
|
|
*/ |
13
|
|
|
protected $env = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var \sixlive\DotenvEditor\EnvFile |
17
|
|
|
*/ |
18
|
|
|
protected $envFile; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Load an values from an env file. |
22
|
|
|
* |
23
|
|
|
* @param string $path |
24
|
|
|
* @return self |
25
|
|
|
* |
26
|
|
|
* @throws \InvalidArgumentException |
27
|
|
|
*/ |
28
|
13 |
|
public function load($path) |
29
|
|
|
{ |
30
|
13 |
|
if (! file_exists($path)) { |
31
|
1 |
|
throw new InvalidArgumentException(sprintf('%s does not exist', $path)); |
32
|
|
|
} |
33
|
|
|
|
34
|
12 |
|
$this->envFile = new EnvFile($path); |
35
|
|
|
|
36
|
12 |
|
if ($this->envFile->isNotEmpty()) { |
37
|
6 |
|
$this->env = array_merge($this->env, $this->envFile->toArray()); |
38
|
|
|
} |
39
|
|
|
|
40
|
12 |
|
return $this; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Set a key value pair for the env file. |
45
|
|
|
* |
46
|
|
|
* @param string $key |
47
|
|
|
* @param string $value |
48
|
|
|
* @return self |
49
|
|
|
*/ |
50
|
11 |
|
public function set($key, $value) |
51
|
|
|
{ |
52
|
11 |
|
$this->env[$key] = $value; |
53
|
|
|
|
54
|
11 |
|
return $this; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Unset a key value of the env file. |
59
|
|
|
* |
60
|
|
|
* @param string $key |
61
|
|
|
* @return self |
62
|
|
|
*/ |
63
|
5 |
|
public function unset($key) |
64
|
|
|
{ |
65
|
5 |
|
unset($this->env[$key]); |
66
|
|
|
|
67
|
5 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Get all of the env values or a single value by key. |
72
|
|
|
* |
73
|
|
|
* @param string $key |
74
|
|
|
* @return array|string |
75
|
|
|
*/ |
76
|
5 |
|
public function getEnv($key = '') |
77
|
|
|
{ |
78
|
5 |
|
return isset($this->env[$key]) |
79
|
2 |
|
? $this->env[$key] |
80
|
5 |
|
: $this->env; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Save the current representation to disk. If no path is specifed and |
85
|
|
|
* a file was loaded, it will overwrite the file that was loaded. |
86
|
|
|
* |
87
|
|
|
* @param string $path |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
9 |
|
public function save($path = '') |
91
|
|
|
{ |
92
|
9 |
|
if (empty($path) && $this->envFile) { |
93
|
8 |
|
return $this->envFile->write($this->format()) > 0; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
return file_put_contents($path, $this->format()) !== false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Add an empty line to the config. |
101
|
|
|
* @return self |
102
|
|
|
*/ |
103
|
2 |
|
public function addEmptyLine() |
104
|
|
|
{ |
105
|
2 |
|
$this->env[] = ''; |
106
|
|
|
|
107
|
2 |
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Add a comment heading. If there is a line before it, it will add an empty |
112
|
|
|
* line before the heading. |
113
|
|
|
* |
114
|
|
|
* @param string $heading |
115
|
|
|
* @return self |
116
|
|
|
*/ |
117
|
3 |
|
public function heading($heading) |
118
|
|
|
{ |
119
|
3 |
|
if (! empty(end($this->env))) { |
120
|
1 |
|
$this->addEmptyLine(); |
121
|
|
|
} |
122
|
|
|
|
123
|
3 |
|
$this->env[] = sprintf('# %s', $heading); |
124
|
|
|
|
125
|
3 |
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Check if a key is defined in the env. |
130
|
|
|
* |
131
|
|
|
* @param string $key |
132
|
|
|
* @return bool |
133
|
|
|
*/ |
134
|
1 |
|
public function has($key) |
135
|
|
|
{ |
136
|
1 |
|
return isset($this->env[$key]); |
137
|
|
|
} |
138
|
|
|
|
139
|
16 |
|
public function __destruct() |
140
|
|
|
{ |
141
|
16 |
|
if ($this->envFile) { |
142
|
12 |
|
$this->envFile->close(); |
143
|
|
|
} |
144
|
16 |
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Format the config file in key=value pairs. |
148
|
|
|
* |
149
|
|
|
* @return string |
150
|
|
|
*/ |
151
|
9 |
|
private function format() |
152
|
|
|
{ |
153
|
|
|
$valuePairs = Arr::mapWithKeys($this->env, function ($item, $key) { |
154
|
9 |
|
return is_string($key) |
155
|
9 |
|
? sprintf('%s=%s', $key, $item) |
156
|
9 |
|
: $item; |
157
|
9 |
|
}); |
158
|
|
|
|
159
|
9 |
|
return implode("\n", $valuePairs); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|