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