1
|
|
|
<?php |
2
|
|
|
namespace LibSSH2; |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* Builder class. |
6
|
|
|
* |
7
|
|
|
* Create command line commands with options and arguments. |
8
|
|
|
* |
9
|
|
|
* @package LibSSH2 |
10
|
|
|
*/ |
11
|
|
|
class Builder |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Command prefix. |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
public $prefix; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Command options. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
public $options; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Command arguments. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
public $arguments; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Returns $command as string. |
37
|
|
|
* |
38
|
|
|
* @return string $command command line string |
39
|
|
|
*/ |
40
|
|
|
public function __toString() |
41
|
|
|
{ |
42
|
|
|
$command = str_pad($this->prefix, strlen($this->prefix) + 1, ' ', STR_PAD_RIGHT); |
43
|
|
|
$command .= !empty($this->options) ? str_pad($this->options, strlen($this->options) + 1, ' ', STR_PAD_RIGHT) : ''; |
44
|
|
|
$command .= !empty($this->arguments) ? str_pad($this->arguments, strlen($this->arguments) + 1, ' ', STR_PAD_RIGHT) : ''; |
45
|
|
|
return $command; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Set command line prefix. |
50
|
|
|
* |
51
|
|
|
* @param string $prefix command line prefix |
52
|
|
|
* @return object class object |
53
|
|
|
*/ |
54
|
|
|
final public function setPrefix($prefix) |
55
|
|
|
{ |
56
|
|
|
$this->prefix = $prefix; |
57
|
|
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Set command line options |
62
|
|
|
* |
63
|
|
|
* @param array $opts command line options |
64
|
|
|
* @return object class object |
65
|
|
|
*/ |
66
|
|
|
final public function setOptions(array $opts) |
67
|
|
|
{ |
68
|
|
|
$options = []; |
69
|
|
|
foreach ($opts as $key => $value) |
70
|
|
|
{ |
71
|
|
|
if (preg_match('/^-.*$/', $key, $matches)) |
72
|
|
|
{ |
73
|
|
|
$options[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value; |
74
|
|
|
} |
75
|
|
|
if (preg_match('/^-.*$/', $value, $matches)) |
76
|
|
|
{ |
77
|
|
|
$options[] = $value; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
$this->options = implode(' ', array_filter($options, 'strlen')); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Set command line arguments |
86
|
|
|
* |
87
|
|
|
* @param array $args command line arguments |
88
|
|
|
* @return object class object |
89
|
|
|
*/ |
90
|
|
|
final public function setArguments(array $args) |
91
|
|
|
{ |
92
|
|
|
$arguments = []; |
93
|
|
|
foreach ($args as $key => $value) |
94
|
|
|
{ |
95
|
|
|
if (preg_match('/^-.*$/', $key, $matches)) |
96
|
|
|
{ |
97
|
|
|
$arguments[] = is_bool($value) ? $key : str_pad($key, strlen($key) + 1, ' ', STR_PAD_RIGHT) . $value; |
98
|
|
|
} |
99
|
|
|
else |
100
|
|
|
{ |
101
|
|
|
$arguments[] = is_array($value) ? implode(' ', $value) : $value; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
$this->arguments = implode(' ', array_filter($arguments, 'strlen')); |
105
|
|
|
return $this; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|