1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\DbDumper; |
4
|
|
|
|
5
|
|
|
use Spatie\DbDumper\Exceptions\DumpFailed; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
|
8
|
|
|
abstract class DbDumper |
9
|
|
|
{ |
10
|
|
|
protected $dbName; |
11
|
|
|
protected $userName; |
12
|
|
|
protected $password; |
13
|
|
|
protected $host = 'localhost'; |
14
|
|
|
protected $port = 0; |
15
|
|
|
protected $dumpBinaryPath = ''; |
16
|
|
|
|
17
|
|
|
public static function create() |
18
|
|
|
{ |
19
|
|
|
return new static(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Dump the contents of the database to the given file. |
24
|
|
|
* |
25
|
|
|
* @param string $dumpFile |
26
|
|
|
*/ |
27
|
|
|
abstract public function dumpToFile($dumpFile); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return string |
31
|
|
|
*/ |
32
|
|
|
public function getDbName() |
33
|
|
|
{ |
34
|
|
|
return $this->dbName; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $dbName |
39
|
|
|
*/ |
40
|
|
|
public function setDbName($dbName) |
41
|
|
|
{ |
42
|
|
|
$this->dbName = $dbName; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $userName |
49
|
|
|
* |
50
|
|
|
* @return $this |
51
|
|
|
*/ |
52
|
|
|
public function setUserName($userName) |
53
|
|
|
{ |
54
|
|
|
$this->userName = $userName; |
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $password |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setPassword($password) |
65
|
|
|
{ |
66
|
|
|
$this->password = $password; |
67
|
|
|
|
68
|
|
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string $host |
73
|
|
|
* |
74
|
|
|
* @return $this |
75
|
|
|
*/ |
76
|
|
|
public function setHost($host) |
77
|
|
|
{ |
78
|
|
|
$this->host = $host; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $port |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function setPort($port) |
89
|
|
|
{ |
90
|
|
|
$this->port = $port; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param string $dumpBinaryPath |
97
|
|
|
* |
98
|
|
|
* @return $this |
99
|
|
|
*/ |
100
|
|
|
public function setDumpBinaryPath($dumpBinaryPath) |
101
|
|
|
{ |
102
|
|
|
if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') { |
103
|
|
|
$dumpBinaryPath .= '/'; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$this->dumpBinaryPath = $dumpBinaryPath; |
107
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param \Symfony\Component\Process\Process $process |
113
|
|
|
* @param string $outputFile |
114
|
|
|
* |
115
|
|
|
* @return bool |
116
|
|
|
* |
117
|
|
|
* @throws \Spatie\DbDumper\Exceptions\DumpFailed |
118
|
|
|
*/ |
119
|
|
|
protected function checkIfDumpWasSuccessFul(Process $process, $outputFile) |
120
|
|
|
{ |
121
|
|
|
if (!$process->isSuccessful()) { |
122
|
|
|
throw DumpFailed::processDidNotEndSuccessfully($process); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (!file_exists($outputFile)) { |
126
|
|
|
throw DumpFailed::dumpfileWasNotCreated(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
if (filesize($outputFile) === 0) { |
130
|
|
|
throw DumpFailed::dumpfileWasEmpty(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
return true; |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|