Last active
March 21, 2018 16:00
-
-
Save phcorp/6e3f0af765f23bb88913bf3cda330d74 to your computer and use it in GitHub Desktop.
PHP merge xml files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env php | |
<?php | |
function println($text, $foregroundColor = NULL, $backgroundColor = NULL) { | |
$colors = [ | |
'background' => [ | |
'black' => '40', | |
'blue' => '44', | |
'cyan' => '46', | |
'green' => '42', | |
'lightGray' => '47', | |
'magenta' => '45', | |
'red' => '41', | |
'yellow' => '43', | |
], | |
'foreground' => [ | |
'black' => '0;30', | |
'blue' => '0;34', | |
'brown' => '0;33', | |
'cyan' => '0;36', | |
'darkGray' => '1;30', | |
'green' => '0;32', | |
'lightBlue' => '1;34', | |
'lightCyan' => '1;36', | |
'lightGray' => '0;37 ', | |
'lightGreen' => '1;32', | |
'lightPurple' => '1;35', | |
'lightRed' => '1;31', | |
'purple' => '0;35', | |
'red' => '0;31', | |
'white' => '1;37', | |
'yellow' => '1;33', | |
], | |
]; | |
echo sprintf( | |
"%s%s%s%s\n", | |
isset($colors['foreground'][$foregroundColor]) ? sprintf("\033[%sm", $colors['foreground'][$foregroundColor]) : '', | |
isset($colors['background'][$backgroundColor]) ? sprintf("\033[%sm", $colors['background'][$backgroundColor]) : '', | |
$text, | |
NULL === $foregroundColor || NULL === $backgroundColor ? "\033[0m" : '' | |
); | |
} | |
// Display help. | |
if (isset($argv[1]) && $argv[1] === '--help') { | |
println( | |
<<<HELP | |
Mandatory arguments: | |
1) An xml destination file | |
2) At least one xml source file | |
Optional arguments that may be put after the mandatory arguments: | |
- Other xml source files | |
HELP | |
); | |
exit(0); | |
} | |
// Get files. | |
$files = array_values(array_filter($argv, function ($arg) { | |
return '.xml' === substr($arg, -4); | |
})); | |
// Invalid files number. | |
if (count($files) < 2) { | |
println('Invalid files number, at least 2 is needed.', 'red'); | |
exit(1); | |
} | |
// Get destination. | |
$destination = array_shift($files); | |
// Merge files. | |
$root = NULL; | |
foreach ($files as $file) { | |
$document = new DOMDocument(); | |
$document->load($file); | |
if (NULL === $root) { | |
$root = $document; | |
continue; | |
} | |
foreach ($document->firstChild->childNodes as $childNode) { | |
$root->firstChild->appendChild($root->importNode($childNode, TRUE)); | |
} | |
} | |
$root->save($destination); | |
// Display result. | |
$limit = count($files) - 2; | |
foreach ($files as $index => $file) { | |
if ($index > $limit) { | |
break; | |
} | |
$suffix = $index < $limit ? ', ' : ' and '; | |
$files[$index] = "$file$suffix"; | |
} | |
println(sprintf( | |
'XML aggregating %s was saved to %s.', | |
implode($files), | |
$destination | |
), 'green'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment