|
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->getClassName())) { |
|
66
|
58 |
|
case 'pgsqldumper': |
|
67
|
30 |
|
$host = ($this->socket !== '') ? $this->socket : $this->host; |
|
68
|
30 |
|
break; |
|
69
|
28 |
|
case 'mongodumper'; |
|
70
|
28 |
|
$host = !empty($this->host) ? "--host {$this->host}" : ""; |
|
71
|
28 |
|
break; |
|
72
|
|
|
default: |
|
73
|
|
|
$host = $this->host; |
|
74
|
|
|
break; |
|
75
|
|
|
} |
|
76
|
58 |
|
return $host; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
58 |
|
public function preparePort() |
|
80
|
|
|
{ |
|
81
|
58 |
|
switch (strtolower($this->getClassName())) { |
|
82
|
58 |
|
case 'pgsqldumper': |
|
83
|
30 |
|
$port = !empty($this->port) ? '-p ' . $this->port : ''; |
|
84
|
30 |
|
break; |
|
85
|
28 |
|
case 'mongodumper': |
|
86
|
28 |
|
$port = !empty($this->port) ? "--port {$this->port}" : ""; |
|
87
|
28 |
|
break; |
|
88
|
|
|
default: |
|
89
|
|
|
$port = $this->port; |
|
90
|
|
|
break; |
|
91
|
|
|
} |
|
92
|
58 |
|
return $port; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
28 |
|
public function prepareSocket() |
|
96
|
|
|
{ |
|
97
|
28 |
|
switch (strtolower($this->getClassName())) { |
|
98
|
28 |
|
case 'mysqldumper': |
|
99
|
28 |
|
$socket = ($this->socket !== '') ? "--socket={$this->socket}" : ''; |
|
100
|
28 |
|
break; |
|
101
|
|
|
default: |
|
102
|
|
|
$socket = $this->socket; |
|
103
|
|
|
break; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
28 |
|
return $socket; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
80 |
|
public function prepareDatabase() |
|
110
|
|
|
{ |
|
111
|
80 |
|
switch (strtolower($this->getClassName())) { |
|
112
|
80 |
|
case 'mysqldumper': |
|
113
|
46 |
|
case 'pgsqldumper': |
|
114
|
64 |
|
$database = !empty($this->dbName) ? $this->dbName : ""; |
|
115
|
64 |
|
break; |
|
116
|
16 |
|
case 'mongodumper'; |
|
117
|
16 |
|
$database = !empty($this->dbName) ? "--db {$this->dbName}" : ""; |
|
118
|
16 |
|
break; |
|
119
|
|
|
default: |
|
120
|
|
|
$database = $this->dbName; |
|
121
|
|
|
break; |
|
122
|
|
|
} |
|
123
|
80 |
|
return $database; |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
58 |
|
public function prepareUserName() |
|
127
|
|
|
{ |
|
128
|
58 |
|
switch (strtolower($this->getClassName())) { |
|
129
|
58 |
|
case 'pgsqldumper': |
|
130
|
30 |
|
$username = !empty($this->username) ? $this->username : ""; |
|
131
|
30 |
|
break; |
|
132
|
28 |
|
case 'mongodumper'; |
|
133
|
28 |
|
$username = !empty($this->username) ? "--username {$this->username}" : ""; |
|
134
|
28 |
|
break; |
|
135
|
|
|
default: |
|
136
|
|
|
$username = $this->username; |
|
137
|
|
|
break; |
|
138
|
|
|
} |
|
139
|
58 |
|
return $username; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
52 |
|
public function prepareIncludeTables() |
|
143
|
|
|
{ |
|
144
|
52 |
|
switch (strtolower($this->getClassName())) { |
|
145
|
52 |
|
case 'mysqldumper': |
|
146
|
28 |
|
$includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : ''; |
|
147
|
28 |
|
$includeTablesArg = !empty($includeTables) ? "--tables {$includeTables}" : ''; |
|
148
|
28 |
|
break; |
|
149
|
24 |
|
case 'pgsqldumper': |
|
150
|
24 |
|
$includeTablesArg = (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : ""; |
|
151
|
24 |
|
break; |
|
152
|
|
|
default: |
|
153
|
|
|
$includeTablesArg = $this->tables; |
|
154
|
|
|
break; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
52 |
|
return $includeTablesArg; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
52 |
|
public function prepareIgnoreTables() |
|
161
|
|
|
{ |
|
162
|
52 |
|
switch (strtolower($this->getClassName())) { |
|
163
|
52 |
|
case 'mysqldumper': |
|
164
|
28 |
|
$ignoreTablesArgs = []; |
|
165
|
28 |
|
foreach ($this->ignoreTables as $tableName) { |
|
166
|
4 |
|
$ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}"; |
|
167
|
|
|
} |
|
168
|
28 |
|
$ignoreTablesArg = (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : ''; |
|
169
|
28 |
|
break; |
|
170
|
24 |
|
case 'pgsqldumper'; |
|
171
|
24 |
|
$ignoreTablesArg = (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : ''; |
|
172
|
24 |
|
break; |
|
173
|
|
|
default: |
|
174
|
|
|
$ignoreTablesArg = $this->ignoreTables; |
|
175
|
|
|
break; |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
52 |
|
return $ignoreTablesArg; |
|
179
|
|
|
} |
|
180
|
|
|
|
|
181
|
52 |
|
public function prepareCreateTables() |
|
182
|
|
|
{ |
|
183
|
52 |
|
switch (strtolower($this->getClassName())) { |
|
184
|
52 |
|
case 'mysqldumper': |
|
185
|
28 |
|
$createTables = !$this->createTables ? '--no-create-info' : ''; |
|
186
|
28 |
|
break; |
|
187
|
24 |
|
case 'pgsqldumper': |
|
188
|
24 |
|
$createTables = (!$this->createTables) ? '--data-only' : ''; |
|
189
|
24 |
|
break; |
|
190
|
|
|
default: |
|
191
|
|
|
$createTables = $this->createTables; |
|
192
|
|
|
break; |
|
193
|
|
|
} |
|
194
|
52 |
|
return $createTables; |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
92 |
|
public function getClassName() |
|
198
|
|
|
{ |
|
199
|
92 |
|
$classWithNamespace = static::class; |
|
200
|
92 |
|
$partials = explode("\\", $classWithNamespace); |
|
201
|
92 |
|
$className = end($partials); |
|
202
|
92 |
|
return $className; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
|