|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Maestriam\Samurai\Foundation; |
|
4
|
|
|
|
|
5
|
|
|
use Maestriam\Samurai\Exceptions\EnvNotFoundException; |
|
6
|
|
|
|
|
7
|
|
|
class EnvHandler |
|
8
|
|
|
{ |
|
9
|
|
|
protected $filename; |
|
10
|
|
|
|
|
11
|
|
|
public function __construct() |
|
12
|
|
|
{ |
|
13
|
|
|
if (! $this->exists()) { |
|
14
|
|
|
$this->initEnv(); |
|
15
|
|
|
} |
|
16
|
|
|
} |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Cria um novo arquivo de configurações do ambiente do projeto |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $custom |
|
22
|
|
|
* @return integer |
|
23
|
|
|
*/ |
|
24
|
|
|
public function initEnv() : int |
|
25
|
|
|
{ |
|
26
|
|
|
return touch($this->file()); |
|
|
|
|
|
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Retorna o nome do arquivo de configurações de ambiente do projeto |
|
31
|
|
|
* |
|
32
|
|
|
* @return string |
|
33
|
|
|
*/ |
|
34
|
|
|
public function file() : string |
|
35
|
|
|
{ |
|
36
|
|
|
$file = config('samurai.env_file') ?? '.env'; |
|
37
|
|
|
|
|
38
|
|
|
return base_path($file); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Undocumented function |
|
43
|
|
|
* |
|
44
|
|
|
* @param string $key |
|
45
|
|
|
* @param string $value |
|
46
|
|
|
* @return void |
|
47
|
|
|
*/ |
|
48
|
|
|
public function set(string $key, string $value) |
|
49
|
|
|
{ |
|
50
|
|
|
$line = $this->existsKey($key); |
|
51
|
|
|
|
|
52
|
|
|
if ($line == null) { |
|
|
|
|
|
|
53
|
|
|
return $this->append($key, $value); |
|
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return $this->change($line, $key, $value); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Retorna o valor de uma chave dentro .env |
|
61
|
|
|
* |
|
62
|
|
|
* @param string $key |
|
63
|
|
|
* @return string|null |
|
64
|
|
|
*/ |
|
65
|
|
|
public function get(string $key) : ?string |
|
66
|
|
|
{ |
|
67
|
|
|
$no = $this->existsKey($key); |
|
68
|
|
|
|
|
69
|
|
|
if ($no === null) { return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
$lines = $this->lines(); |
|
73
|
|
|
|
|
74
|
|
|
$pieces = explode('=', $lines[$no]); |
|
75
|
|
|
|
|
76
|
|
|
return $pieces[1]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Verifica se uma chave existe no arquivo de .env |
|
81
|
|
|
* Se existir, retorna seu índice dentro do array |
|
82
|
|
|
* Caso contrário, nulo |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $key |
|
85
|
|
|
* @return integer|null |
|
86
|
|
|
*/ |
|
87
|
|
|
public function existsKey(string $key) : ?int |
|
88
|
|
|
{ |
|
89
|
|
|
$search = null; |
|
90
|
|
|
$lines = $this->lines(); |
|
91
|
|
|
$pattern = strtoupper('/'. $key . '=/'); |
|
92
|
|
|
|
|
93
|
|
|
if (empty($lines)) { return null; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach($lines as $no => $line) { |
|
97
|
|
|
if (preg_match($pattern, $line)) { |
|
98
|
|
|
$search = $no; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
return $search; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Retorna todas as linhas do arquivo de configuração |
|
107
|
|
|
* dividas em um array |
|
108
|
|
|
* |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
private function lines() : array |
|
112
|
|
|
{ |
|
113
|
|
|
$content = $this->content(); |
|
114
|
|
|
|
|
115
|
|
|
return explode("\n", $content); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Verifica se o arquivo .env está configurado |
|
120
|
|
|
* corretamente na base do projeto |
|
121
|
|
|
* |
|
122
|
|
|
* @return void |
|
123
|
|
|
*/ |
|
124
|
|
|
public function exists() : bool |
|
125
|
|
|
{ |
|
126
|
|
|
return is_file($this->file()); |
|
|
|
|
|
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* Retorna o conteúdo do arquivo .env |
|
131
|
|
|
* |
|
132
|
|
|
* @return string |
|
133
|
|
|
*/ |
|
134
|
|
|
public function content() : string |
|
135
|
|
|
{ |
|
136
|
|
|
if (! $this->exists()) { |
|
137
|
|
|
throw new EnvNotFoundException(); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return file_get_contents($this->file()); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
/** |
|
144
|
|
|
* Adiciona uma nova chave no arquivo .env |
|
145
|
|
|
* |
|
146
|
|
|
* @param string $key |
|
147
|
|
|
* @param string $value |
|
148
|
|
|
* @return void |
|
149
|
|
|
*/ |
|
150
|
|
|
private function append(string $key, string $value) |
|
151
|
|
|
{ |
|
152
|
|
|
$lines = $this->lines(); |
|
153
|
|
|
$content = strtoupper($key) . '=' . $value; |
|
154
|
|
|
$lines[] = $content; |
|
155
|
|
|
|
|
156
|
|
|
return $this->store($lines); |
|
|
|
|
|
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Substitui o valor de uma chave exsitente |
|
161
|
|
|
* no arquivo .env |
|
162
|
|
|
* |
|
163
|
|
|
* @param string $key |
|
164
|
|
|
* @param string $value |
|
165
|
|
|
* @return void |
|
166
|
|
|
*/ |
|
167
|
|
|
private function change(int $line, string $key, string $value) |
|
168
|
|
|
{ |
|
169
|
|
|
$lines = $this->lines(); |
|
170
|
|
|
$content = strtoupper($key) . '=' . $value; |
|
171
|
|
|
|
|
172
|
|
|
$lines[$line] = $content; |
|
173
|
|
|
|
|
174
|
|
|
return $this->store($lines); |
|
|
|
|
|
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Undocumented function |
|
179
|
|
|
* |
|
180
|
|
|
* @param array $lines |
|
181
|
|
|
* @return void |
|
182
|
|
|
*/ |
|
183
|
|
|
private function store(array $lines) |
|
184
|
|
|
{ |
|
185
|
|
|
$content = implode("\n", $lines); |
|
186
|
|
|
|
|
187
|
|
|
return file_put_contents($this->file(), $content); |
|
|
|
|
|
|
188
|
|
|
} |
|
189
|
|
|
} |
|
190
|
|
|
|