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
|
|
|
abstract public function dump(); |
32
|
|
|
abstract public function restore(); |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @return \Symfony\Component\Process\Process |
36
|
|
|
*/ |
37
|
|
|
protected function prepareProcessCommand() |
38
|
|
|
{ |
39
|
|
|
$process = Process::fromShellCommandline($this->command); |
40
|
|
|
$process->setTimeout($this->timeout); |
41
|
|
|
return $process; |
42
|
|
|
} |
43
|
|
|
/** |
44
|
|
|
* @return \Symfony\Component\Process\Process |
45
|
|
|
*/ |
46
|
|
|
protected function run() |
47
|
|
|
{ |
48
|
|
|
try { |
49
|
|
|
|
50
|
|
|
$process = Process::fromShellCommandline($this->command); |
51
|
|
|
$process->setTimeout($this->timeout); |
52
|
|
|
|
53
|
|
|
if ($this->debug) { |
54
|
|
|
return $process->mustRun(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $process->run(); |
|
|
|
|
58
|
|
|
|
59
|
|
|
} catch (ProcessFailedException $e) { |
60
|
|
|
throw new \Exception($e->getMessage()); |
61
|
|
|
|
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
76 |
|
public function getDumpCommand(string $credentialFile = '', $destinationPath = '') |
66
|
|
|
{ |
67
|
76 |
|
$destinationPath = !empty($destinationPath) ? $destinationPath : $this->destinationPath; |
68
|
76 |
|
switch (strtolower($this->getDumperClassName())) { |
69
|
76 |
|
case 'mysqldumper': |
70
|
28 |
|
$dumpCommand = $this->prepareDumpCommand($credentialFile, $destinationPath); |
|
|
|
|
71
|
28 |
|
break; |
72
|
|
|
default: |
73
|
48 |
|
$dumpCommand = $this->prepareDumpCommand($destinationPath); |
74
|
48 |
|
break; |
75
|
|
|
} |
76
|
|
|
|
77
|
76 |
|
return $this->removeExtraSpaces($dumpCommand); |
78
|
|
|
} |
79
|
|
|
|
80
|
32 |
|
public function getRestoreCommand(string $credentialFile = '', string $filePath = '') |
81
|
|
|
{ |
82
|
32 |
|
$filePath = !empty($filePath) ? '"' . $filePath : $this->restorePath; |
83
|
32 |
|
switch (strtolower($this->getDumperClassName())) { |
84
|
32 |
|
case 'mysqldumper': |
85
|
6 |
|
$restoreCommand = $this->prepareRestoreCommand($credentialFile, $filePath); |
|
|
|
|
86
|
6 |
|
break; |
87
|
|
|
default: |
88
|
26 |
|
$restoreCommand = $this->prepareRestoreCommand($filePath); |
89
|
26 |
|
break; |
90
|
|
|
} |
91
|
|
|
|
92
|
32 |
|
return $this->removeExtraSpaces($restoreCommand); |
93
|
|
|
} |
94
|
|
|
|
95
|
108 |
|
public function getDumperClassName() |
96
|
|
|
{ |
97
|
108 |
|
$classWithNamespace = static::class; |
98
|
108 |
|
$partials = explode("\\", $classWithNamespace); |
99
|
108 |
|
$className = end($partials); |
100
|
108 |
|
return $className; |
101
|
|
|
} |
102
|
|
|
|
103
|
108 |
|
public function removeExtraSpaces(string $str) |
104
|
|
|
{ |
105
|
108 |
|
return preg_replace('/\s+/', ' ', $str); |
106
|
|
|
} |
107
|
|
|
|
108
|
108 |
|
public static function isWindows() |
109
|
|
|
{ |
110
|
108 |
|
return strcasecmp(substr(PHP_OS, 0, 3), 'WIN') == 0 ? true : false; |
111
|
|
|
} |
112
|
|
|
|
113
|
108 |
|
public function quoteCommand(string $command) |
114
|
|
|
{ |
115
|
108 |
|
return static::isWindows() ? "\"{$command}\"" : "'{$command}'"; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|