Skip to content

Commit 5e94d25

Browse files
committed
update architecture
1 parent f505ec7 commit 5e94d25

19 files changed

+137
-158
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"type": "project",
55
"require": {
66
"docopt/docopt": "^1.0.6",
7-
"symfony/yaml": "*"
7+
"symfony/yaml": "^7.3.2"
88
},
99
"require-dev": {
1010
"squizlabs/php_codesniffer": "^3.7",

composer.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docopt.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
gendiff -h
2+
3+
Generate diff
4+
5+
Usage:
6+
gendiff (-h|--help)
7+
gendiff (-v|--version)
8+
gendiff [Options]... FILE1 FILE2
9+
10+
Options:
11+
-h --help Show this screen
12+
-v --version Show version
13+
--format <fmt> Report format [default: stylish]

fixtures/docopt.txt

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

fixtures/filesDiffs.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
{
2-
hexlet.io
3-
- 50
4-
+ 20
5-
- 123.234.53.22
6-
-
2+
host: hexlet.io
3+
- timeout: 50
4+
+ timeout: 20
5+
- proxy: 123.234.53.22
6+
- follow: false
7+
+ verbose: true
78
}

src/CommandFactory.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,24 @@
44

55
class CommandFactory implements CommandFactoryInterface
66
{
7-
private string $docopt;
7+
private $parser;
8+
private $fileReader;
89

9-
public function __construct(string $docopt = '')
10+
public function __construct($parser, $fileReader)
1011
{
11-
$this->docopt = $docopt;
12+
$this->parser = $parser;
13+
$this->fileReader = $fileReader;
1214
}
1315

1416
public function getCommand(string $commandType): ?CommandInterface
1517
{
1618
switch ($commandType) {
1719
case "parse":
18-
$requestedCommand = new CommandLineParser($this->docopt);
20+
$requestedCommand = new CommandLineParser($this->parser);
1921
break;
2022
case "difference":
21-
$requestedCommand = new FilesDiffCommand();
23+
$requestedCommand = (new FilesDiffCommand())
24+
->setFileReader($this->fileReader);
2225
break;
2326
case "show":
2427
$requestedCommand = new DisplayCommand();

src/CommandInterface.php

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

55
interface CommandInterface
66
{
7-
public function execute(CommandInterface $data = null): CommandInterface;
7+
public function execute(CommandInterface $command = null): CommandInterface;
88
}

src/CommandLineParser.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@
22

33
namespace Differ;
44

5-
use Docopt;
6-
75
class CommandLineParser implements CommandInterface
86
{
9-
private $docopt;
7+
private string $parserDescriptor;
8+
private $parser;
109
private array $args;
1110

12-
public function __construct(string $docopt = "")
11+
public function __construct($parser = null)
1312
{
14-
$this->docopt = $docopt;
13+
$this->parser = $parser;
14+
15+
$filename = __DIR__ . "/../docopt.txt";
16+
$handler = @fopen($filename, 'r');
17+
$filesize = filesize($filename);
18+
$this->parserDescriptor = fread($handler, $filesize);
19+
fclose($handler);
1520
}
1621

1722
public function execute(CommandInterface $command = null): CommandInterface
1823
{
1924
if (is_null($command)) {
20-
$this->args = (new Docopt())->handle($this->docopt, array('version' => '1.0.6'))
21-
->args;
25+
$objArgs = $this->parser->handle($this->parserDescriptor, array('version' => '1.0.6'));
26+
$this->args = $objArgs->args;
2227
}
2328

2429
return $this;

src/ConsoleApp.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@ class ConsoleApp
1010
private FileReaderInterface $fileReader;
1111

1212
public function __construct(
13-
CommandFactoryInterface $commandFactory,
14-
FileReaderInterface $fileReader
13+
CommandFactoryInterface $commandFactory
1514
) {
1615
$this->commandFactory = $commandFactory;
17-
$this->fileReader = $fileReader;
1816
}
1917

2018
public function run(): void
@@ -25,7 +23,6 @@ public function run(): void
2523

2624
$this->currentCommand = $this->commandFactory->getCommand("difference");
2725
$this->nextCommand = $this->currentCommand
28-
->setFileReader($this->fileReader)
2926
->execute($this->nextCommand);
3027

3128
$this->currentCommand = $this->commandFactory->getCommand("show");

src/Differ.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
function genDiff(string $pathToFile1, string $pathToFile2)
1212
{
13-
$commandFactory = new CommandFactory();
13+
$commandFactory = new CommandFactory(
14+
new \Docopt(),
15+
new FileReader()
16+
);
1417
$parserCommand = $commandFactory->getCommand('parse');
1518
$fileNames = [
1619
"FILE1" => $pathToFile1,
@@ -19,8 +22,7 @@ function genDiff(string $pathToFile1, string $pathToFile2)
1922
$nextCommand = $parserCommand->setFileNames($fileNames);
2023

2124
$currentCommand = $commandFactory->getCommand('difference');
22-
$nextCommand = $currentCommand->setFileReader(new FileReader())
23-
->execute($nextCommand);
25+
$nextCommand = $currentCommand->execute($nextCommand);
2426

2527
$currentCommand = $commandFactory->getCommand('show');
2628
return $currentCommand->execute($nextCommand);

0 commit comments

Comments
 (0)