1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper; |
4
|
|
|
|
5
|
|
|
use CodexShaper\Dumper\Contracts\Dumper as DumperContract; |
6
|
|
|
use CodexShaper\Dumper\Traits\DumperTrait; |
7
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
abstract class Dumper implements DumperContract |
11
|
|
|
{ |
12
|
|
|
use DumperTrait; |
13
|
|
|
|
14
|
118 |
|
public function __construct(array $options = []) |
15
|
|
|
{ |
16
|
118 |
|
foreach ($options as $option => $value) { |
17
|
|
|
if (property_exists($this, $option)) { |
18
|
|
|
$this->{$option} = $value; |
19
|
|
|
} |
20
|
|
|
} |
21
|
118 |
|
} |
22
|
|
|
/** |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
118 |
|
public static function create(array $options = []) |
26
|
|
|
{ |
27
|
118 |
|
return new static($options); |
28
|
|
|
} |
29
|
|
|
/** |
30
|
|
|
* @return \Symfony\Component\Process\Process |
31
|
|
|
*/ |
32
|
|
|
protected function prepareProcessCommand() |
33
|
|
|
{ |
34
|
|
|
$process = Process::fromShellCommandline($this->command); |
35
|
|
|
$process->setTimeout($this->timeout); |
36
|
|
|
return $process; |
37
|
|
|
} |
38
|
|
|
/** |
39
|
|
|
* @return \Symfony\Component\Process\Process |
40
|
|
|
*/ |
41
|
|
|
protected function run() |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
|
45
|
|
|
$process = Process::fromShellCommandline($this->command); |
46
|
|
|
$process->setTimeout($this->timeout); |
47
|
|
|
|
48
|
|
|
if ($this->debug) { |
49
|
|
|
return $process->mustRun(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $process->run(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
} catch (ProcessFailedException $e) { |
55
|
|
|
throw new \Exception($e->getMessage()); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
abstract public function dump(); |
61
|
|
|
abstract public function restore(); |
62
|
|
|
|
63
|
58 |
|
public function prepareHost() |
64
|
|
|
{ |
65
|
58 |
|
switch (strtolower($this->getDumperClassName())) { |
66
|
58 |
|
case 'pgsqldumper': |
67
|
30 |
|
return ($this->socket !== '') ? $this->socket : $this->host; |
68
|
28 |
|
case 'mongodumper'; |
69
|
28 |
|
return !empty($this->host) ? "--host {$this->host}" : ""; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
73
|
58 |
|
public function preparePort() |
74
|
|
|
{ |
75
|
58 |
|
switch (strtolower($this->getDumperClassName())) { |
76
|
58 |
|
case 'pgsqldumper': |
77
|
30 |
|
return !empty($this->port) ? '-p ' . $this->port : ''; |
78
|
28 |
|
case 'mongodumper': |
79
|
28 |
|
return !empty($this->port) ? "--port {$this->port}" : ""; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
28 |
|
public function prepareSocket() |
84
|
|
|
{ |
85
|
28 |
|
switch (strtolower($this->getDumperClassName())) { |
86
|
28 |
|
case 'mysqldumper': |
87
|
28 |
|
return ($this->socket !== '') ? "--socket={$this->socket}" : ''; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
80 |
|
public function prepareDatabase() |
92
|
|
|
{ |
93
|
80 |
|
switch (strtolower($this->getDumperClassName())) { |
94
|
80 |
|
case 'mysqldumper': |
95
|
46 |
|
case 'pgsqldumper': |
96
|
64 |
|
return !empty($this->dbName) ? $this->dbName : ""; |
97
|
16 |
|
case 'mongodumper'; |
98
|
16 |
|
return !empty($this->dbName) ? "--db {$this->dbName}" : ""; |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
58 |
|
public function prepareUserName() |
103
|
|
|
{ |
104
|
58 |
|
switch (strtolower($this->getDumperClassName())) { |
105
|
58 |
|
case 'pgsqldumper': |
106
|
30 |
|
return !empty($this->username) ? $this->username : ""; |
107
|
28 |
|
case 'mongodumper'; |
108
|
28 |
|
return !empty($this->username) ? "--username {$this->username}" : ""; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
52 |
|
public function prepareIncludeTables() |
113
|
|
|
{ |
114
|
52 |
|
switch (strtolower($this->getDumperClassName())) { |
115
|
52 |
|
case 'mysqldumper': |
116
|
28 |
|
$includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : ''; |
117
|
28 |
|
return !empty($includeTables) ? "--tables {$includeTables}" : ''; |
118
|
24 |
|
case 'pgsqldumper': |
119
|
24 |
|
return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : ""; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
52 |
|
public function prepareIgnoreTables() |
124
|
|
|
{ |
125
|
52 |
|
switch (strtolower($this->getDumperClassName())) { |
126
|
52 |
|
case 'mysqldumper': |
127
|
28 |
|
$ignoreTablesArgs = []; |
128
|
28 |
|
foreach ($this->ignoreTables as $tableName) { |
129
|
4 |
|
$ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}"; |
130
|
|
|
} |
131
|
28 |
|
return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : ''; |
132
|
24 |
|
case 'pgsqldumper'; |
133
|
24 |
|
return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : ''; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
52 |
|
public function prepareCreateTables() |
138
|
|
|
{ |
139
|
52 |
|
switch (strtolower($this->getDumperClassName())) { |
140
|
52 |
|
case 'mysqldumper': |
141
|
28 |
|
return !$this->createTables ? '--no-create-info' : ''; |
142
|
24 |
|
case 'pgsqldumper': |
143
|
24 |
|
return (!$this->createTables) ? '--data-only' : ''; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
76 |
|
public function getDumpCommand(string $credentialFile = '', $destinationPath = '') |
148
|
|
|
{ |
149
|
76 |
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
150
|
76 |
|
switch (strtolower($this->getDumperClassName())) { |
151
|
76 |
|
case 'mysqldumper': |
152
|
28 |
|
$dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath); |
|
|
|
|
153
|
28 |
|
break; |
154
|
|
|
default: |
155
|
48 |
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
156
|
48 |
|
break; |
157
|
|
|
} |
158
|
|
|
|
159
|
76 |
|
return $this->removeExtraSpaces($dumpCommand); |
160
|
|
|
} |
161
|
|
|
|
162
|
32 |
|
public function getRestoreCommand(string $credentialFile = '', string $filePath = '') |
163
|
|
|
{ |
164
|
32 |
|
$filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath; |
165
|
32 |
|
switch (strtolower($this->getDumperClassName())) { |
166
|
32 |
|
case 'mysqldumper': |
167
|
6 |
|
$restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath); |
|
|
|
|
168
|
6 |
|
break; |
169
|
|
|
default: |
170
|
26 |
|
$restoreCommand = $this->prepareRestoreCommand($filePath); |
171
|
26 |
|
break; |
172
|
|
|
} |
173
|
|
|
|
174
|
32 |
|
return $this->removeExtraSpaces($restoreCommand); |
175
|
|
|
} |
176
|
|
|
|
177
|
108 |
|
public function getDumperClassName() |
178
|
|
|
{ |
179
|
108 |
|
$classWithNamespace = static::class; |
180
|
108 |
|
$partials = explode("\\", $classWithNamespace); |
181
|
108 |
|
$className = end($partials); |
182
|
108 |
|
return $className; |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|