Conditions | 4 |
Paths | 8 |
Total Lines | 19 |
Code Lines | 14 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | <?php |
||
20 | public function patch($file, $patch): void |
||
21 | { |
||
22 | if (!is_dir($dir = dirname($file))) { |
||
23 | mkdir($dir, 0755, true); |
||
24 | } |
||
25 | if (!is_file($file)) { |
||
26 | touch($file); |
||
27 | } |
||
28 | $prefix = '### > dotfiles-patch ###'; |
||
29 | $suffix = '### < dotfiles-patch ###'; |
||
30 | $patch = "\n${prefix}\n${patch}\n${suffix}\n"; |
||
31 | $regex = '/\\n'.$prefix.'.*'.$suffix.'\\n/is'; |
||
32 | |||
33 | $contents = file_get_contents($file); |
||
34 | if (preg_match($regex, $contents, $matches)) { |
||
35 | $contents = str_replace($matches[0], $patch, $contents); |
||
36 | $this->dumpFile($file, $contents); |
||
37 | } else { |
||
38 | $this->appendToFile($file, $patch); |
||
39 | } |
||
42 |