Passed
Pull Request — master (#17)
by Jindun
02:14
created

YamlTools   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 65
rs 10
c 0
b 0
f 0
wmc 5

4 Methods

Rating   Name   Duplication   Size   Complexity  
A mergeSuccessive() 0 12 2
A deleteYamlItem() 0 11 1
A mergeContentIntoFile() 0 7 1
A mergeTwoFiles() 0 4 1
1
<?php
2
3
namespace TheAentMachine\AentDockerCompose\YamlTools;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Process\Process;
7
8
class YamlTools
9
{
10
11
    /**
12
     * Merge the content of $sourceFile into $destinationFile's one (overwritten)
13
     * @param string $destinationFile
14
     * @param string $sourceFile
15
     */
16
    public static function mergeTwoFiles(string $destinationFile, string $sourceFile): void
17
    {
18
        $files = [$destinationFile, $sourceFile];
19
        self::mergeSuccessive($files, $destinationFile);
20
    }
21
22
    /**
23
     * Given an array of yaml file pathnames, merge them from the last to the first
24
     * @param mixed[] $yamlFilePathnames
25
     * @param null|string $outputFile if null, dump the result to stdout
26
     */
27
    public static function mergeSuccessive(array $yamlFilePathnames, ?string $outputFile = null): void
28
    {
29
        $command = array('yaml-tools', 'merge', '-i');
30
        $command = array_merge($command, $yamlFilePathnames);
31
        if (null !== $outputFile) {
32
            $command[] = '-o';
33
            $command[] = $outputFile;
34
        }
35
        $process = new Process($command);
36
        $process->enableOutput();
37
        $process->setTty(true);
38
        $process->mustRun();
39
    }
40
41
    /**
42
     * Merge yaml content into one file
43
     * @param string $content
44
     * @param string $file
45
     * @throws \Exception
46
     */
47
    public static function mergeContentIntoFile(string $content, string $file): void
48
    {
49
        $tmpFile = __DIR__ . '/tmp-yaml-tools-file.yml';
50
        $fileSystem = new Filesystem();
51
        $fileSystem->dumpFile($tmpFile, $content);
52
        self::mergeTwoFiles($file, $tmpFile);
53
        $fileSystem->remove($tmpFile);
54
    }
55
56
    /**
57
     * Delete one yaml item given its path (e.g. key1 key2 0 key3) in the $inputFile, then write it into $outputFile (or stdout if empty)
58
     * Caution : this also deletes its preceding comments
59
     * @param string[] $pathToItem e.g. key1 key2 0 key3
60
     * @param string $file
61
     */
62
    public static function deleteYamlItem(array $pathToItem, string $file): void
63
    {
64
        $command = array('yaml-tools', 'delete');
65
        $command = array_merge($command, $pathToItem, [
66
            '-i', $file,
67
            '-o', $file,
68
        ]);
69
        $process = new Process($command);
70
        $process->enableOutput();
71
        $process->setTty(true);
72
        $process->mustRun();
73
    }
74
}
75