|
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
|
|
|
*/ |
|
17
|
|
|
class Result |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Command that got executed. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
private $cmd; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Result code. |
|
28
|
|
|
* |
|
29
|
|
|
* @var int |
|
30
|
|
|
*/ |
|
31
|
|
|
private $code; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Output buffer. |
|
35
|
|
|
* |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $buffer; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* StdOut. |
|
42
|
|
|
* |
|
43
|
|
|
* @var string |
|
44
|
|
|
*/ |
|
45
|
|
|
private $stdOut; |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* StdErr. |
|
49
|
|
|
* |
|
50
|
|
|
* @var string |
|
51
|
|
|
*/ |
|
52
|
|
|
private $stdErr; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Result constructor. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $cmd |
|
58
|
|
|
* @param int $code |
|
59
|
|
|
* @param string $stdOut |
|
60
|
|
|
* @param string $stdErr |
|
61
|
|
|
*/ |
|
62
|
17 |
|
public function __construct(string $cmd, int $code, string $stdOut = '', string $stdErr = '') |
|
63
|
|
|
{ |
|
64
|
17 |
|
$this->cmd = $cmd; |
|
65
|
17 |
|
$this->code = $code; |
|
66
|
17 |
|
$this->stdOut = $stdOut; |
|
67
|
17 |
|
$this->stdErr = $stdErr; |
|
68
|
17 |
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Cmd getter. |
|
72
|
|
|
* |
|
73
|
|
|
* @return string |
|
74
|
|
|
*/ |
|
75
|
1 |
|
public function getCmd() : string |
|
76
|
|
|
{ |
|
77
|
1 |
|
return $this->cmd; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Code getter. |
|
82
|
|
|
* |
|
83
|
|
|
* @return int |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function getCode() : int |
|
86
|
|
|
{ |
|
87
|
3 |
|
return $this->code; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Command executed successful. |
|
92
|
|
|
*/ |
|
93
|
5 |
|
public function isSuccessful() : bool |
|
94
|
|
|
{ |
|
95
|
5 |
|
return $this->code == 0; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* StdOutput getter. |
|
100
|
|
|
* |
|
101
|
|
|
* @return string |
|
102
|
|
|
*/ |
|
103
|
2 |
|
public function getStdOut() : string |
|
104
|
|
|
{ |
|
105
|
2 |
|
return $this->stdOut; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* StdError getter. |
|
110
|
|
|
* |
|
111
|
|
|
* @return string |
|
112
|
|
|
*/ |
|
113
|
2 |
|
public function getStdErr() : string |
|
114
|
|
|
{ |
|
115
|
2 |
|
return $this->stdErr; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Return the output as array. |
|
120
|
|
|
* |
|
121
|
|
|
* @return array |
|
122
|
|
|
*/ |
|
123
|
3 |
|
public function getStdOutAsArray() : array |
|
124
|
|
|
{ |
|
125
|
3 |
|
if (null === $this->buffer) { |
|
126
|
3 |
|
$this->buffer = $this->textToBuffer(); |
|
127
|
|
|
} |
|
128
|
3 |
|
return $this->buffer; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Converts a string into an array. |
|
133
|
|
|
* |
|
134
|
|
|
* @return array |
|
135
|
|
|
*/ |
|
136
|
3 |
|
private function textToBuffer() : array |
|
137
|
|
|
{ |
|
138
|
3 |
|
$buffer = explode(PHP_EOL, $this->stdOut); |
|
139
|
|
|
// remove empty array elements at the end |
|
140
|
3 |
|
$last = count($buffer) - 1; |
|
141
|
3 |
|
while (empty($buffer[$last])) { |
|
142
|
1 |
|
unset($buffer[$last]); |
|
143
|
1 |
|
$last--; |
|
144
|
|
|
} |
|
145
|
3 |
|
return $buffer; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* Magic to string method. |
|
150
|
|
|
* |
|
151
|
|
|
* @return string |
|
152
|
|
|
*/ |
|
153
|
1 |
|
public function __toString() |
|
154
|
|
|
{ |
|
155
|
1 |
|
return $this->stdOut; |
|
156
|
|
|
} |
|
157
|
|
|
} |
|
158
|
|
|
|