1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheAentMachine\YamlTools; |
4
|
|
|
|
5
|
|
|
use Safe\Exceptions\FilesystemException; |
6
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
9
|
|
|
use TheAentMachine\Yaml\Dumper; |
10
|
|
|
use function Safe\chown; |
11
|
|
|
use function Safe\chgrp; |
12
|
|
|
|
13
|
|
|
final class YamlTools |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Merge the content of $sourceFile into $destinationFile's one (overwritten) |
17
|
|
|
* @param string $destinationFile |
18
|
|
|
* @param string $sourceFile |
19
|
|
|
*/ |
20
|
|
|
public static function mergeTwoFiles(string $destinationFile, string $sourceFile): void |
21
|
|
|
{ |
22
|
|
|
$files = [$destinationFile, $sourceFile]; |
23
|
|
|
self::mergeSuccessive($files, $destinationFile); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Given an array of yaml file pathnames, merge them from the last to the first |
28
|
|
|
* @param mixed[] $yamlFilePathnames |
29
|
|
|
* @param null|string $outputFile if null, dump the result to stdout |
30
|
|
|
*/ |
31
|
|
|
public static function mergeSuccessive(array $yamlFilePathnames, ?string $outputFile = null): void |
32
|
|
|
{ |
33
|
|
|
$command = array('yaml-tools', 'merge', '-i'); |
34
|
|
|
$command = array_merge($command, $yamlFilePathnames); |
35
|
|
|
if (null !== $outputFile) { |
36
|
|
|
$command[] = '-o'; |
37
|
|
|
$command[] = $outputFile; |
38
|
|
|
} |
39
|
|
|
$process = new Process($command); |
40
|
|
|
$process->enableOutput(); |
41
|
|
|
$process->setTty(true); |
42
|
|
|
$process->mustRun(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Merge yaml content into one file (created if non existent, with its directory's owner and group code) |
47
|
|
|
* @param string|mixed[] $content |
48
|
|
|
* @param string $file |
49
|
|
|
* @throws FilesystemException |
50
|
|
|
*/ |
51
|
|
|
public static function mergeContentIntoFile($content, string $file): void |
52
|
|
|
{ |
53
|
|
|
if (\is_array($content)) { |
54
|
|
|
$content = self::dump($content); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$fileSystem = new Filesystem(); |
58
|
|
|
|
59
|
|
|
if ($fileSystem->exists($file)) { |
60
|
|
|
$tmpFile = $fileSystem->tempnam(sys_get_temp_dir(), 'yaml-tools-merge-'); |
61
|
|
|
$fileSystem->dumpFile($tmpFile, $content); |
62
|
|
|
self::mergeTwoFiles($file, $tmpFile); |
63
|
|
|
$fileSystem->remove($tmpFile); |
64
|
|
|
} else { |
65
|
|
|
$fileSystem->dumpFile($file, $content); |
66
|
|
|
$dirInfo = new \SplFileInfo(\dirname($file)); |
67
|
|
|
chown($file, $dirInfo->getOwner()); |
68
|
|
|
chgrp($file, $dirInfo->getGroup()); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* 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) |
74
|
|
|
* Caution : this also deletes its preceding comments |
75
|
|
|
* @param string[] $pathToItem e.g. key1 key2 0 key3 |
76
|
|
|
* @param string $file |
77
|
|
|
*/ |
78
|
|
|
public static function deleteYamlItem(array $pathToItem, string $file): void |
79
|
|
|
{ |
80
|
|
|
$command = array('yaml-tools', 'delete'); |
81
|
|
|
$command = array_merge($command, $pathToItem, [ |
82
|
|
|
'-i', $file, |
83
|
|
|
'-o', $file, |
84
|
|
|
]); |
85
|
|
|
$process = new Process($command); |
86
|
|
|
$process->enableOutput(); |
87
|
|
|
$process->setTty(true); |
88
|
|
|
$process->mustRun(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* See https://github.com/thecodingmachine/yaml-tools#normalize-docker-compose |
93
|
|
|
* @param string $inputFile |
94
|
|
|
* @param string|null $outputFile |
95
|
|
|
*/ |
96
|
|
|
public static function normalizeDockerCompose(string $inputFile, ?string $outputFile = null): void |
97
|
|
|
{ |
98
|
|
|
$command = array('yaml-tools', 'normalize-docker-compose', '-i', $inputFile); |
99
|
|
|
if (null !== $outputFile) { |
100
|
|
|
$command[] = '-o'; |
101
|
|
|
$command[] = $outputFile; |
102
|
|
|
} |
103
|
|
|
$process = new Process($command); |
104
|
|
|
$process->enableOutput(); |
105
|
|
|
$process->setTty(true); |
106
|
|
|
$process->mustRun(); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Dumps $item in YAML. |
111
|
|
|
* |
112
|
|
|
* @param mixed $item |
113
|
|
|
* @return string |
114
|
|
|
*/ |
115
|
|
|
public static function dump($item): string |
116
|
|
|
{ |
117
|
|
|
$yaml = new Dumper(2); |
118
|
|
|
return $yaml->dump($item, 256, 0, Yaml::DUMP_OBJECT_AS_MAP); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|