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