1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\DbDumper\Databases; |
4
|
|
|
|
5
|
|
|
use Spatie\DbDumper\DbDumper; |
6
|
|
|
use Spatie\DbDumper\Exceptions\CannotStartDump; |
7
|
|
|
use Symfony\Component\Process\Process; |
8
|
|
|
|
9
|
|
|
class MySql extends DbDumper |
10
|
|
|
{ |
11
|
|
|
protected $dbName; |
12
|
|
|
protected $userName; |
13
|
|
|
protected $password; |
14
|
|
|
protected $host = 'localhost'; |
15
|
|
|
protected $port = 3306; |
16
|
|
|
protected $socket; |
17
|
|
|
protected $dumpBinaryPath = ''; |
18
|
|
|
protected $useExtendedInserts = true; |
19
|
|
|
|
20
|
|
|
public function setDbName(string $dbName) : MySql |
21
|
|
|
{ |
22
|
|
|
$this->dbName = $dbName; |
23
|
|
|
|
24
|
|
|
return $this; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
public function setUserName(string $userName) : MySql |
28
|
|
|
{ |
29
|
|
|
$this->userName = $userName; |
30
|
|
|
|
31
|
|
|
return $this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
public function setPassword(string $password) : MySql |
35
|
|
|
{ |
36
|
|
|
$this->password = $password; |
37
|
|
|
|
38
|
|
|
return $this; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
public function setHost(string $host) : MySql |
42
|
|
|
{ |
43
|
|
|
$this->host = $host; |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function setPort(int $port) : MySql |
49
|
|
|
{ |
50
|
|
|
$this->port = $port; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function setSocket(int $socket) : MySql |
56
|
|
|
{ |
57
|
|
|
$this->socket = $socket; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function setDumpBinaryPath(string $dumpBinaryPath) : MySql |
63
|
|
|
{ |
64
|
|
|
if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') { |
65
|
|
|
$dumpBinaryPath .= '/'; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
$this->dumpBinaryPath = $dumpBinaryPath; |
69
|
|
|
|
70
|
|
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function useExtendedInserts() : MySql |
74
|
|
|
{ |
75
|
|
|
$this->useExtendedInserts = true; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function dontUseExtendedInserts() : MySql |
81
|
|
|
{ |
82
|
|
|
$this->useExtendedInserts = false; |
83
|
|
|
|
84
|
|
|
return $this; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Dump the contents of the database to the given file. |
89
|
|
|
*/ |
90
|
|
|
public function dumpToFile(string $dumpFile) |
91
|
|
|
{ |
92
|
|
|
$this->guardAgainstIncompleteCredentials(); |
93
|
|
|
|
94
|
|
|
$tempFileHandle = tmpfile(); |
95
|
|
|
|
96
|
|
|
fwrite($tempFileHandle, $this->getContentsOfCredentialsFile()); |
97
|
|
|
|
98
|
|
|
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri']; |
99
|
|
|
|
100
|
|
|
$command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile); |
101
|
|
|
|
102
|
|
|
$process = (new Process($command))->run(); |
103
|
|
|
|
104
|
|
|
$this->checkIfDumpWasSuccessFull($process, $dumpFile); |
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get the command that should be performed to dump the database. |
109
|
|
|
*/ |
110
|
|
|
public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile) : string |
111
|
|
|
{ |
112
|
|
|
$command = [ |
113
|
|
|
"{$this->dumpBinaryPath}mysqldump", |
114
|
|
|
"--defaults-extra-file={$temporaryCredentialsFile}", |
115
|
|
|
'--skip-comments', |
116
|
|
|
$this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert', |
117
|
|
|
]; |
118
|
|
|
|
119
|
|
|
if ($this->socket != '') { |
120
|
|
|
$command[] = "--socket={$this->socket}"; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$command[] = "{$this->dbName} > {$dumpFile}"; |
124
|
|
|
|
125
|
|
|
return implode(' ', $command); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getContentsOfCredentialsFile() : string |
129
|
|
|
{ |
130
|
|
|
$contents = [ |
131
|
|
|
'[client]', |
132
|
|
|
"user = '{$this->userName}'", |
133
|
|
|
"password = '{$this->password}'", |
134
|
|
|
"host = '{$this->host}'", |
135
|
|
|
"port = '{$this->port}'", |
136
|
|
|
]; |
137
|
|
|
|
138
|
|
|
return implode(PHP_EOL, $contents); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
protected function guardAgainstIncompleteCredentials() |
142
|
|
|
{ |
143
|
|
|
foreach (['userName', 'dbName', 'host'] as $requiredProperty) { |
144
|
|
|
if (strlen($this->$requiredProperty) === 0) { |
145
|
|
|
throw CannotStartDump::emptyParameter($requiredProperty); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: