|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace sixlive\DotenvEditor; |
|
4
|
|
|
|
|
5
|
|
|
use SplFileObject; |
|
6
|
|
|
use sixlive\DotenvEditor\Support\Arr; |
|
7
|
|
|
|
|
8
|
|
|
class EnvFile |
|
9
|
|
|
{ |
|
10
|
|
|
/** |
|
11
|
|
|
* @var \SplFileObject|null |
|
12
|
|
|
*/ |
|
13
|
|
|
protected $file; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @param string $path |
|
17
|
|
|
*/ |
|
18
|
10 |
|
public function __construct($path) |
|
19
|
|
|
{ |
|
20
|
10 |
|
$this->file = new SplFileObject($path, 'r+'); |
|
21
|
10 |
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Write content to the file. |
|
25
|
|
|
* |
|
26
|
|
|
* @param string $content |
|
27
|
|
|
* |
|
28
|
|
|
* @return self |
|
29
|
|
|
*/ |
|
30
|
7 |
|
public function write($content) |
|
31
|
|
|
{ |
|
32
|
7 |
|
$this->file->rewind(); |
|
33
|
7 |
|
$this->file->ftruncate(0); |
|
34
|
7 |
|
$this->file->fwrite($content); |
|
35
|
|
|
|
|
36
|
7 |
|
return $this; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if the file is NOT empty. |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
10 |
|
public function isNotEmpty() |
|
45
|
|
|
{ |
|
46
|
10 |
|
return $this->file->getSize() > 0; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Convert the file values into an associative array. |
|
51
|
|
|
* |
|
52
|
|
|
* @return array |
|
53
|
|
|
*/ |
|
54
|
4 |
|
public function toArray() |
|
55
|
|
|
{ |
|
56
|
4 |
|
return Arr::flatten($this->mapLineValuesAndKeys()); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Close the file buffer. |
|
61
|
|
|
* |
|
62
|
|
|
* @return void |
|
63
|
|
|
*/ |
|
64
|
10 |
|
public function close() |
|
65
|
|
|
{ |
|
66
|
10 |
|
$this->file = null; |
|
67
|
10 |
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Extract lines from the file. |
|
71
|
|
|
* |
|
72
|
|
|
* @return array |
|
73
|
|
|
*/ |
|
74
|
4 |
|
private function linesFromFile() |
|
75
|
|
|
{ |
|
76
|
4 |
|
return explode( |
|
77
|
4 |
|
"\n", |
|
78
|
4 |
|
$this->file->fread($this->file->getSize()) |
|
79
|
|
|
); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Convert config line into key value pairs. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array |
|
86
|
|
|
*/ |
|
87
|
4 |
|
private function convertLineValuesToArray() |
|
88
|
|
|
{ |
|
89
|
|
|
return array_map(function ($line) { |
|
90
|
4 |
|
return explode('=', $line); |
|
91
|
4 |
|
}, $this->linesFromFile()); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Map config line extraction into key value pairs. |
|
96
|
|
|
* |
|
97
|
|
|
* @return array |
|
98
|
|
|
*/ |
|
99
|
4 |
|
private function mapLineValuesAndKeys() |
|
100
|
|
|
{ |
|
101
|
|
|
return array_map(function ($line) { |
|
102
|
4 |
|
if (count($line) === 2) { |
|
103
|
4 |
|
return [$line[0] => $line[1]]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
4 |
|
return $line[0]; |
|
107
|
4 |
|
}, $this->convertLineValuesToArray()); |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|