Skip to content

Commit 121c1a0

Browse files
committed
added console application interface
1 parent 41a8ba3 commit 121c1a0

File tree

8 files changed

+82
-57
lines changed

8 files changed

+82
-57
lines changed

src/CommandFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ public function __construct(string $docopt = '')
1111
$this->docopt = $docopt;
1212
}
1313

14-
public function getCommand(string $commandType): CommandInterface | OutputInterface | null
14+
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface | null
1515
{
1616
if (!strcmp($commandType, "parse")) {
17-
return new Output($this->docopt);
17+
return new CommandLineParser($this->docopt);
1818
} elseif (!strcmp($commandType, "difference")) {
1919
return new FilesDiffCommand();
2020
} elseif (!strcmp($commandType, "show")) {

src/CommandFactoryInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
interface CommandFactoryInterface
66
{
7-
public function getCommand(string $commandType): CommandInterface | OutputInterface | null;
7+
public function getCommand(string $commandType): CommandInterface | CommandLineParserInterface | null;
88
}

src/Output.php renamed to src/CommandLineParser.php

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

55
use Docopt;
66

7-
class Output implements OutputInterface
7+
class CommandLineParser implements CommandLineParserInterface
88
{
99
private string $docopt;
1010

@@ -13,7 +13,7 @@ public function __construct(string $docopt)
1313
$this->docopt = $docopt;
1414
}
1515

16-
public function parseCommandData(): object
16+
public function execute(): object | null
1717
{
1818
return (new Docopt())->handle($this->docopt, array('version' => '1.0.6'));
1919
}

src/CommandLineParserInterface.php

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

src/ConsoleApp.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<?php
2+
3+
namespace Differ;
4+
5+
class ConsoleApp
6+
{
7+
private string $docopt;
8+
private CommandFactoryInterface $commandFactory;
9+
private CommandLineParserInterface $commandLineParser;
10+
private CommandInterface $filesDiffCommand;
11+
private CommandInterface $displayCommand;
12+
private object $cliData;
13+
private object $filesContent;
14+
15+
public function __construct()
16+
{
17+
$this->docopt = <<<'DOCOPT'
18+
gendiff -h
19+
20+
Generate diff
21+
22+
Usage:
23+
gendiff (-h|--help)
24+
gendiff (-v|--version)
25+
gendiff [Options]... FILE1 FILE2
26+
27+
Options:
28+
-h --help Show this screen
29+
-v --version Show version
30+
--format <fmt> Report format [default: stylish]
31+
32+
DOCOPT;
33+
34+
$this->commandFactory = new CommandFactory($this->docopt);
35+
}
36+
37+
public function run(): void
38+
{
39+
if ($this->commandLineParser = $this->commandFactory->getCommand("parse")) {
40+
$this->cliData = $this->commandLineParser->execute();
41+
} else {
42+
throw new \Exception("can't create command line parser");
43+
}
44+
45+
if ($this->filesDiffCommand = $this->commandFactory->getCommand("difference")) {
46+
$this->filesContent = $this->filesDiffCommand->setFileReader(new FileReader())
47+
->execute($this->cliData);
48+
} else {
49+
throw new \Exception("can't create files difference command");
50+
}
51+
52+
if ($this->displayCommand = $this->commandFactory->getCommand("show")) {
53+
$this->displayCommand->execute(/*(object)*/$this->filesContent)
54+
->showDiffsToConsole();
55+
} else {
56+
throw new \Exception("can't create display command");
57+
}
58+
}
59+
}

src/ConsoleAppInterface.php

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

src/Gendiff.php

Lines changed: 2 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,48 +4,6 @@
44

55
function runGendiff(): void
66
{
7-
8-
$docopt = <<<'DOCOPT'
9-
gendiff -h
10-
11-
Generate diff
12-
13-
Usage:
14-
gendiff (-h|--help)
15-
gendiff (-v|--version)
16-
gendiff [Options]... FILE1 FILE2
17-
18-
Options:
19-
-h --help Show this screen
20-
-v --version Show version
21-
--format <fmt> Report format [default: stylish]
22-
23-
DOCOPT;
24-
25-
$factory = new CommandFactory($docopt);
26-
27-
$outputParser = $factory->getCommand("parse");
28-
if (is_null($outputParser)) {
29-
print_r("error: can't create command to parse command data\n");
30-
} else {
31-
$cliData = $outputParser->parseCommandData();
32-
}
33-
34-
$filesDiffCommand = $factory->getCommand("difference");
35-
if (is_null($filesDiffCommand)) {
36-
print_r("error: can't create command to get files difference\n");
37-
exit;
38-
} else {
39-
$filesContent = $filesDiffCommand->setFileReader(new FileReader())
40-
->execute($cliData);
41-
}
42-
43-
$displayCommand = $factory->getCommand("show");
44-
if (is_null($displayCommand)) {
45-
print_r("error: can't create command to show differences to console\n");
46-
exit;
47-
} else {
48-
$displayCommand->execute((object)$filesContent)
49-
->showDiffsToConsole();
50-
}
7+
$consoleApp = new ConsoleApp();
8+
$consoleApp->run();
519
}

src/OutputInterface.php

Lines changed: 0 additions & 8 deletions
This file was deleted.

0 commit comments

Comments
 (0)