Skip to content

Commit 0b975e8

Browse files
committed
added files parsing and print content
1 parent 6d2879a commit 0b975e8

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

file1.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"host": "hexlet.io",
3+
"timeout": 50,
4+
"proxy": "123.234.53.22",
5+
"follow": false
6+
}

file2.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"timeout": 20,
3+
"verbose": true,
4+
"host": "hexlet.io"
5+
}

src/Gendiff.php

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,18 @@
44

55
use Docopt;
66

7-
function runGendiff()
7+
function parseFile(string $filename): array
8+
{
9+
$handle = fopen($filename, "r");
10+
$result = json_decode(fread($handle, 4096));
11+
fclose($handle);
12+
13+
$keys = (get_object_vars($result));
14+
15+
return $keys;
16+
}
17+
18+
function runGendiff(): void
819
{
920
$doc = <<<'DOCOPT'
1021
gendiff -h
@@ -14,6 +25,7 @@ function runGendiff()
1425
Usage:
1526
gendiff (-h|--help)
1627
gendiff (-v|--version)
28+
gendiff [Options]... FILE1 FILE2
1729
1830
Options:
1931
-h --help Show this screen
@@ -22,9 +34,26 @@ function runGendiff()
2234

2335
DOCOPT;
2436

25-
$result = Docopt::handle($doc, array('version' => '1.0.6'));
26-
27-
foreach ($result as $k => $v) {
37+
$cliData = Docopt::handle($doc, array('version' => '1.0.6'));
38+
/*
39+
foreach ($cliData as $k => $v) {
2840
print_r($k . ': ' . json_encode($v) . PHP_EOL);
2941
}
42+
*/
43+
if (file_exists($cliData['FILE1'])) {
44+
if (file_exists($cliData['FILE2'])) {
45+
$file1Content = parseFile($cliData['FILE1']);
46+
$file2Content = parseFile($cliData['FILE2']);
47+
48+
print_r("File1 content:\n");
49+
print_r($file1Content);
50+
print_r("File2 content:\n");
51+
print_r($file2Content);
52+
} else {
53+
print_r("File {$cliData['FILE2']} not exists");
54+
}
55+
} else {
56+
print_r("File {$cliData['FILE1']} not exists\n");
57+
runGendiff();
58+
}
3059
}

0 commit comments

Comments
 (0)