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; |
11
|
|
|
|
12
|
|
|
use RuntimeException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class CommandLine |
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 CommandLine implements Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* List of system commands to execute. |
26
|
|
|
* |
27
|
|
|
* @var \SebastianFeldmann\Cli\Command[] |
28
|
|
|
*/ |
29
|
|
|
private $commands = []; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Redirect the output |
33
|
|
|
* |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $redirectOutput; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Output pipeline |
40
|
|
|
* |
41
|
|
|
* @var \SebastianFeldmann\Cli\Command[] |
42
|
|
|
*/ |
43
|
|
|
private $pipeline = []; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* List of acceptable exit codes. |
47
|
|
|
* |
48
|
|
|
* @var array |
49
|
|
|
*/ |
50
|
|
|
private $acceptedExitCodes = [0]; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Set the list of accepted exit codes. |
54
|
|
|
* |
55
|
|
|
* @param int[] $codes |
56
|
|
|
*/ |
57
|
|
|
public function acceptExitCodes(array $codes) |
58
|
|
|
{ |
59
|
|
|
$this->acceptedExitCodes = $codes; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Redirect the stdOut. |
64
|
|
|
* |
65
|
|
|
* @param string $path |
66
|
|
|
*/ |
67
|
1 |
|
public function redirectOutputTo($path) |
68
|
|
|
{ |
69
|
1 |
|
$this->redirectOutput = $path; |
70
|
1 |
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Should the output be redirected. |
74
|
|
|
* |
75
|
|
|
* @return boolean |
76
|
|
|
*/ |
77
|
1 |
|
public function isOutputRedirected() |
78
|
|
|
{ |
79
|
1 |
|
return !empty($this->redirectOutput); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Redirect getter. |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
1 |
|
public function getRedirectPath() |
88
|
|
|
{ |
89
|
1 |
|
return $this->redirectOutput; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Pipe the command into given command. |
94
|
|
|
* |
95
|
|
|
* @param \SebastianFeldmann\Cli\Command $cmd |
96
|
|
|
*/ |
97
|
1 |
|
public function pipeOutputTo(Command $cmd) |
98
|
|
|
{ |
99
|
1 |
|
if (!$this->canPipe()) { |
100
|
|
|
throw new RuntimeException('Can\'t pipe output'); |
101
|
|
|
} |
102
|
1 |
|
$this->pipeline[] = $cmd; |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Can the pipe '|' operator be used. |
107
|
|
|
* |
108
|
|
|
* @return bool |
109
|
|
|
*/ |
110
|
1 |
|
public function canPipe() |
111
|
|
|
{ |
112
|
1 |
|
return !defined('PHP_WINDOWS_VERSION_BUILD'); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Is there a command pipeline. |
117
|
|
|
* |
118
|
|
|
* @return bool |
119
|
|
|
*/ |
120
|
3 |
|
public function isPiped() |
121
|
|
|
{ |
122
|
3 |
|
return !empty($this->pipeline); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Return command pipeline. |
127
|
|
|
* |
128
|
|
|
* @return string |
129
|
|
|
*/ |
130
|
3 |
|
public function getPipeline() |
131
|
|
|
{ |
132
|
3 |
|
return $this->isPiped() ? ' | ' . implode(' | ', $this->pipeline) : ''; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Adds a cli command to list of commands to execute. |
137
|
|
|
* |
138
|
|
|
* @param \SebastianFeldmann\Cli\Command |
139
|
|
|
*/ |
140
|
4 |
|
public function addCommand(Command $cmd) |
141
|
|
|
{ |
142
|
4 |
|
$this->commands[] = $cmd; |
143
|
4 |
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generates the system command. |
147
|
|
|
* |
148
|
|
|
* @return string |
149
|
|
|
*/ |
150
|
4 |
|
public function getCommand() : string |
151
|
|
|
{ |
152
|
4 |
|
$amount = count($this->commands); |
153
|
4 |
|
if ($amount < 1) { |
154
|
1 |
|
throw new RuntimeException('no command to execute'); |
155
|
|
|
} |
156
|
3 |
|
$cmd = ($amount > 1 ? '(' . implode(' && ', $this->commands) . ')' : $this->commands[0]) |
157
|
3 |
|
. $this->getPipeline() |
158
|
3 |
|
. (!empty($this->redirectOutput) ? ' > ' . $this->redirectOutput : ''); |
159
|
|
|
|
160
|
3 |
|
return $cmd; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Returns a list of exit codes that are valid. |
165
|
|
|
* |
166
|
|
|
* @return array |
167
|
|
|
*/ |
168
|
|
|
public function getAcceptableExitCodes() : array |
169
|
|
|
{ |
170
|
|
|
return $this->acceptedExitCodes; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Returns the command to execute. |
175
|
|
|
* |
176
|
|
|
* @return string |
177
|
|
|
*/ |
178
|
1 |
|
public function __toString() : string |
179
|
|
|
{ |
180
|
1 |
|
return $this->getCommand(); |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|