1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace PmgDev\DatabaseReplicator; |
4
|
|
|
|
5
|
|
|
use Nette\Utils\FileSystem; |
6
|
|
|
use PmgDev\DatabaseReplicator\Source; |
7
|
|
|
use PmgDev\DatabaseReplicator\Source\Files; |
8
|
|
|
|
9
|
|
|
abstract class Builder |
10
|
|
|
{ |
11
|
|
|
/** @var string */ |
12
|
|
|
private $sourceFile; |
13
|
|
|
|
14
|
|
|
/** @var Config */ |
15
|
|
|
private $config; |
16
|
|
|
|
17
|
|
|
/** @var Command */ |
18
|
|
|
private $command; |
19
|
|
|
|
20
|
|
|
/** @var string */ |
21
|
|
|
private $tempDir = '/tmp'; |
22
|
|
|
|
23
|
|
|
|
24
|
|
|
public function __construct(string $sourceFile, Config $config, Command $command) |
25
|
|
|
{ |
26
|
|
|
$this->sourceFile = $sourceFile; |
27
|
|
|
$this->config = $config; |
28
|
|
|
$this->command = $command; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
|
32
|
|
|
final public function setTempDir(string $tempDir): void |
33
|
|
|
{ |
34
|
|
|
FileSystem::createDir($tempDir); |
35
|
|
|
$this->tempDir = $tempDir; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
|
39
|
|
|
abstract public function createDatabase(): DatabaseConnection; |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
final protected function createDatabaseReplicator( |
43
|
|
|
?Files $files = NULL, |
44
|
|
|
?Files $dynamicFiles = NULL |
45
|
|
|
): Database\Replicator |
46
|
|
|
{ |
47
|
|
|
$sourceHash = $this->createSourceHash($files); |
48
|
|
|
$databasePrefix = $this->createDatabasePrefix($sourceHash); |
49
|
|
|
$sourceDatabase = $this->createSourceDatabase($databasePrefix, $sourceHash); |
50
|
|
|
return new Database\Replicator($this->command, $databasePrefix, $sourceDatabase, $dynamicFiles); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
|
54
|
|
|
final protected function createSourceHash(?Files $files = NULL): Source\Hash |
55
|
|
|
{ |
56
|
|
|
if ($files === NULL) { |
57
|
|
|
$files = $this->createSourceFiles(); |
58
|
|
|
} |
59
|
|
|
return new Source\Hash($this->config->database, $this->tempDir, $files); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
|
63
|
|
|
final protected function createSourceFiles() |
64
|
|
|
{ |
65
|
|
|
return new Files([$this->sourceFile]); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
|
|
final protected function createDatabasePrefix(Source\Hash $sourceHash): Database\Prefix |
70
|
|
|
{ |
71
|
|
|
return new Database\Prefix($this->getConfig(), $sourceHash); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
final protected function getConfig(): Config |
76
|
|
|
{ |
77
|
|
|
return clone $this->config; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
|
81
|
|
|
final protected function createSourceDatabase( |
82
|
|
|
Database\Prefix $prefix, |
83
|
|
|
Source\Hash $sourceHash |
84
|
|
|
): Source\Database |
85
|
|
|
{ |
86
|
|
|
return new Source\Database($prefix, $sourceHash, $this->command); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
|
90
|
|
|
final protected function getCommand(): Command |
91
|
|
|
{ |
92
|
|
|
return $this->command; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
} |
96
|
|
|
|