|
3 | 3 | namespace EnvEditor;
|
4 | 4 |
|
5 | 5 | use EnvEditor\EnvFile\Block;
|
6 |
| -use EnvEditor\EnvFile\Visitor; |
7 |
| -use EnvEditor\EnvFile\EOLType; |
8 | 6 | use EnvEditor\EnvFile\Block\Variable as VariableBlock;
|
| 7 | +use EnvEditor\EnvFile\EOLType; |
| 8 | +use EnvEditor\EnvFile\Visitor; |
| 9 | + |
| 10 | +class EnvFile |
| 11 | +{ |
9 | 12 |
|
10 |
| -class EnvFile { |
| 13 | + public string $EOL = "\n"; |
11 | 14 |
|
12 |
| - public string $EOL = "\n"; |
| 15 | + /** @var Block[] */ |
| 16 | + public array $blocks = []; |
13 | 17 |
|
14 |
| - /** @var Block[] */ |
15 |
| - public array $blocks = []; |
| 18 | + function __construct() |
| 19 | + { |
| 20 | + $this->EOL = EOLType::UNIX; |
| 21 | + } |
16 | 22 |
|
17 |
| - function __construct() { |
18 |
| - $this->EOL = EOLType::UNIX; |
19 |
| - } |
| 23 | + public static function loadFrom(string $path): EnvFile |
| 24 | + { |
| 25 | + $parser = new Parser(); |
| 26 | + $content = file_get_contents($path); |
20 | 27 |
|
21 |
| - public function visitBlocks(Visitor $visitor): void |
22 |
| - { |
23 |
| - foreach($this->blocks as $block) { |
24 |
| - $block->visit($visitor); |
| 28 | + return $parser->parse($content); |
25 | 29 | }
|
26 |
| - } |
27 |
| - |
28 |
| - /** |
29 |
| - * @return VariableBlock[] |
30 |
| - */ |
31 |
| - public function getVariableBlocks(): array { |
32 |
| - return array_values(array_filter($this->blocks, function($block){ |
33 |
| - return $block instanceof VariableBlock; |
34 |
| - })); |
35 |
| - } |
36 |
| - |
37 |
| - public function findVariable(string $key): ?VariableBlock { |
38 |
| - foreach($this->getVariableBlocks() as $block) { |
39 |
| - if($block->key == $key) { |
40 |
| - return $block; |
41 |
| - } |
| 30 | + |
| 31 | + public function visitBlocks(Visitor $visitor): void |
| 32 | + { |
| 33 | + foreach ($this->blocks as $block) { |
| 34 | + $block->visit($visitor); |
| 35 | + } |
42 | 36 | }
|
43 | 37 |
|
44 |
| - return null; |
45 |
| - } |
| 38 | + public function putVariable(VariableBlock $variable): void |
| 39 | + { |
| 40 | + foreach ($this->blocks as $i => $block) { |
46 | 41 |
|
47 |
| - public function putVariable(VariableBlock $variable): void |
48 |
| - { |
49 |
| - foreach($this->blocks as $i => $block) { |
| 42 | + if (!$block instanceof VariableBlock) continue; |
50 | 43 |
|
51 |
| - if(!$block instanceof VariableBlock) continue; |
| 44 | + if ($block->key->content == $variable->key->content) { |
| 45 | + $this->blocks[$i] = $variable; |
| 46 | + return; |
| 47 | + } |
| 48 | + } |
52 | 49 |
|
53 |
| - if($block->key->content == $variable->key->content) { |
54 |
| - $this->blocks[$i] = $variable; |
55 |
| - return; |
56 |
| - } |
| 50 | + $this->blocks[] = $variable; |
57 | 51 | }
|
58 | 52 |
|
59 |
| - $this->blocks[] = $variable; |
60 |
| - } |
| 53 | + public function removeVariableKey(string $key): ?VariableBlock |
| 54 | + { |
| 55 | + $variable = $this->findVariable($key); |
| 56 | + if (!$variable) return null; |
61 | 57 |
|
62 |
| - public function removeVariableKey(string $key): ?VariableBlock { |
63 |
| - $variable = $this->findVariable($key); |
64 |
| - if(!$variable) return null; |
| 58 | + $index = array_search($variable, $this->blocks); |
| 59 | + array_splice($this->blocks, $index, 1); |
| 60 | + return $variable; |
| 61 | + } |
65 | 62 |
|
66 |
| - $index = array_search($variable, $this->blocks); |
67 |
| - array_splice($this->blocks, $index, 1); |
68 |
| - return $variable; |
69 |
| - } |
| 63 | + public function findVariable(string $key): ?VariableBlock |
| 64 | + { |
| 65 | + foreach ($this->getVariableBlocks() as $block) { |
| 66 | + if ($block->key == $key) { |
| 67 | + return $block; |
| 68 | + } |
| 69 | + } |
70 | 70 |
|
71 |
| - public function getValue(string $key): string { |
72 |
| - $variable = $this->findVariable($key); |
| 71 | + return null; |
| 72 | + } |
73 | 73 |
|
74 |
| - return $variable ? $variable->value->content : ""; |
75 |
| - } |
| 74 | + /** |
| 75 | + * @return VariableBlock[] |
| 76 | + */ |
| 77 | + public function getVariableBlocks(): array |
| 78 | + { |
| 79 | + return array_values(array_filter($this->blocks, function ($block) { |
| 80 | + return $block instanceof VariableBlock; |
| 81 | + })); |
| 82 | + } |
76 | 83 |
|
77 |
| - public function setValue(string $key, string $content): void |
78 |
| - { |
79 |
| - $variable = $this->findVariable($key); |
80 |
| - if(!$variable) { |
81 |
| - $variable = new VariableBlock(); |
82 |
| - $variable->key->content = $key; |
| 84 | + public function getValue(string $key): string |
| 85 | + { |
| 86 | + $variable = $this->findVariable($key); |
83 | 87 |
|
84 |
| - $this->blocks[] = $variable; |
| 88 | + return $variable ? $variable->value->content : ""; |
85 | 89 | }
|
86 | 90 |
|
87 |
| - $variable->value->setContent($content); |
88 |
| - } |
| 91 | + // Utility methods |
89 | 92 |
|
90 |
| - // Utility methods |
| 93 | + public function setValue(string $key, string $content): void |
| 94 | + { |
| 95 | + $variable = $this->findVariable($key); |
| 96 | + if (!$variable) { |
| 97 | + $variable = new VariableBlock(); |
| 98 | + $variable->key->content = $key; |
91 | 99 |
|
92 |
| - public static function loadFrom(string $path): EnvFile { |
93 |
| - $parser = new Parser(); |
94 |
| - $content = file_get_contents($path); |
| 100 | + $this->blocks[] = $variable; |
| 101 | + } |
95 | 102 |
|
96 |
| - return $parser->parse($content); |
97 |
| - } |
| 103 | + $variable->value->setContent($content); |
| 104 | + } |
98 | 105 |
|
99 |
| - public function saveTo(string $path): void { |
100 |
| - $composer = new Composer(); |
101 |
| - $content = $composer->compose($this); |
| 106 | + public function saveTo(string $path): void |
| 107 | + { |
| 108 | + $composer = new Composer(); |
| 109 | + $content = $composer->compose($this); |
102 | 110 |
|
103 |
| - file_put_contents($path, $content); |
104 |
| - } |
| 111 | + file_put_contents($path, $content); |
| 112 | + } |
105 | 113 |
|
106 |
| - // Private methods |
| 114 | + // Private methods |
107 | 115 |
|
108 | 116 |
|
109 | 117 | }
|
0 commit comments