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
|
|
|
* @return \Spatie\DbDumper\Databases\MySql |
41
|
|
|
*/ |
42
|
|
|
public function setDbName($dbName) |
43
|
|
|
{ |
44
|
|
|
$this->dbName = $dbName; |
45
|
|
|
|
46
|
|
|
return $this; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $userName |
51
|
|
|
* |
52
|
|
|
* @return \Spatie\DbDumper\Databases\MySql |
53
|
|
|
*/ |
54
|
|
|
public function setUserName($userName) |
55
|
|
|
{ |
56
|
|
|
$this->userName = $userName; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param string $password |
63
|
|
|
* |
64
|
|
|
* @return \Spatie\DbDumper\Databases\MySql |
65
|
|
|
*/ |
66
|
|
|
public function setPassword($password) |
67
|
|
|
{ |
68
|
|
|
$this->password = $password; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $host |
75
|
|
|
* |
76
|
|
|
* @return \Spatie\DbDumper\Databases\MySql |
77
|
|
|
*/ |
78
|
|
|
public function setHost($host) |
79
|
|
|
{ |
80
|
|
|
$this->host = $host; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param int $port |
87
|
|
|
* |
88
|
|
|
* @return \Spatie\DbDumper\Databases\MySql |
89
|
|
|
*/ |
90
|
|
|
public function setPort($port) |
91
|
|
|
{ |
92
|
|
|
$this->port = $port; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $dumpBinaryPath |
99
|
|
|
* |
100
|
|
|
* @return \Spatie\DbDumper\Databases\PostgreSql |
101
|
|
|
*/ |
102
|
|
|
public function setDumpBinaryPath($dumpBinaryPath) |
103
|
|
|
{ |
104
|
|
|
if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') { |
105
|
|
|
$dumpBinaryPath .= '/'; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
$this->dumpBinaryPath = $dumpBinaryPath; |
109
|
|
|
|
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param \Symfony\Component\Process\Process $process |
115
|
|
|
* @param string $outputFile |
116
|
|
|
* |
117
|
|
|
* @return bool |
118
|
|
|
* |
119
|
|
|
* @throws \Spatie\DbDumper\Exceptions\DumpFailed |
120
|
|
|
*/ |
121
|
|
|
protected function checkIfDumpWasSuccessFul(Process $process, $outputFile) |
122
|
|
|
{ |
123
|
|
|
if (!$process->isSuccessful()) { |
124
|
|
|
throw DumpFailed::processDidNotEndSuccessfully($process); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
if (!file_exists($outputFile)) { |
128
|
|
|
throw DumpFailed::dumpfileWasNotCreated(); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (filesize($outputFile) === 0) { |
132
|
|
|
throw DumpFailed::dumpfileWasEmpty(); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|