1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper\Traits; |
4
|
|
|
|
5
|
|
|
trait PrepareOptionsTrait |
6
|
|
|
{ |
7
|
58 |
|
public function prepareHost() |
8
|
|
|
{ |
9
|
58 |
|
switch (strtolower($this->getDumperClassName())) { |
10
|
58 |
|
case 'pgsqldumper': |
11
|
30 |
|
return ($this->socket !== '') ? $this->socket : $this->host; |
12
|
28 |
|
case 'mongodumper'; |
13
|
28 |
|
return !empty($this->host) ? "--host {$this->host}" : ""; |
14
|
|
|
} |
15
|
|
|
} |
16
|
|
|
|
17
|
58 |
|
public function preparePort() |
18
|
|
|
{ |
19
|
58 |
|
switch (strtolower($this->getDumperClassName())) { |
20
|
58 |
|
case 'pgsqldumper': |
21
|
30 |
|
return !empty($this->port) ? '-p ' . $this->port : ''; |
22
|
28 |
|
case 'mongodumper': |
23
|
28 |
|
return !empty($this->port) ? "--port {$this->port}" : ""; |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
28 |
|
public function prepareSocket() |
28
|
|
|
{ |
29
|
28 |
|
switch (strtolower($this->getDumperClassName())) { |
30
|
28 |
|
case 'mysqldumper': |
31
|
28 |
|
return ($this->socket !== '') ? "--socket={$this->socket}" : ''; |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
80 |
|
public function prepareDatabase() |
36
|
|
|
{ |
37
|
80 |
|
switch (strtolower($this->getDumperClassName())) { |
38
|
80 |
|
case 'mysqldumper': |
39
|
46 |
|
case 'pgsqldumper': |
40
|
64 |
|
return !empty($this->dbName) ? $this->dbName : ""; |
41
|
16 |
|
case 'mongodumper'; |
42
|
16 |
|
return !empty($this->dbName) ? "--db {$this->dbName}" : ""; |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
58 |
|
public function prepareUserName() |
47
|
|
|
{ |
48
|
58 |
|
switch (strtolower($this->getDumperClassName())) { |
49
|
58 |
|
case 'pgsqldumper': |
50
|
30 |
|
return !empty($this->username) ? $this->username : ""; |
51
|
28 |
|
case 'mongodumper'; |
52
|
28 |
|
return !empty($this->username) ? "--username {$this->username}" : ""; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
52 |
|
public function prepareIncludeTables() |
57
|
|
|
{ |
58
|
52 |
|
switch (strtolower($this->getDumperClassName())) { |
59
|
52 |
|
case 'mysqldumper': |
60
|
28 |
|
$includeTables = (count($this->tables) > 0) ? implode(' ', $this->tables) : ''; |
61
|
28 |
|
return !empty($includeTables) ? "--tables {$includeTables}" : ''; |
62
|
24 |
|
case 'pgsqldumper': |
63
|
24 |
|
return (count($this->tables) > 0) ? '-t ' . implode(' -t ', $this->tables) : ""; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
52 |
|
public function prepareIgnoreTables() |
68
|
|
|
{ |
69
|
52 |
|
switch (strtolower($this->getDumperClassName())) { |
70
|
52 |
|
case 'mysqldumper': |
71
|
28 |
|
$ignoreTablesArgs = []; |
72
|
28 |
|
foreach ($this->ignoreTables as $tableName) { |
73
|
4 |
|
$ignoreTablesArgs[] = "--ignore-table={$this->dbName}.{$tableName}"; |
74
|
|
|
} |
75
|
28 |
|
return (count($ignoreTablesArgs) > 0) ? implode(' ', $ignoreTablesArgs) : ''; |
76
|
24 |
|
case 'pgsqldumper'; |
77
|
24 |
|
return (count($this->ignoreTables) > 0) ? '-T ' . implode(' -T ', $this->ignoreTables) : ''; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
81
|
76 |
|
public function getDumpCommand(string $credentialFile = '', $destinationPath = '') |
82
|
|
|
{ |
83
|
76 |
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
84
|
76 |
|
switch (strtolower($this->getDumperClassName())) { |
85
|
76 |
|
case 'mysqldumper': |
86
|
28 |
|
$dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath); |
|
|
|
|
87
|
28 |
|
break; |
88
|
|
|
default: |
89
|
48 |
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
90
|
48 |
|
break; |
91
|
|
|
} |
92
|
|
|
|
93
|
76 |
|
return $this->removeExtraSpaces($dumpCommand); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
32 |
|
public function getRestoreCommand(string $credentialFile = '', string $filePath = '') |
97
|
|
|
{ |
98
|
32 |
|
$filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath; |
99
|
32 |
|
switch (strtolower($this->getDumperClassName())) { |
100
|
32 |
|
case 'mysqldumper': |
101
|
6 |
|
$restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath); |
|
|
|
|
102
|
6 |
|
break; |
103
|
|
|
default: |
104
|
26 |
|
$restoreCommand = $this->prepareRestoreCommand($filePath); |
105
|
26 |
|
break; |
106
|
|
|
} |
107
|
|
|
|
108
|
32 |
|
return $this->removeExtraSpaces($restoreCommand); |
109
|
|
|
} |
110
|
|
|
|
111
|
108 |
|
public function getDumperClassName() |
112
|
|
|
{ |
113
|
108 |
|
$classWithNamespace = static::class; |
114
|
108 |
|
$partials = explode("\\", $classWithNamespace); |
115
|
108 |
|
$className = end($partials); |
116
|
108 |
|
return $className; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|