1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\DbDumper\Databases; |
4
|
|
|
|
5
|
|
|
use Spatie\DbDumper\DbDumper; |
6
|
|
|
use Spatie\DbDumper\Exceptions\CannotStartDump; |
7
|
|
|
|
8
|
|
|
class MySql extends DbDumper |
9
|
|
|
{ |
10
|
|
|
protected $dbName; |
11
|
|
|
protected $userName; |
12
|
|
|
protected $password; |
13
|
|
|
protected $host = 'localhost'; |
14
|
|
|
protected $port; |
15
|
|
|
protected $socket; |
16
|
|
|
protected $dumpBinaryPath = ''; |
17
|
|
|
protected $useExtendedInserts = true; |
18
|
|
|
|
19
|
|
|
public function setDbName(string $dbName) : MySql |
20
|
|
|
{ |
21
|
|
|
$this->dbName = $dbName; |
22
|
|
|
|
23
|
|
|
return $this; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
public function setUserName(string $userName) : MySql |
27
|
|
|
{ |
28
|
|
|
$this->userName = $userName; |
29
|
|
|
|
30
|
|
|
return $this; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
public function setPassword(string $password) : MySql |
34
|
|
|
{ |
35
|
|
|
$this->password = $password; |
36
|
|
|
|
37
|
|
|
return $this; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setHost(string $host) : MySql |
41
|
|
|
{ |
42
|
|
|
$this->host = $host; |
43
|
|
|
|
44
|
|
|
return $this; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function setPort(int $port) : MySql |
48
|
|
|
{ |
49
|
|
|
$this->port = $port; |
50
|
|
|
|
51
|
|
|
return $this; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
public function setSocket(int $socket) : MySql |
55
|
|
|
{ |
56
|
|
|
$this->socket = $socket; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
public function setDumpBinaryPath(string $dumpBinaryPath) : MySql |
62
|
|
|
{ |
63
|
|
|
if ($dumpBinaryPath != '' && substr($dumpBinaryPath, -1) != '/') { |
64
|
|
|
$dumpBinaryPath .= '/'; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->dumpBinaryPath = $dumpBinaryPath; |
68
|
|
|
|
69
|
|
|
return $this; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function useExtendedInserts() : MySql |
73
|
|
|
{ |
74
|
|
|
$this->useExtendedInserts = true; |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function doNotUseExtendedInserts() : MySql |
80
|
|
|
{ |
81
|
|
|
$this->useExtendedInserts = false; |
82
|
|
|
|
83
|
|
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Dump the contents of the database to the given file. |
88
|
|
|
*/ |
89
|
|
|
public function dumpToFile(string $dumpFile) |
90
|
|
|
{ |
91
|
|
|
$this->guardAgainstIncompletedCredentials(); |
92
|
|
|
|
93
|
|
|
$tempFileHandle = tmpfile(); |
94
|
|
|
|
95
|
|
|
fwrite($tempFileHandle, $this->getContentsOfCredentialFile()); |
|
|
|
|
96
|
|
|
|
97
|
|
|
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri']; |
98
|
|
|
|
99
|
|
|
$command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile); |
100
|
|
|
|
101
|
|
|
$this->process |
102
|
|
|
->setCommandLine($command) |
103
|
|
|
->run(); |
104
|
|
|
|
105
|
|
|
$this->checkIfDumpWasSuccessFull($dumpFile); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the command that should be performed to dump the database. |
110
|
|
|
*/ |
111
|
|
|
public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile) : string |
112
|
|
|
{ |
113
|
|
|
$command = [ |
114
|
|
|
"{$this->dumpBinaryPath}mysqldump", |
115
|
|
|
"--defaults-extra-file={$temporaryCredentialsFile}", |
116
|
|
|
'--skip-comments', |
117
|
|
|
$this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert', |
118
|
|
|
]; |
119
|
|
|
|
120
|
|
|
if ($this->socket != '') { |
121
|
|
|
$command[] = "--socket={$this->socket}"; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
$command[] = "{$this->dbName} > {$dumpFile}"; |
125
|
|
|
|
126
|
|
|
return implode(' ', $command); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
protected function guardAgainstIncompletedCredentials() |
130
|
|
|
{ |
131
|
|
|
foreach (['userName', 'dbName', 'host'] as $requiredProperty) { |
132
|
|
|
if ($this->$requiredProperty == '') { |
133
|
|
|
throw CannotStartDump::emptyParameter($requiredProperty); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
protected function getContentsOfCredentialsFile() : string |
139
|
|
|
{ |
140
|
|
|
$contents = [ |
141
|
|
|
'[client]', |
142
|
|
|
"user = '{$this->userName}'", |
143
|
|
|
"password = '{$this->password}'", |
144
|
|
|
"host = '{$this->host}'", |
145
|
|
|
"port = '{$this->port}'", |
146
|
|
|
]; |
147
|
|
|
|
148
|
|
|
return implode(PHP_EOL, $contents); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.