1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of rsync-lib |
5
|
|
|
* |
6
|
|
|
* (c) Alberto Fernández <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read |
9
|
|
|
* the LICENSE file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace AFM\Rsync; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Command abstraction class, construct commands |
16
|
|
|
* using arguments options and parameters |
17
|
|
|
* |
18
|
|
|
* Command format: |
19
|
|
|
* <pre> |
20
|
|
|
* [executable] [-abLs](options) [-a value](argument) [--test value](argument) [parameter1] ... [parameterN] |
21
|
|
|
* </pre> |
22
|
|
|
* |
23
|
|
|
* @author Alberto <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class Command |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $executable; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
private $options = array(); |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $arguments = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var string |
44
|
|
|
*/ |
45
|
|
|
private $command; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $parameters = array(); |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Every command must have an executable |
54
|
|
|
* |
55
|
|
|
* @param $executable |
56
|
|
|
*/ |
57
|
|
|
public function __construct($executable) |
58
|
|
|
{ |
59
|
|
|
$this->executable = $executable; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Adds a parameter to the command, will be appended |
64
|
|
|
* in the same order as insertion at the end |
65
|
|
|
* |
66
|
|
|
* @param $parameter |
67
|
|
|
*/ |
68
|
|
|
public function addParameter($parameter) |
69
|
|
|
{ |
70
|
|
|
$this->parameters[] = $parameter; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Adds an option to the command, will be |
75
|
|
|
* appended to the command in the next format: |
76
|
|
|
* |
77
|
|
|
* <pre> |
78
|
|
|
* -aBLs |
79
|
|
|
* </pre> |
80
|
|
|
* |
81
|
|
|
* @param $option |
82
|
|
|
*/ |
83
|
|
|
public function addOption($option) |
84
|
|
|
{ |
85
|
|
|
$this->options[] = $option; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Adds an argument to the command. If the argument |
90
|
|
|
* is more than one letter, "-- " will be appended before |
91
|
|
|
* if not, it will act as an option with a value: |
92
|
|
|
* |
93
|
|
|
* <pre> |
94
|
|
|
* --argument [value] |
95
|
|
|
* -p [value] |
96
|
|
|
* </pre> |
97
|
|
|
* |
98
|
|
|
* @param $name |
99
|
|
|
* @param bool|mixed $value |
100
|
|
|
*/ |
101
|
|
|
public function addArgument($name, $value = true) |
102
|
|
|
{ |
103
|
|
|
$this->arguments[$name][] = $value; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* @param $executable |
108
|
|
|
*/ |
109
|
|
|
public function setExecutable($executable) |
110
|
|
|
{ |
111
|
|
|
$this->executable = $executable; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
|
|
public function getExecutable() |
118
|
|
|
{ |
119
|
|
|
return $this->executable; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Constructs the command appendind options, |
124
|
|
|
* arguments, executable and parameters |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected function constructCommand() |
129
|
|
|
{ |
130
|
|
|
$command = array(); |
131
|
|
|
$command[] = $this->executable; |
132
|
|
|
|
133
|
|
|
if (!empty($this->options)) { |
134
|
|
|
$command[] = "-".implode($this->options); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
foreach ($this->arguments as $argument => $values) { |
138
|
|
|
foreach ($values as $value) { |
139
|
|
|
if (strlen($argument) == 1) { |
140
|
|
|
$command[] = "-".$argument." '".$value."'"; |
141
|
|
|
} else { |
142
|
|
|
$command[] = "--".(is_string($value) || is_int($value) ? $argument." '".$value."'" : $argument); |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
if (!empty($this->parameters)) { |
148
|
|
|
$command[] = implode(" ", $this->parameters); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
$stringCommand = implode(" ", $command); |
152
|
|
|
|
153
|
|
|
return $stringCommand; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Gets the command string |
158
|
|
|
* |
159
|
|
|
* @return mixed |
160
|
|
|
*/ |
161
|
|
|
public function getCommand() |
162
|
|
|
{ |
163
|
|
|
if (is_null($this->command)) { |
164
|
|
|
$this->command = $this->constructCommand(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
return $this->command; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* @see getCommand |
172
|
|
|
* @return mixed |
173
|
|
|
*/ |
174
|
|
|
public function __toString() |
175
|
|
|
{ |
176
|
|
|
return $this->getCommand(); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Execute command, with optional output printer |
181
|
|
|
* |
182
|
|
|
* @param bool $showOutput |
183
|
|
|
*/ |
184
|
|
|
public function execute($showOutput = false) |
185
|
|
|
{ |
186
|
|
|
$this->getCommand(); |
187
|
|
|
|
188
|
|
|
if ($showOutput) { |
189
|
|
|
$this->executeWithOutput(); |
190
|
|
|
} else { |
191
|
|
|
shell_exec($this->command); |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Execute and buffers command result to print it |
197
|
|
|
* |
198
|
|
|
* @throws \InvalidArgumentException When the command couldn't be executed |
199
|
|
|
*/ |
200
|
|
|
private function executeWithOutput() |
201
|
|
|
{ |
202
|
|
|
if (($fp = popen($this->command, "r"))) { |
203
|
|
|
while (!feof($fp)) { |
204
|
|
|
echo fread($fp, 1024); |
205
|
|
|
flush(); |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
fclose($fp); |
209
|
|
|
} else { |
210
|
|
|
throw new \InvalidArgumentException("Cannot execute command: '".$this->command."'"); |
211
|
|
|
} |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|