Conditions | 4 |
Paths | 6 |
Total Lines | 27 |
Code Lines | 19 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
22 | public function set(string $key, string $value, string $comment = null): void |
||
23 | { |
||
24 | $content = $this->getContent(); |
||
25 | if ($this->has($key)) { |
||
26 | // Note: if the key is already in the file, comments are not modified. |
||
27 | $content = \preg_replace("/^$key=.*/m", $key.'='.$value, $content); |
||
28 | } else { |
||
29 | $commentLines = \explode("\n", $comment ?? ''); |
||
30 | $commentLines = \array_map(function (string $line) { |
||
31 | return '# '.$line; |
||
32 | }, $commentLines); |
||
33 | $comments = \implode("\n", $commentLines); |
||
34 | if ($comment) { |
||
35 | $content .= <<<ENVVAR |
||
36 | $comments |
||
37 | |||
38 | ENVVAR; |
||
39 | } |
||
40 | $content .= <<<ENVVAR |
||
41 | $key=$value |
||
42 | |||
43 | ENVVAR; |
||
44 | } |
||
45 | |||
46 | $return = \file_put_contents($this->filePath, $content); |
||
47 | if ($return === false) { |
||
48 | throw new \RuntimeException('Unable to write file '.$this->filePath); |
||
49 | } |
||
70 |