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\Output\Util as OutputUtil; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Result |
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 Result |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* Command that got executed. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $cmd; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Result code. |
35
|
|
|
* |
36
|
|
|
* @var int |
37
|
|
|
*/ |
38
|
|
|
private $code; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* List of valid exit codes. |
42
|
|
|
* |
43
|
|
|
* @var int[] |
44
|
|
|
*/ |
45
|
|
|
private $validExitCodes; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Output buffer. |
49
|
|
|
* |
50
|
|
|
* @var array |
51
|
|
|
*/ |
52
|
|
|
private $buffer; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* StdOut. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
private $stdOut; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* StdErr. |
63
|
|
|
* |
64
|
|
|
* @var string |
65
|
|
|
*/ |
66
|
|
|
private $stdErr; |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Path where the output is redirected to. |
70
|
|
|
* |
71
|
|
|
* @var string |
72
|
|
|
*/ |
73
|
|
|
private $redirectPath; |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Result constructor. |
77
|
|
|
* |
78
|
|
|
* @param string $cmd |
79
|
|
|
* @param int $code |
80
|
|
|
* @param string $stdOut |
81
|
|
|
* @param string $stdErr |
82
|
|
|
* @param string $redirectPath |
83
|
30 |
|
* @param int[] $validExitCodes |
84
|
|
|
*/ |
85
|
|
|
public function __construct( |
86
|
|
|
string $cmd, |
87
|
|
|
int $code, |
88
|
|
|
string $stdOut = '', |
89
|
|
|
string $stdErr = '', |
90
|
|
|
string $redirectPath = '', |
91
|
|
|
array $validExitCodes = [0] |
92
|
30 |
|
) { |
93
|
30 |
|
$this->cmd = $cmd; |
94
|
30 |
|
$this->code = $code; |
95
|
30 |
|
$this->stdOut = $stdOut; |
96
|
30 |
|
$this->stdErr = $stdErr; |
97
|
30 |
|
$this->redirectPath = $redirectPath; |
98
|
30 |
|
$this->validExitCodes = $validExitCodes; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Cmd getter. |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
2 |
|
*/ |
106
|
|
|
public function getCmd(): string |
107
|
2 |
|
{ |
108
|
|
|
return $this->cmd; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Code getter. |
113
|
|
|
* |
114
|
|
|
* @return int |
115
|
5 |
|
*/ |
116
|
|
|
public function getCode(): int |
117
|
5 |
|
{ |
118
|
|
|
return $this->code; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Command executed successful. |
123
|
6 |
|
*/ |
124
|
|
|
public function isSuccessful(): bool |
125
|
6 |
|
{ |
126
|
|
|
return in_array($this->code, $this->validExitCodes); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* StdOutput getter. |
131
|
|
|
* |
132
|
|
|
* @return string |
133
|
4 |
|
*/ |
134
|
|
|
public function getStdOut(): string |
135
|
4 |
|
{ |
136
|
|
|
return $this->stdOut; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* StdError getter. |
141
|
|
|
* |
142
|
|
|
* @return string |
143
|
5 |
|
*/ |
144
|
|
|
public function getStdErr(): string |
145
|
5 |
|
{ |
146
|
|
|
return $this->stdErr; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Is the output redirected to a file. |
151
|
|
|
* |
152
|
|
|
* @return bool |
153
|
4 |
|
*/ |
154
|
|
|
public function isOutputRedirected(): bool |
155
|
4 |
|
{ |
156
|
|
|
return !empty($this->redirectPath); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return path to the file where the output is redirected to. |
161
|
|
|
* |
162
|
|
|
* @return string |
163
|
4 |
|
*/ |
164
|
|
|
public function getRedirectPath(): string |
165
|
4 |
|
{ |
166
|
|
|
return $this->redirectPath; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* Return the output as array. |
171
|
|
|
* |
172
|
|
|
* @return array |
173
|
4 |
|
*/ |
174
|
|
|
public function getStdOutAsArray(): array |
175
|
4 |
|
{ |
176
|
4 |
|
if (null === $this->buffer) { |
177
|
|
|
$this->buffer = $this->textToBuffer(); |
178
|
4 |
|
} |
179
|
|
|
return $this->buffer; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Converts a string into an array. |
184
|
|
|
* |
185
|
|
|
* @return array |
186
|
4 |
|
*/ |
187
|
|
|
private function textToBuffer(): array |
188
|
4 |
|
{ |
189
|
|
|
return OutputUtil::trimEmptyLines(explode("\n", OutputUtil::normalizeLineEndings($this->stdOut))); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Magic to string method. |
194
|
|
|
* |
195
|
|
|
* @return string |
196
|
1 |
|
*/ |
197
|
|
|
public function __toString(): string |
198
|
1 |
|
{ |
199
|
|
|
return $this->stdOut; |
200
|
|
|
} |
201
|
|
|
} |
202
|
|
|
|