Skip to content

Commit 55298c0

Browse files
committed
Separate prepare option methods from Dumper
1 parent 9ce9acd commit 55298c0

File tree

3 files changed

+107
-134
lines changed

3 files changed

+107
-134
lines changed

src/Dumper.php

Lines changed: 17 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
use CodexShaper\Dumper\Contracts\Dumper as DumperContract;
66
use CodexShaper\Dumper\Traits\DumperTrait;
7+
use CodexShaper\Dumper\Traits\PrepareOptionsTrait;
78
use Symfony\Component\Process\Exception\ProcessFailedException;
89
use Symfony\Component\Process\Process;
910

1011
abstract class Dumper implements DumperContract
1112
{
12-
use DumperTrait;
13+
use DumperTrait, PrepareOptionsTrait;
1314

1415
public function __construct(array $options = [])
1516
{
@@ -60,90 +61,6 @@ protected function run()
6061
abstract public function dump();
6162
abstract public function restore();
6263

63-
public function prepareHost()
64-
{
65-
switch (strtolower($this->getDumperClassName())) {
66-
case 'pgsqldumper':
67-
return ($this->socket !== '') ? $this->socket : $this->host;
68-
case 'mongodumper';
69-
return !empty($this->host) ? "--host {$this->host}" : "";
70-
}
71-
}
72-
73-
public function preparePort()
74-
{
75-
switch (strtolower($this->getDumperClassName())) {
76-
case 'pgsqldumper':
77-
return !empty($this->port) ? '-p ' . $this->port : '';
78-
case 'mongodumper':
79-
return !empty($this->port) ? "--port {$this->port}" : "";
80-
}
81-
}
82-
83-
public function prepareSocket()
84-
{
85-
switch (strtolower($this->getDumperClassName())) {
86-
case 'mysqldumper':
87-
return ($this->socket !== '') ? "--socket={$this->socket}" : '';
88-
}
89-
}
90-
91-
public function prepareDatabase()
92-
{
93-
switch (strtolower($this->getDumperClassName())) {
94-
case 'mysqldumper':
95-
case 'pgsqldumper':
96-
return !empty($this->dbName) ? $this->dbName : "";
97-
case 'mongodumper';
98-
return !empty($this->dbName) ? "--db {$this->dbName}" : "";
99-
}
100-
}
101-
102-
public function prepareUserName()
103-
{
104-
switch (strtolower($this->getDumperClassName())) {
105-
case 'pgsqldumper':
106-
return !empty($this->username) ? $this->username : "";
107-
case 'mongodumper';
108-
return !empty($this->username) ? "--username {$this->username}" : "";
109-
}
110-
}
111-
112-
public function prepareIncludeTables()
113-
{
114-
switch (strtolower($this->getDumperClassName())) {
115-
case 'mysqldumper':
116-
$includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : '';
117-
return !empty($includeTables) ? "--tables {$includeTables}" : '';
118-
case 'pgsqldumper':
119-
return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : "";
120-
}
121-
}
122-
123-
public function prepareIgnoreTables()
124-
{
125-
switch (strtolower($this->getDumperClassName())) {
126-
case 'mysqldumper':
127-
$ignoreTablesArgs = [];
128-
foreach ($this->ignoreTables as $tableName) {
129-
$ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}";
130-
}
131-
return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : '';
132-
case 'pgsqldumper';
133-
return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : '';
134-
}
135-
}
136-
137-
public function prepareCreateTables()
138-
{
139-
switch (strtolower($this->getDumperClassName())) {
140-
case 'mysqldumper':
141-
return !$this->createTables ? '--no-create-info' : '';
142-
case 'pgsqldumper':
143-
return (!$this->createTables) ? '--data-only' : '';
144-
}
145-
}
146-
14764
public function getDumpCommand(string $credentialFile = '', $destinationPath = '')
14865
{
14966
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath;
@@ -181,4 +98,19 @@ public function getDumperClassName()
18198
$className = end($partials);
18299
return $className;
183100
}
101+
102+
public function removeExtraSpaces(string $str)
103+
{
104+
return preg_replace('/\s+/', ' ', $str);
105+
}
106+
107+
public static function isWindows()
108+
{
109+
return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false;
110+
}
111+
112+
public function quoteCommand(string $command)
113+
{
114+
return static::isWindows() ? "\"{$command}\"" : "'{$command}'";
115+
}
184116
}

src/Traits/DumperTrait.php

Lines changed: 0 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -50,42 +50,36 @@ public function setDbName(string $name)
5050
public function setUserName(string $username)
5151
{
5252
$this->username = $username;
53-
5453
return $this;
5554
}
5655

5756
public function setPassword(string $password)
5857
{
5958
$this->password = $password;
60-
6159
return $this;
6260
}
6361

6462
public function setHost(string $host)
6563
{
6664
$this->host = $host;
67-
6865
return $this;
6966
}
7067

7168
public function setPort(int $port)
7269
{
7370
$this->port = $port;
74-
7571
return $this;
7672
}
7773

7874
public function setSocket(string $socket)
7975
{
8076
$this->socket = $socket;
81-
8277
return $this;
8378
}
8479

8580
public function setTimeOut(int $timeout)
8681
{
8782
$this->timeout = $timeout;
88-
8983
return $this;
9084
}
9185
/**
@@ -97,13 +91,10 @@ public function setTables($tables)
9791
if (!empty($this->ignoreTables)) {
9892
throw new \Exception("You can choose only once between tables and ignoreTables at a time");
9993
}
100-
10194
if (is_string($tables)) {
10295
$tables = [$tables];
10396
}
104-
10597
$this->tables = $tables;
106-
10798
return $this;
10899
}
109100
/**
@@ -114,59 +105,45 @@ public function setIgnoreTables($tables)
114105
{
115106
if (!empty($this->tables)) {
116107
throw new \Exception("You can choose only once between tables and ignoreTables at a time");
117-
118108
}
119-
120109
if (is_string($tables)) {
121110
$tables = [$tables];
122111
}
123-
124112
$this->ignoreTables = $tables;
125-
126113
return $this;
127114
}
128-
129115
public function setCommandBinaryPath(string $path)
130116
{
131117
$this->commandBinaryPath = $path;
132-
133118
return $this;
134119
}
135-
136120
public function setDestinationPath(string $path)
137121
{
138122
$this->destinationPath = $path;
139-
140123
return $this;
141124
}
142-
143125
public function setRestorePath(string $path)
144126
{
145127
$this->restorePath = $path;
146-
147128
return $this;
148129
}
149-
150130
// Compress
151131
public function setCompressBinaryPath(string $path)
152132
{
153133
$this->compressBinaryPath = $path;
154134
return $this;
155135
}
156-
157136
public function setCompressCommand(string $command)
158137
{
159138
$this->compressCommand = $command;
160139
$this->isCompress = true;
161140
return $this;
162141
}
163-
164142
public function setCompressExtension(string $extension)
165143
{
166144
$this->compressExtension = $extension;
167145
return $this;
168146
}
169-
170147
public function useCompress(string $command = "gzip", string $extension = ".gz", string $binary_path = "")
171148
{
172149
$this->compressCommand = $command;
@@ -176,75 +153,49 @@ public function useCompress(string $command = "gzip", string $extension = ".gz",
176153

177154
return $this;
178155
}
179-
180156
public function enableDebug()
181157
{
182158
$this->debug = true;
183159
return $this;
184160
}
185-
186161
public function getDbName()
187162
{
188163
return $this->dbName;
189164
}
190-
191165
public function getUserName()
192166
{
193167
return $this->username;
194168
}
195-
196169
public function getPassword()
197170
{
198171
return $this->password;
199172
}
200-
201173
public function getHost()
202174
{
203175
return $this->host;
204176
}
205-
206177
public function getPort()
207178
{
208179
return $this->port;
209180
}
210-
211181
public function getSocket()
212182
{
213183
return $this->socket;
214184
}
215-
216185
public function getTimeOut()
217186
{
218187
return $this->timeout;
219188
}
220-
221189
public function getCommandBinaryPath()
222190
{
223191
return $this->commandBinaryPath;
224192
}
225-
226193
public function getDestinationPath()
227194
{
228195
return $this->destinationPath;
229196
}
230-
231197
public function getRestorePath()
232198
{
233199
return $this->restorePath;
234200
}
235-
236-
public function removeExtraSpaces(string $str)
237-
{
238-
return preg_replace('/\s+/', ' ', $str);
239-
}
240-
241-
public static function isWindows()
242-
{
243-
return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false;
244-
}
245-
246-
public function quoteCommand(string $command)
247-
{
248-
return static::isWindows() ? "\"{$command}\"" : "'{$command}'";
249-
}
250201
}

0 commit comments

Comments
 (0)