1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper; |
4
|
|
|
|
5
|
|
|
use CodexShaper\Dumper\Contracts\Dumper as DumperContract; |
6
|
|
|
use CodexShaper\Dumper\Traits\DumperTrait; |
7
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
8
|
|
|
use Symfony\Component\Process\Process; |
9
|
|
|
|
10
|
|
|
abstract class Dumper implements DumperContract |
11
|
|
|
{ |
12
|
|
|
use DumperTrait; |
13
|
|
|
|
14
|
18 |
|
public function __construct(array $options = []) |
15
|
|
|
{ |
16
|
18 |
|
foreach ($options as $option => $value) { |
17
|
|
|
if (property_exists($this, $option)) { |
18
|
|
|
$this->{$option} = $value; |
19
|
|
|
} |
20
|
|
|
} |
21
|
18 |
|
} |
22
|
|
|
/** |
23
|
|
|
* @return $this |
24
|
|
|
*/ |
25
|
18 |
|
public static function create(array $options = []) |
26
|
|
|
{ |
27
|
18 |
|
return new static($options); |
28
|
|
|
} |
29
|
|
|
/** |
30
|
|
|
* @return \Symfony\Component\Process\Process |
31
|
|
|
*/ |
32
|
16 |
|
protected function prepareProcessCommand() |
33
|
|
|
{ |
34
|
16 |
|
$process = Process::fromShellCommandline($this->command); |
35
|
16 |
|
$process->setTimeout($this->timeout); |
36
|
16 |
|
return $process; |
37
|
|
|
} |
38
|
|
|
/** |
39
|
|
|
* @return \Symfony\Component\Process\Process |
40
|
|
|
*/ |
41
|
|
|
protected function run() |
42
|
|
|
{ |
43
|
|
|
try { |
44
|
|
|
|
45
|
|
|
$process = Process::fromShellCommandline($this->command); |
46
|
|
|
$process->setTimeout($this->timeout); |
47
|
|
|
|
48
|
|
|
if ($this->debug) { |
49
|
|
|
return $process->mustRun(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $process->run(); |
|
|
|
|
53
|
|
|
|
54
|
|
|
} catch (ProcessFailedException $e) { |
55
|
|
|
throw new \Exception($e->getMessage()); |
56
|
|
|
|
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
abstract public function dump(); |
61
|
|
|
abstract public function restore(); |
62
|
|
|
|
63
|
|
|
public function prepareHost() |
64
|
|
|
{ |
65
|
|
|
switch (strtolower($this->getDumperClassName())) { |
66
|
|
|
case 'pgsqldumper': |
67
|
|
|
return ($this->socket !== '') ? $this->socket : $this->host; |
68
|
|
|
break; |
|
|
|
|
69
|
|
|
case 'mongodumper'; |
70
|
|
|
return !empty($this->host) ? "--host {$this->host}" : ""; |
71
|
|
|
break; |
72
|
|
|
} |
73
|
|
|
return $host; |
|
|
|
|
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function preparePort() |
77
|
|
|
{ |
78
|
|
|
switch (strtolower($this->getDumperClassName())) { |
79
|
|
|
case 'pgsqldumper': |
80
|
|
|
return !empty($this->port) ? '-p ' . $this->port : ''; |
81
|
|
|
break; |
|
|
|
|
82
|
|
|
case 'mongodumper': |
83
|
|
|
return !empty($this->port) ? "--port {$this->port}" : ""; |
84
|
|
|
break; |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function prepareSocket() |
89
|
|
|
{ |
90
|
|
|
switch (strtolower($this->getDumperClassName())) { |
91
|
|
|
case 'mysqldumper': |
92
|
|
|
return ($this->socket !== '') ? "--socket={$this->socket}" : ''; |
93
|
|
|
break; |
|
|
|
|
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function prepareDatabase() |
98
|
|
|
{ |
99
|
|
|
switch (strtolower($this->getDumperClassName())) { |
100
|
|
|
case 'mysqldumper': |
101
|
|
|
case 'pgsqldumper': |
102
|
|
|
return !empty($this->dbName) ? $this->dbName : ""; |
103
|
|
|
break; |
|
|
|
|
104
|
|
|
case 'mongodumper'; |
105
|
|
|
return !empty($this->dbName) ? "--db {$this->dbName}" : ""; |
106
|
|
|
break; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public function prepareUserName() |
111
|
|
|
{ |
112
|
|
|
switch (strtolower($this->getDumperClassName())) { |
113
|
|
|
case 'pgsqldumper': |
114
|
|
|
return !empty($this->username) ? $this->username : ""; |
115
|
|
|
break; |
|
|
|
|
116
|
|
|
case 'mongodumper'; |
117
|
|
|
return !empty($this->username) ? "--username {$this->username}" : ""; |
118
|
|
|
break; |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function prepareIncludeTables() |
123
|
|
|
{ |
124
|
|
|
switch (strtolower($this->getDumperClassName())) { |
125
|
|
|
case 'mysqldumper': |
126
|
|
|
$includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : ''; |
127
|
|
|
return !empty($includeTables) ? "--tables {$includeTables}" : ''; |
128
|
|
|
break; |
|
|
|
|
129
|
|
|
case 'pgsqldumper': |
130
|
|
|
return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : ""; |
131
|
|
|
break; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function prepareIgnoreTables() |
136
|
|
|
{ |
137
|
|
|
switch (strtolower($this->getDumperClassName())) { |
138
|
|
|
case 'mysqldumper': |
139
|
|
|
$ignoreTablesArgs = []; |
140
|
|
|
foreach ($this->ignoreTables as $tableName) { |
141
|
|
|
$ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}"; |
142
|
|
|
} |
143
|
|
|
return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : ''; |
144
|
|
|
break; |
|
|
|
|
145
|
|
|
case 'pgsqldumper'; |
146
|
|
|
return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : ''; |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
public function prepareCreateTables() |
152
|
|
|
{ |
153
|
|
|
switch (strtolower($this->getDumperClassName())) { |
154
|
|
|
case 'mysqldumper': |
155
|
|
|
return !$this->createTables ? '--no-create-info' : ''; |
156
|
|
|
break; |
|
|
|
|
157
|
|
|
case 'pgsqldumper': |
158
|
|
|
return (!$this->createTables) ? '--data-only' : ''; |
159
|
|
|
break; |
160
|
|
|
} |
161
|
|
|
return $createTables; |
|
|
|
|
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
public function getDumperClassName() |
165
|
|
|
{ |
166
|
|
|
$classWithNamespace = static::class; |
167
|
|
|
$partials = explode("\\", $classWithNamespace); |
168
|
|
|
$className = end($partials); |
169
|
|
|
return $className; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|