Skip to content

Commit ee2fca9

Browse files
committed
use 'command' design pattern
1 parent 0b975e8 commit ee2fca9

File tree

7 files changed

+114
-33
lines changed

7 files changed

+114
-33
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
/vendor/
2+
/sandbox/

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
},
1616
"files": [
1717
"vendor/docopt/docopt/src/docopt.php",
18-
"src/Gendiff.php"
18+
"src/Gendiff.php",
19+
"src/CommandInterface.php",
20+
"src/Command.php",
21+
"src/OutputInterface.php"
1922
]
2023
},
2124
"authors": [

src/Command.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\CommandInterface;
6+
use App\OutputInterface;
7+
8+
class Command implements CommandInterface
9+
{
10+
protected $output;
11+
protected $docopt;
12+
protected $cliData;
13+
private const MAX_FILE_SIZE = 4096;
14+
15+
public function __construct(OutputInterface $output, string $doc)
16+
{
17+
$this->output = $output;
18+
$this->docopt = $doc;
19+
$this->cliData = $this->output->parseCommandData($this->docopt);
20+
}
21+
22+
private function parseFile(string $filename): array
23+
{
24+
$handle = fopen($filename, "r");
25+
$result = json_decode(fread($handle, self::MAX_FILE_SIZE));
26+
fclose($handle);
27+
$type = gettype($result);
28+
if ($type === 'object') {
29+
$keys = (get_object_vars($result));
30+
} elseif ($type === 'array') {
31+
$keys = array_keys($result);
32+
} else {
33+
$keys = null;
34+
}
35+
36+
return $keys;
37+
}
38+
39+
public function execute()
40+
{
41+
if (isset($this->cliData['FILE1']) & isset($this->cliData['FILE2'])) {
42+
if (file_exists($this->cliData['FILE1']) & file_exists($this->cliData['FILE2'])) {
43+
$file1Content = $this->parseFile($this->cliData['FILE1']);
44+
$file2Content = $this->parseFile($this->cliData['FILE2']);
45+
46+
print_r("File1 content:\n");
47+
print_r($file1Content);
48+
print_r("File2 content:\n");
49+
print_r($file2Content);
50+
} else {
51+
print_r("File {$this->cliData['FILE2']} not exists\n");
52+
}
53+
} else {
54+
print_r("File {$this->cliData['FILE1']} not exists\n");
55+
}
56+
}
57+
}

src/CommandInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App;
4+
5+
interface CommandInterface
6+
{
7+
public function execute();
8+
}

src/Gendiff.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,14 @@
33
namespace App;
44

55
use Docopt;
6-
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-
}
6+
use App\OutputInterface;
7+
use App\CommandInterface;
8+
use App\Invoker;
9+
use App\Command;
1710

1811
function runGendiff(): void
1912
{
13+
2014
$doc = <<<'DOCOPT'
2115
gendiff -h
2216
@@ -34,26 +28,16 @@ function runGendiff(): void
3428

3529
DOCOPT;
3630

37-
$cliData = Docopt::handle($doc, array('version' => '1.0.6'));
38-
/*
39-
foreach ($cliData as $k => $v) {
40-
print_r($k . ': ' . json_encode($v) . PHP_EOL);
41-
}
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");
31+
$output = new class implements OutputInterface
32+
{
33+
public function parseCommandData(string $docopt): object
34+
{
35+
return Docopt::handle($docopt, array('version' => '1.0.6'));
5436
}
55-
} else {
56-
print_r("File {$cliData['FILE1']} not exists\n");
57-
runGendiff();
58-
}
37+
};
38+
39+
$command = new Command($output, $doc);
40+
$invoker = new Invoker();
41+
$invoker->setCommand($command);
42+
$invoker->run();
5943
}

src/Invoker.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace App;
4+
5+
use App\CommandInterface;
6+
7+
class Invoker
8+
{
9+
private $command;
10+
11+
public function setCommand(CommandInterface $command)
12+
{
13+
$this->command = $command;
14+
}
15+
16+
public function run()
17+
{
18+
$this->command->execute();
19+
}
20+
}

src/OutputInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace App;
4+
5+
interface OutputInterface
6+
{
7+
public function parseCommandData(string $docopt): object;
8+
}

0 commit comments

Comments
 (0)