|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Spatie\DbDumper; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\Process\Process; |
|
6
|
|
|
use Spatie\DbDumper\Exceptions\DumpFailed; |
|
7
|
|
|
use Spatie\DbDumper\Compressors\GzipCompressor; |
|
8
|
|
|
use Spatie\DbDumper\Compressors\Bzip2Compressor; |
|
9
|
|
|
use Spatie\DbDumper\Compressors\LzmaCompressor; |
|
10
|
|
|
use Spatie\DbDumper\Exceptions\CannotSetParameter; |
|
11
|
|
|
|
|
12
|
|
|
abstract class DbDumper |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
protected $dbName; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
protected $userName; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
protected $password; |
|
22
|
|
|
|
|
23
|
|
|
/** @var string */ |
|
24
|
|
|
protected $host = 'localhost'; |
|
25
|
|
|
|
|
26
|
|
|
/** @var int */ |
|
27
|
|
|
protected $port = 5432; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
protected $socket = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** @var int */ |
|
33
|
|
|
protected $timeout = 0; |
|
34
|
|
|
|
|
35
|
|
|
/** @var string */ |
|
36
|
|
|
protected $dumpBinaryPath = ''; |
|
37
|
|
|
|
|
38
|
|
|
/** @var array */ |
|
39
|
|
|
protected $includeTables = []; |
|
40
|
|
|
|
|
41
|
|
|
/** @var array */ |
|
42
|
|
|
protected $excludeTables = []; |
|
43
|
|
|
|
|
44
|
|
|
/** @var array */ |
|
45
|
|
|
protected $extraOptions = []; |
|
46
|
|
|
|
|
47
|
|
|
/** @var string */ |
|
48
|
|
|
protected $compressor = null; |
|
49
|
|
|
|
|
50
|
|
|
public static function create() |
|
51
|
|
|
{ |
|
52
|
|
|
return new static(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function getDbName(): string |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->dbName; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param string $dbName |
|
62
|
|
|
* |
|
63
|
|
|
* @return $this |
|
64
|
|
|
*/ |
|
65
|
|
|
public function setDbName(string $dbName) |
|
66
|
|
|
{ |
|
67
|
|
|
$this->dbName = $dbName; |
|
68
|
|
|
|
|
69
|
|
|
return $this; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param string $userName |
|
74
|
|
|
* |
|
75
|
|
|
* @return $this |
|
76
|
|
|
*/ |
|
77
|
|
|
public function setUserName(string $userName) |
|
78
|
|
|
{ |
|
79
|
|
|
$this->userName = $userName; |
|
80
|
|
|
|
|
81
|
|
|
return $this; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @param string $password |
|
86
|
|
|
* |
|
87
|
|
|
* @return $this |
|
88
|
|
|
*/ |
|
89
|
|
|
public function setPassword(string $password) |
|
90
|
|
|
{ |
|
91
|
|
|
$this->password = $password; |
|
92
|
|
|
|
|
93
|
|
|
return $this; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $host |
|
98
|
|
|
* |
|
99
|
|
|
* @return $this |
|
100
|
|
|
*/ |
|
101
|
|
|
public function setHost(string $host) |
|
102
|
|
|
{ |
|
103
|
|
|
$this->host = $host; |
|
104
|
|
|
|
|
105
|
|
|
return $this; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function getHost(): string |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->host; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param int $port |
|
115
|
|
|
* |
|
116
|
|
|
* @return $this |
|
117
|
|
|
*/ |
|
118
|
|
|
public function setPort(int $port) |
|
119
|
|
|
{ |
|
120
|
|
|
$this->port = $port; |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param string $socket |
|
127
|
|
|
* |
|
128
|
|
|
* @return $this |
|
129
|
|
|
*/ |
|
130
|
|
|
public function setSocket(string $socket) |
|
131
|
|
|
{ |
|
132
|
|
|
$this->socket = $socket; |
|
133
|
|
|
|
|
134
|
|
|
return $this; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @param int $timeout |
|
139
|
|
|
* |
|
140
|
|
|
* @return $this |
|
141
|
|
|
*/ |
|
142
|
|
|
public function setTimeout(int $timeout) |
|
143
|
|
|
{ |
|
144
|
|
|
$this->timeout = $timeout; |
|
145
|
|
|
|
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @param string $dumpBinaryPath |
|
151
|
|
|
* |
|
152
|
|
|
* @return $this |
|
153
|
|
|
*/ |
|
154
|
|
|
public function setDumpBinaryPath(string $dumpBinaryPath) |
|
155
|
|
|
{ |
|
156
|
|
|
if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') { |
|
157
|
|
|
$dumpBinaryPath .= '/'; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
|
|
$this->dumpBinaryPath = $dumpBinaryPath; |
|
161
|
|
|
|
|
162
|
|
|
return $this; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
public function enableCompression() |
|
166
|
|
|
{ |
|
167
|
|
|
return $this->GzipCompression(); |
|
168
|
|
|
} |
|
169
|
|
|
|
|
170
|
|
|
/** |
|
171
|
|
|
* @param string $level |
|
172
|
|
|
* |
|
173
|
|
|
* @return $this |
|
174
|
|
|
*/ |
|
175
|
|
|
public function GzipCompression(string $level = '9') |
|
176
|
|
|
{ |
|
177
|
|
|
$this->compressor = new GzipCompressor; |
|
|
|
|
|
|
178
|
|
|
|
|
179
|
|
|
$this->compressor->setLevel($level); |
|
180
|
|
|
|
|
181
|
|
|
return $this; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @param string $level |
|
186
|
|
|
* |
|
187
|
|
|
* @return $this |
|
188
|
|
|
*/ |
|
189
|
|
|
public function Bzip2Compression(string $level = '9') |
|
190
|
|
|
{ |
|
191
|
|
|
$this->compressor = new Bzip2Compressor; |
|
|
|
|
|
|
192
|
|
|
|
|
193
|
|
|
$this->compressor->setLevel($level); |
|
194
|
|
|
|
|
195
|
|
|
return $this; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* @param string $level |
|
200
|
|
|
* |
|
201
|
|
|
* @return $this |
|
202
|
|
|
*/ |
|
203
|
|
|
public function LzmaCompression(string $level = 'e') |
|
204
|
|
|
{ |
|
205
|
|
|
$this->compressor = new LzmaCompressor; |
|
|
|
|
|
|
206
|
|
|
|
|
207
|
|
|
$this->compressor->setLevel($level); |
|
208
|
|
|
|
|
209
|
|
|
return $this; |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
public function getCompressorExtension() |
|
213
|
|
|
{ |
|
214
|
|
|
return $this->compressor->getExtension(); |
|
|
|
|
|
|
215
|
|
|
} |
|
216
|
|
|
|
|
217
|
|
|
/** |
|
218
|
|
|
* @param string|array $includeTables |
|
219
|
|
|
* |
|
220
|
|
|
* @return $this |
|
221
|
|
|
* |
|
222
|
|
|
* @throws \Spatie\DbDumper\Exceptions\CannotSetParameter |
|
223
|
|
|
*/ |
|
224
|
|
View Code Duplication |
public function includeTables($includeTables) |
|
|
|
|
|
|
225
|
|
|
{ |
|
226
|
|
|
if (! empty($this->excludeTables)) { |
|
227
|
|
|
throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables'); |
|
228
|
|
|
} |
|
229
|
|
|
|
|
230
|
|
|
if (! is_array($includeTables)) { |
|
231
|
|
|
$includeTables = explode(', ', $includeTables); |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
$this->includeTables = $includeTables; |
|
235
|
|
|
|
|
236
|
|
|
return $this; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* @param string|array $excludeTables |
|
241
|
|
|
* |
|
242
|
|
|
* @return $this |
|
243
|
|
|
* |
|
244
|
|
|
* @throws \Spatie\DbDumper\Exceptions\CannotSetParameter |
|
245
|
|
|
*/ |
|
246
|
|
View Code Duplication |
public function excludeTables($excludeTables) |
|
|
|
|
|
|
247
|
|
|
{ |
|
248
|
|
|
if (! empty($this->includeTables)) { |
|
249
|
|
|
throw CannotSetParameter::conflictingParameters('excludeTables', 'includeTables'); |
|
250
|
|
|
} |
|
251
|
|
|
|
|
252
|
|
|
if (! is_array($excludeTables)) { |
|
253
|
|
|
$excludeTables = explode(', ', $excludeTables); |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
$this->excludeTables = $excludeTables; |
|
257
|
|
|
|
|
258
|
|
|
return $this; |
|
259
|
|
|
} |
|
260
|
|
|
|
|
261
|
|
|
/** |
|
262
|
|
|
* @param string $extraOption |
|
263
|
|
|
* |
|
264
|
|
|
* @return $this |
|
265
|
|
|
*/ |
|
266
|
|
|
public function addExtraOption(string $extraOption) |
|
267
|
|
|
{ |
|
268
|
|
|
if (! empty($extraOption)) { |
|
269
|
|
|
$this->extraOptions[] = $extraOption; |
|
270
|
|
|
} |
|
271
|
|
|
|
|
272
|
|
|
return $this; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
abstract public function dumpToFile(string $dumpFile); |
|
276
|
|
|
|
|
277
|
|
|
protected function checkIfDumpWasSuccessFul(Process $process, string $outputFile) |
|
278
|
|
|
{ |
|
279
|
|
|
if (! $process->isSuccessful()) { |
|
280
|
|
|
throw DumpFailed::processDidNotEndSuccessfully($process); |
|
281
|
|
|
} |
|
282
|
|
|
|
|
283
|
|
|
if (! file_exists($outputFile)) { |
|
284
|
|
|
throw DumpFailed::dumpfileWasNotCreated(); |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
if (filesize($outputFile) === 0) { |
|
288
|
|
|
throw DumpFailed::dumpfileWasEmpty(); |
|
289
|
|
|
} |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
protected function echoToFile(string $command, string $dumpFile): string |
|
293
|
|
|
{ |
|
294
|
|
|
$compression = $this->compressor ? ' | '.$this->compressor->getCompressorCommand() : ''; |
|
|
|
|
|
|
295
|
|
|
$dumpFile = '"'.addcslashes($dumpFile, '\\"').'"'; |
|
296
|
|
|
|
|
297
|
|
|
return $command.$compression.' > '.$dumpFile; |
|
298
|
|
|
} |
|
299
|
|
|
} |
|
300
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..