1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Spatie\DbDumper\Databases; |
4
|
|
|
|
5
|
|
|
use Spatie\DbDumper\DbDumper; |
6
|
|
|
use Symfony\Component\Process\Process; |
7
|
|
|
use Spatie\DbDumper\Exceptions\CannotStartDump; |
8
|
|
|
|
9
|
|
|
class MySql extends DbDumper |
10
|
|
|
{ |
11
|
|
|
/** @var bool */ |
12
|
|
|
protected $useExtendedInserts = true; |
13
|
|
|
|
14
|
|
|
/** @var bool */ |
15
|
|
|
protected $useSingleTransaction = false; |
16
|
|
|
|
17
|
|
|
/** @var bool */ |
18
|
|
|
protected $useSkipComments = true; |
19
|
|
|
|
20
|
|
|
public function __construct() |
21
|
|
|
{ |
22
|
|
|
$this->port = 3306; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @return $this |
27
|
|
|
*/ |
28
|
|
|
public function useExtendedInserts() |
29
|
|
|
{ |
30
|
|
|
$this->useExtendedInserts = true; |
31
|
|
|
|
32
|
|
|
return $this; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return $this |
37
|
|
|
*/ |
38
|
|
|
public function dontUseExtendedInserts() |
39
|
|
|
{ |
40
|
|
|
$this->useExtendedInserts = false; |
41
|
|
|
|
42
|
|
|
return $this; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function useSingleTransaction() |
49
|
|
|
{ |
50
|
|
|
$this->useSingleTransaction = true; |
51
|
|
|
|
52
|
|
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return $this |
57
|
|
|
*/ |
58
|
|
|
public function dontUseSingleTransaction() |
59
|
|
|
{ |
60
|
|
|
$this->useSingleTransaction = false; |
61
|
|
|
|
62
|
|
|
return $this; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @return $this |
67
|
|
|
*/ |
68
|
|
|
public function useSkipComments() |
69
|
|
|
{ |
70
|
|
|
$this->useSkipComments = true; |
71
|
|
|
|
72
|
|
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return $this |
77
|
|
|
*/ |
78
|
|
|
public function dontUseSkipComments() |
79
|
|
|
{ |
80
|
|
|
$this->useSkipComments = false; |
81
|
|
|
|
82
|
|
|
return $this; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Dump the contents of the database to the given file. |
87
|
|
|
* |
88
|
|
|
* @param string $dumpFile |
89
|
|
|
* |
90
|
|
|
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump |
91
|
|
|
* @throws \Spatie\DbDumper\Exceptions\DumpFailed |
92
|
|
|
*/ |
93
|
|
View Code Duplication |
public function dumpToFile(string $dumpFile) |
|
|
|
|
94
|
|
|
{ |
95
|
|
|
$this->guardAgainstIncompleteCredentials(); |
96
|
|
|
|
97
|
|
|
$tempFileHandle = tmpfile(); |
98
|
|
|
fwrite($tempFileHandle, $this->getContentsOfCredentialsFile()); |
99
|
|
|
$temporaryCredentialsFile = stream_get_meta_data($tempFileHandle)['uri']; |
100
|
|
|
|
101
|
|
|
$command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile); |
102
|
|
|
|
103
|
|
|
$process = new Process($command); |
104
|
|
|
|
105
|
|
|
if (! is_null($this->timeout)) { |
106
|
|
|
$process->setTimeout($this->timeout); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
$process->run(); |
110
|
|
|
|
111
|
|
|
$this->checkIfDumpWasSuccessFul($process, $dumpFile); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Get the command that should be performed to dump the database. |
116
|
|
|
* |
117
|
|
|
* @param string $dumpFile |
118
|
|
|
* @param string $temporaryCredentialsFile |
119
|
|
|
* |
120
|
|
|
* @return string |
121
|
|
|
*/ |
122
|
|
|
public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string |
123
|
|
|
{ |
124
|
|
|
$quote = (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN' ? '"' : "'"); |
125
|
|
|
|
126
|
|
|
$command = [ |
127
|
|
|
"{$quote}{$this->dumpBinaryPath}mysqldump{$quote}", |
128
|
|
|
"--defaults-extra-file=\"{$temporaryCredentialsFile}\"", |
129
|
|
|
$this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert', |
130
|
|
|
]; |
131
|
|
|
|
132
|
|
|
if ($this->useSkipComments) { |
133
|
|
|
$command[] = '--skip-comments'; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
if ($this->useSingleTransaction) { |
137
|
|
|
$command[] = '--single-transaction'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($this->socket !== '') { |
141
|
|
|
$command[] = "--socket={$this->socket}"; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (! empty($this->excludeTables)) { |
145
|
|
|
$command[] = '--ignore-table='.implode(' --ignore-table=', $this->excludeTables); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
foreach ($this->extraOptions as $extraOption) { |
149
|
|
|
$command[] = $extraOption; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
$command[] = "{$this->dbName}"; |
153
|
|
|
|
154
|
|
|
if (! empty($this->includeTables)) { |
155
|
|
|
$command[] = implode(' ', $this->includeTables); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
$command[] = "> \"{$dumpFile}\""; |
159
|
|
|
|
160
|
|
|
return implode(' ', $command); |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
public function getContentsOfCredentialsFile(): string |
164
|
|
|
{ |
165
|
|
|
$contents = [ |
166
|
|
|
'[client]', |
167
|
|
|
"user = '{$this->userName}'", |
168
|
|
|
"password = '{$this->password}'", |
169
|
|
|
"host = '{$this->host}'", |
170
|
|
|
"port = '{$this->port}'", |
171
|
|
|
]; |
172
|
|
|
|
173
|
|
|
return implode(PHP_EOL, $contents); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
View Code Duplication |
protected function guardAgainstIncompleteCredentials() |
|
|
|
|
177
|
|
|
{ |
178
|
|
|
foreach (['userName', 'dbName', 'host'] as $requiredProperty) { |
179
|
|
|
if (strlen($this->$requiredProperty) === 0) { |
180
|
|
|
throw CannotStartDump::emptyParameter($requiredProperty); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.