1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace CodexShaper\Dumper; |
4
|
|
|
|
5
|
|
|
use CodexShaper\Dumper\Contracts\Dumper as DumperContract; |
6
|
|
|
use CodexShaper\Dumper\Traits\DumperTrait; |
7
|
|
|
use CodexShaper\Dumper\Traits\PrepareOptionsTrait; |
8
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException; |
9
|
|
|
use Symfony\Component\Process\Process; |
10
|
|
|
|
11
|
|
|
abstract class Dumper implements DumperContract |
12
|
|
|
{ |
13
|
|
|
use DumperTrait, PrepareOptionsTrait; |
|
|
|
|
14
|
|
|
|
15
|
118 |
|
public function __construct(array $options = []) |
16
|
|
|
{ |
17
|
118 |
|
foreach ($options as $option => $value) { |
18
|
|
|
if (property_exists($this, $option)) { |
19
|
|
|
$this->{$option} = $value; |
20
|
|
|
} |
21
|
|
|
} |
22
|
118 |
|
} |
23
|
|
|
/** |
24
|
|
|
* @return $this |
25
|
|
|
*/ |
26
|
118 |
|
public static function create(array $options = []) |
27
|
|
|
{ |
28
|
118 |
|
return new static($options); |
29
|
|
|
} |
30
|
|
|
/** |
31
|
|
|
* @return \Symfony\Component\Process\Process |
32
|
|
|
*/ |
33
|
|
|
protected function prepareProcessCommand() |
34
|
|
|
{ |
35
|
|
|
$process = Process::fromShellCommandline($this->command); |
36
|
|
|
$process->setTimeout($this->timeout); |
37
|
|
|
return $process; |
38
|
|
|
} |
39
|
|
|
/** |
40
|
|
|
* @return \Symfony\Component\Process\Process |
41
|
|
|
*/ |
42
|
|
|
protected function run() |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
|
46
|
|
|
$process = Process::fromShellCommandline($this->command); |
47
|
|
|
$process->setTimeout($this->timeout); |
48
|
|
|
|
49
|
|
|
if ($this->debug) { |
50
|
|
|
return $process->mustRun(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $process->run(); |
|
|
|
|
54
|
|
|
|
55
|
|
|
} catch (ProcessFailedException $e) { |
56
|
|
|
throw new \Exception($e->getMessage()); |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
abstract public function dump(); |
62
|
|
|
abstract public function restore(); |
63
|
|
|
|
64
|
76 |
|
public function getDumpCommand(string $credentialFile = '', $destinationPath = '') |
65
|
|
|
{ |
66
|
76 |
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
67
|
76 |
|
switch (strtolower($this->getDumperClassName())) { |
68
|
76 |
|
case 'mysqldumper': |
69
|
28 |
|
$dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath); |
|
|
|
|
70
|
28 |
|
break; |
71
|
|
|
default: |
72
|
48 |
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
73
|
48 |
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
76 |
|
return $this->removeExtraSpaces($dumpCommand); |
77
|
|
|
} |
78
|
|
|
|
79
|
32 |
|
public function getRestoreCommand(string $credentialFile = '', string $filePath = '') |
80
|
|
|
{ |
81
|
32 |
|
$filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath; |
82
|
32 |
|
switch (strtolower($this->getDumperClassName())) { |
83
|
32 |
|
case 'mysqldumper': |
84
|
6 |
|
$restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath); |
|
|
|
|
85
|
6 |
|
break; |
86
|
|
|
default: |
87
|
26 |
|
$restoreCommand = $this->prepareRestoreCommand($filePath); |
88
|
26 |
|
break; |
89
|
|
|
} |
90
|
|
|
|
91
|
32 |
|
return $this->removeExtraSpaces($restoreCommand); |
92
|
|
|
} |
93
|
|
|
|
94
|
108 |
|
public function getDumperClassName() |
95
|
|
|
{ |
96
|
108 |
|
$classWithNamespace = static::class; |
97
|
108 |
|
$partials = explode("\\", $classWithNamespace); |
98
|
108 |
|
$className = end($partials); |
99
|
108 |
|
return $className; |
100
|
|
|
} |
101
|
|
|
|
102
|
108 |
|
public function removeExtraSpaces(string $str) |
103
|
|
|
{ |
104
|
108 |
|
return preg_replace('/\s+/', ' ', $str); |
105
|
|
|
} |
106
|
|
|
|
107
|
108 |
|
public static function isWindows() |
108
|
|
|
{ |
109
|
108 |
|
return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false; |
110
|
|
|
} |
111
|
|
|
|
112
|
108 |
|
public function quoteCommand(string $command) |
113
|
|
|
{ |
114
|
108 |
|
return static::isWindows() ? "\"{$command}\"" : "'{$command}'"; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|