1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* @author Tom Klingenberg <https://github.com/ktomk> |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
namespace N98\Magento\Command\Database; |
7
|
|
|
|
8
|
|
|
use N98\Magento\Command\Database\Compressor\AbstractCompressor; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* One or multiple commands to execute, with support for Compressors |
12
|
|
|
* |
13
|
|
|
* @package N98\Magento\Command\Database |
14
|
|
|
*/ |
15
|
|
|
class Execs |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
private $options = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private $execs = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var AbstractCompressor |
29
|
|
|
*/ |
30
|
|
|
private $compressor; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string|null |
34
|
|
|
*/ |
35
|
|
|
private $fileName; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Execs constructor. |
39
|
|
|
* |
40
|
|
|
* @param string $command [optional] |
|
|
|
|
41
|
|
|
*/ |
42
|
|
|
public function __construct($command = null) |
43
|
|
|
{ |
44
|
|
|
$this->options = (array) $command; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $type of compression: "gz" | "gzip" | "none" | null |
49
|
|
|
*/ |
50
|
|
|
public function setCompression($type) |
51
|
|
|
{ |
52
|
|
|
$this->compressor = AbstractCompressor::create($type); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return AbstractCompressor |
57
|
|
|
*/ |
58
|
|
|
public function getCompressor() |
59
|
|
|
{ |
60
|
|
|
if (!$this->compressor) { |
61
|
|
|
$this->setCompression(null); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
return $this->compressor; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param string $fileName output filename, will redirect mysqldump data into |
69
|
|
|
*/ |
70
|
|
|
public function setFileName($fileName) |
71
|
|
|
{ |
72
|
|
|
$this->fileName = $fileName; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return string|null |
77
|
|
|
*/ |
78
|
|
|
public function getFileName() |
79
|
|
|
{ |
80
|
|
|
return $this->fileName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $options |
85
|
|
|
*/ |
86
|
|
|
public function addOptions($options) |
87
|
|
|
{ |
88
|
|
|
$this->options[] = trim($options, ' '); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param string $options |
93
|
|
|
*/ |
94
|
|
|
public function add($options) |
95
|
|
|
{ |
96
|
|
|
$this->execs[] = $options; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @param string $separator |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
public function getBaseCommand($separator = '>') |
104
|
|
|
{ |
105
|
|
|
$command = $this->getCompressor()->getCompressingCommand( |
106
|
|
|
implode(' ', $this->options) |
107
|
|
|
); |
108
|
|
|
|
109
|
|
|
if (strlen($this->fileName)) { |
110
|
|
|
$command .= ' ' . $separator . ' ' . escapeshellarg($this->fileName); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $command; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @return array |
118
|
|
|
*/ |
119
|
|
|
public function getCommands() |
120
|
|
|
{ |
121
|
|
|
if (empty($this->execs)) { |
122
|
|
|
return [$this->getBaseCommand()]; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$commands = []; |
126
|
|
|
foreach ($this->execs as $exec) { |
127
|
|
|
$next = clone $this; |
128
|
|
|
$next->options[] = trim($exec); |
129
|
|
|
$commands[] = $next->getBaseCommand($commands ? '>>' : '>'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $commands; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check looks for
@param
annotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.