Completed
Push — master ( 8ffbc5...79a56c )
by David
05:07 queued 02:19
created

YamlTools::dump()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace TheAentMachine\YamlTools;
4
5
use Symfony\Component\Filesystem\Filesystem;
6
use Symfony\Component\Process\Process;
7
use Symfony\Component\Yaml\Yaml;
8
use TheAentMachine\Yaml\Dumper;
9
10
final class YamlTools
11
{
12
    /**
13
     * Merge the content of $sourceFile into $destinationFile's one (overwritten)
14
     * @param string $destinationFile
15
     * @param string $sourceFile
16
     */
17
    public static function mergeTwoFiles(string $destinationFile, string $sourceFile): void
18
    {
19
        $files = [$destinationFile, $sourceFile];
20
        self::mergeSuccessive($files, $destinationFile);
21
    }
22
23
    /**
24
     * Given an array of yaml file pathnames, merge them from the last to the first
25
     * @param mixed[] $yamlFilePathnames
26
     * @param null|string $outputFile if null, dump the result to stdout
27
     */
28
    public static function mergeSuccessive(array $yamlFilePathnames, ?string $outputFile = null): void
29
    {
30
        $command = array('yaml-tools', 'merge', '-i');
31
        $command = array_merge($command, $yamlFilePathnames);
32
        if (null !== $outputFile) {
33
            $command[] = '-o';
34
            $command[] = $outputFile;
35
        }
36
        $process = new Process($command);
37
        $process->enableOutput();
38
        $process->setTty(true);
39
        $process->mustRun();
40
    }
41
42
    /**
43
     * Merge yaml content into one file
44
     * @param string $content
45
     * @param string $file
46
     */
47
    public static function mergeContentIntoFile(string $content, string $file): void
48
    {
49
        $fileSystem = new Filesystem();
50
        $tmpFile = $fileSystem->tempnam(sys_get_temp_dir(), 'yaml-tools-merge-');
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
    /**
76
     * See https://github.com/thecodingmachine/yaml-tools#normalize-docker-compose
77
     * @param string $inputFile
78
     * @param string|null $outputFile
79
     */
80
    public static function normalizeDockerCompose(string $inputFile, ?string $outputFile = null): void
81
    {
82
        $command = array('yaml-tools', 'normalize-docker-compose', '-i', $inputFile);
83
        if (null !== $outputFile) {
84
            $command[] = '-o';
85
            $command[] = $outputFile;
86
        }
87
        $process = new Process($command);
88
        $process->enableOutput();
89
        $process->setTty(true);
90
        $process->mustRun();
91
    }
92
93
    /**
94
     * Dumps $item in YAML.
95
     *
96
     * @param mixed $item
97
     * @return string
98
     */
99
    public function dump($item): string
100
    {
101
        $yaml = new Dumper(2);
102
        return $yaml->dump($item, 256, 0, Yaml::DUMP_OBJECT_AS_MAP);
103
    }
104
}
105