1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of SebastianFeldmann\Cli. |
5
|
|
|
* |
6
|
|
|
* (c) Sebastian Feldmann <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace SebastianFeldmann\Cli\Command; |
13
|
|
|
|
14
|
|
|
use SebastianFeldmann\Cli\Command; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Executable |
18
|
|
|
* |
19
|
|
|
* @package SebastianFeldmann\Cli |
20
|
|
|
* @author Sebastian Feldmann <[email protected]> |
21
|
|
|
* @link https://github.com/sebastianfeldmann/cli |
22
|
|
|
* @since Class available since Release 0.9.0 |
23
|
|
|
*/ |
24
|
|
|
class Executable implements Command |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Command name |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $cmd; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Display stdErr |
35
|
|
|
* |
36
|
|
|
* @var boolean |
37
|
|
|
*/ |
38
|
|
|
private $isSilent = false; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Command options |
42
|
|
|
* |
43
|
|
|
* @var string[] |
44
|
|
|
*/ |
45
|
|
|
private $options = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* List of acceptable exit codes. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $acceptableExitCodes = []; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Constructor. |
56
|
|
|
* |
57
|
|
|
* @param string $cmd |
58
|
12 |
|
* @param int[] $exitCodes |
59
|
|
|
*/ |
60
|
12 |
|
public function __construct(string $cmd, array $exitCodes = [0]) |
61
|
12 |
|
{ |
62
|
12 |
|
$this->cmd = $cmd; |
63
|
|
|
$this->acceptableExitCodes = $exitCodes; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Returns the string to execute on the command line. |
68
|
|
|
* |
69
|
10 |
|
* @return string |
70
|
|
|
*/ |
71
|
10 |
|
public function getCommand(): string |
72
|
10 |
|
{ |
73
|
10 |
|
return $this->cmd |
74
|
|
|
. (count($this->options) ? ' ' . implode(' ', $this->options) : '') |
75
|
|
|
. ($this->isSilent ? ' 2> /dev/null' : ''); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns a list of exit codes that are valid. |
80
|
|
|
* |
81
|
1 |
|
* @return int[] |
82
|
|
|
*/ |
83
|
1 |
|
public function getAcceptableExitCodes(): array |
84
|
|
|
{ |
85
|
|
|
return $this->acceptableExitCodes; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Silence the 'Cmd' by redirecting its stdErr output to /dev/null. |
90
|
|
|
* The silence feature is disabled for Windows systems. |
91
|
|
|
* |
92
|
|
|
* @param bool $bool |
93
|
1 |
|
* @return \SebastianFeldmann\Cli\Command\Executable |
94
|
|
|
*/ |
95
|
1 |
|
public function silence($bool = true): Executable |
96
|
1 |
|
{ |
97
|
|
|
$this->isSilent = $bool && !defined('PHP_WINDOWS_VERSION_BUILD'); |
98
|
|
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Add option to list. |
103
|
|
|
* |
104
|
|
|
* @param string $option |
105
|
|
|
* @param mixed $value |
106
|
|
|
* @param string $glue |
107
|
3 |
|
* @return \SebastianFeldmann\Cli\Command\Executable |
108
|
|
|
*/ |
109
|
3 |
|
public function addOption(string $option, $value = null, string $glue = '='): Executable |
110
|
|
|
{ |
111
|
2 |
|
if ($value !== null) { |
112
|
1 |
|
// force space for multiple arguments e.g. --option 'foo' 'bar' |
113
|
|
|
if (is_array($value)) { |
114
|
2 |
|
$glue = ' '; |
115
|
|
|
} |
116
|
1 |
|
$value = $glue . $this->escapeArgument($value); |
117
|
|
|
} else { |
118
|
3 |
|
$value = ''; |
119
|
|
|
} |
120
|
3 |
|
$this->options[] = $option . $value; |
121
|
|
|
|
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Adds an option to a command if it is not empty. |
127
|
|
|
* |
128
|
|
|
* @param string $option |
129
|
|
|
* @param mixed $check |
130
|
|
|
* @param bool $asValue |
131
|
|
|
* @param string $glue |
132
|
2 |
|
* @return \SebastianFeldmann\Cli\Command\Executable |
133
|
|
|
*/ |
134
|
2 |
|
public function addOptionIfNotEmpty(string $option, $check, bool $asValue = true, string $glue = '='): Executable |
135
|
2 |
|
{ |
136
|
1 |
|
if (!empty($check)) { |
137
|
|
|
if ($asValue) { |
138
|
1 |
|
$this->addOption($option, $check, $glue); |
139
|
|
|
} else { |
140
|
|
|
$this->addOption($option); |
141
|
2 |
|
} |
142
|
|
|
} |
143
|
|
|
return $this; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Add argument to list. |
148
|
|
|
* |
149
|
|
|
* @param mixed $argument |
150
|
6 |
|
* @return \SebastianFeldmann\Cli\Command\Executable |
151
|
|
|
*/ |
152
|
6 |
|
public function addArgument($argument): Executable |
153
|
6 |
|
{ |
154
|
|
|
$this->options[] = $this->escapeArgument($argument); |
155
|
|
|
return $this; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Escape a shell argument. |
160
|
|
|
* |
161
|
|
|
* @param mixed $argument |
162
|
8 |
|
* @return string |
163
|
|
|
*/ |
164
|
8 |
|
protected function escapeArgument($argument): string |
165
|
2 |
|
{ |
166
|
2 |
|
if (is_array($argument)) { |
167
|
|
|
$argument = array_map('escapeshellarg', $argument); |
168
|
6 |
|
$escaped = implode(' ', $argument); |
169
|
|
|
} else { |
170
|
8 |
|
$escaped = escapeshellarg($argument); |
171
|
|
|
} |
172
|
|
|
return $escaped; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Returns the command to execute. |
177
|
|
|
* |
178
|
10 |
|
* @return string |
179
|
|
|
*/ |
180
|
10 |
|
public function __toString(): string |
181
|
|
|
{ |
182
|
|
|
return $this->getCommand(); |
183
|
|
|
} |
184
|
|
|
} |
185
|
|
|
|