Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c9596ec

Browse files
committed
Enable compare_yaml_hashmaps.php
1 parent bba3990 commit c9596ec

File tree

5 files changed

+181
-2
lines changed

5 files changed

+181
-2
lines changed

‎README.md‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,12 @@ find_binary
5353
```bash
5454
php find_binary.php <integer>
5555
```
56-
Decompose an integer into powers of two
56+
Decompose an integer into powers of two
57+
58+
compare_hashmaps
59+
----------------
60+
61+
```bash
62+
php compare_yaml_hashmaps.php file1 file2
63+
```
64+
Compare 2 YAML hashmap files (such as symfony parameters.yml files)

‎Util/TextColorWriter.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static function textColor($string, $colorID)
4242
*
4343
* @return array
4444
*/
45-
private function getKnownColors()
45+
private staticfunction getKnownColors()
4646
{
4747
$colors = array(
4848
static::BASH_PROMPT_BLACK,

‎compare_yaml_hashmaps.php‎

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
require_once __DIR__.'/vendor/autoload.php';
4+
5+
use Symfony\Component\Yaml\Yaml;
6+
7+
/**
8+
* Validate console input
9+
*
10+
* Die if input is not valid
11+
*
12+
* @param array $argv user inputs
13+
*/
14+
function validateInput($argv)
15+
{
16+
if (count($argv) !== 4) {
17+
echo 'php compare_hashmaps <file1> <file2> <root_node>' . PHP_EOL;
18+
die(1);
19+
}
20+
21+
if (!file_exists($argv[1])) {
22+
echo "Error: " . $argv[1] . " is not a valid file" . PHP_EOL;
23+
die(1);
24+
}
25+
if (!file_exists($argv[2])) {
26+
echo "Error: " . $argv[2] . " is not a valid file" . PHP_EOL;
27+
die(1);
28+
}
29+
}
30+
31+
/**
32+
* @param string $filepath
33+
* @param string $rootNode
34+
*
35+
* @return array
36+
* @throws Exception
37+
*/
38+
function extractArrayFromFile($filepath, $rootNode)
39+
{
40+
$value = Yaml::parse(file_get_contents($filepath));
41+
42+
if (!array_key_exists($rootNode, $value)) {
43+
$showKeys = implode(', ', array_keys($value));
44+
throw new \RuntimeException("Expects root node '$rootNode'' in file $filepath, got: $showKeys");
45+
}
46+
47+
return $value[$rootNode];
48+
}
49+
50+
validateInput($argv);
51+
52+
require 'Util/TextColorWriter.php';
53+
54+
$filepath1 = $argv[1];
55+
$filepath2 = $argv[2];
56+
$rootNode = $argv[3];
57+
58+
$list1 = extractArrayFromFile(realpath($filepath1), $rootNode);
59+
$list2 = extractArrayFromFile(realpath($filepath2), $rootNode);
60+
61+
$inFile1ButNotInFile2 = [];
62+
$inFile2ButNotInFile1 = [];
63+
64+
foreach ($list1 as $key => $value) {
65+
if (!array_key_exists($key, $list2)) {
66+
$inFile1ButNotInFile2[$key] = $value;
67+
}
68+
}
69+
foreach ($list2 as $key => $value) {
70+
if (!array_key_exists($key, $list1)) {
71+
$inFile2ButNotInFile1[$key] = $value;
72+
}
73+
}
74+
75+
76+
// echo result
77+
echo TextColorWriter::textColor('Done', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
78+
echo TextColorWriter::textColor('Found in file 1 but not in file 2:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
79+
80+
foreach ($inFile1ButNotInFile2 as $key => $value) {
81+
echo ' - ' . $key . PHP_EOL;
82+
}
83+
84+
echo TextColorWriter::textColor('Found in file 2 but not in file 1:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
85+
86+
foreach ($inFile2ButNotInFile1 as $key => $value) {
87+
echo ' - ' . $key . PHP_EOL;
88+
}

‎composer.json‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"name": "matks/php_scripts",
3+
"description": "Useful php scripts",
4+
"type": "library",
5+
"require": {
6+
"symfony/yaml": "^3.2"
7+
},
8+
"license": "MIT"
9+
}

‎composer.lock‎

Lines changed: 74 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /