diff --git a/README.md b/README.md index 7fd5d13..c2c8991 100644 --- a/README.md +++ b/README.md @@ -53,4 +53,12 @@ find_binary ```bash php find_binary.php ``` -Decompose an integer into powers of two \ No newline at end of file +Decompose an integer into powers of two + +compare_hashmaps +---------------- + +```bash +php compare_yaml_hashmaps.php file1 file2 +``` +Compare 2 YAML hashmap files (such as symfony parameters.yml files) \ No newline at end of file diff --git a/Util/TextColorWriter.php b/Util/TextColorWriter.php index 529c142..7c30a0a 100644 --- a/Util/TextColorWriter.php +++ b/Util/TextColorWriter.php @@ -42,7 +42,7 @@ public static function textColor($string, $colorID) * * @return array */ - private function getKnownColors() + private static function getKnownColors() { $colors = array( static::BASH_PROMPT_BLACK, diff --git a/compare_yaml_hashmaps.php b/compare_yaml_hashmaps.php new file mode 100644 index 0000000..c539b3a --- /dev/null +++ b/compare_yaml_hashmaps.php @@ -0,0 +1,88 @@ + ' . PHP_EOL; + die(1); + } + + if (!file_exists($argv[1])) { + echo "Error: " . $argv[1] . " is not a valid file" . PHP_EOL; + die(1); + } + if (!file_exists($argv[2])) { + echo "Error: " . $argv[2] . " is not a valid file" . PHP_EOL; + die(1); + } +} + +/** + * @param string $filepath + * @param string $rootNode + * + * @return array + * @throws Exception + */ +function extractArrayFromFile($filepath, $rootNode) +{ + $value = Yaml::parse(file_get_contents($filepath)); + + if (!array_key_exists($rootNode, $value)) { + $showKeys = implode(', ', array_keys($value)); + throw new \RuntimeException("Expects root node '$rootNode'' in file $filepath, got: $showKeys"); + } + + return $value[$rootNode]; +} + +validateInput($argv); + +require 'Util/TextColorWriter.php'; + +$filepath1 = $argv[1]; +$filepath2 = $argv[2]; +$rootNode = $argv[3]; + +$list1 = extractArrayFromFile(realpath($filepath1), $rootNode); +$list2 = extractArrayFromFile(realpath($filepath2), $rootNode); + +$inFile1ButNotInFile2 = []; +$inFile2ButNotInFile1 = []; + +foreach ($list1 as $key => $value) { + if (!array_key_exists($key, $list2)) { + $inFile1ButNotInFile2[$key] = $value; + } +} +foreach ($list2 as $key => $value) { + if (!array_key_exists($key, $list1)) { + $inFile2ButNotInFile1[$key] = $value; + } +} + + +// echo result +echo TextColorWriter::textColor('Done', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; +echo TextColorWriter::textColor('Found in file 1 but not in file 2:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; + +foreach ($inFile1ButNotInFile2 as $key => $value) { + echo ' - ' . $key . PHP_EOL; +} + +echo TextColorWriter::textColor('Found in file 2 but not in file 1:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL; + +foreach ($inFile2ButNotInFile1 as $key => $value) { + echo ' - ' . $key . PHP_EOL; +} diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..010ac70 --- /dev/null +++ b/composer.json @@ -0,0 +1,9 @@ +{ + "name": "matks/php_scripts", + "description": "Useful php scripts", + "type": "library", + "require": { + "symfony/yaml": "^3.2" + }, + "license": "MIT" +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..4a28d03 --- /dev/null +++ b/composer.lock @@ -0,0 +1,74 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "25f22b21561a33adbe36c50e82567d9e", + "content-hash": "2ac310c063d671147381ec25a693d9b6", + "packages": [ + { + "name": "symfony/yaml", + "version": "v3.2.1", + "source": { + "type": "git", + "url": "https://github.com/symfony/yaml.git", + "reference": "a7095af4b97a0955f85c8989106c249fa649011f" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/symfony/yaml/zipball/a7095af4b97a0955f85c8989106c249fa649011f", + "reference": "a7095af4b97a0955f85c8989106c249fa649011f", + "shasum": "" + }, + "require": { + "php": ">=5.5.9" + }, + "require-dev": { + "symfony/console": "~2.8|~3.0" + }, + "suggest": { + "symfony/console": "For validating YAML files using the lint command" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.2-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\Yaml\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony Yaml Component", + "homepage": "https://symfony.com", + "time": "2016-12-10 10:07:06" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +}