|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (C) 2017 by TEQneers GmbH & Co. KG |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
6
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
7
|
|
|
* in the Software without restriction, including without limitation the rights |
|
8
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
9
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
10
|
|
|
* furnished to do so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in |
|
13
|
|
|
* all copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
|
21
|
|
|
* THE SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* Git Stream Wrapper for PHP |
|
26
|
|
|
* |
|
27
|
|
|
* @category TQ |
|
28
|
|
|
* @package TQ_VCS |
|
29
|
|
|
* @subpackage VCS |
|
30
|
|
|
* @copyright Copyright (C) 2018 by TEQneers GmbH & Co. KG |
|
31
|
|
|
*/ |
|
32
|
|
|
|
|
33
|
|
|
namespace TQ\Vcs\Cli; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* The result of a CLI call - provides access to stdout, stderror and the return code |
|
37
|
|
|
* |
|
38
|
|
|
* @author Stefan Gehrig <gehrigteqneers.de> |
|
39
|
|
|
* @category TQ |
|
40
|
|
|
* @package TQ_VCS |
|
41
|
|
|
* @subpackage VCS |
|
42
|
|
|
* @copyright Copyright (C) 2018 by TEQneers GmbH & Co. KG |
|
43
|
|
|
*/ |
|
44
|
|
|
class CallResult |
|
45
|
|
|
{ |
|
46
|
|
|
/** |
|
47
|
|
|
* The stdout stream |
|
48
|
|
|
* |
|
49
|
|
|
* @var resource |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $stdOut; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* True if there is a stdout |
|
55
|
|
|
* |
|
56
|
|
|
* @var boolean |
|
57
|
|
|
*/ |
|
58
|
|
|
protected $hasStdOut; |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* The stderr stream |
|
62
|
|
|
* |
|
63
|
|
|
* @var resource |
|
64
|
|
|
*/ |
|
65
|
|
|
protected $stdErr; |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* True if there is a stderr |
|
69
|
|
|
* |
|
70
|
|
|
* @var boolean |
|
71
|
|
|
*/ |
|
72
|
|
|
protected $hasStdErr; |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* The return code |
|
76
|
|
|
* |
|
77
|
|
|
* @var integer |
|
78
|
|
|
*/ |
|
79
|
|
|
protected $returnCode; |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Reference to the call that resulted in this result |
|
83
|
|
|
* |
|
84
|
|
|
* @var Call |
|
85
|
|
|
*/ |
|
86
|
|
|
protected $cliCall; |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Creates a new result container for a CLI call |
|
90
|
|
|
* |
|
91
|
|
|
* @param Call $cliCall Reference to the call that resulted in this result |
|
92
|
|
|
* @param resource $stdOut The stdout stream |
|
93
|
|
|
* @param resource $stdErr The stderr stream |
|
94
|
|
|
* @param integer $returnCode The return code |
|
95
|
|
|
*/ |
|
96
|
157 |
|
public function __construct(Call $cliCall, $stdOut, $stdErr, $returnCode) |
|
97
|
|
|
{ |
|
98
|
157 |
|
fseek($stdOut, 0, SEEK_END); |
|
99
|
157 |
|
$hasStdOut = (ftell($stdOut) > 0); |
|
100
|
157 |
|
fseek($stdOut, 0, SEEK_SET); |
|
101
|
|
|
|
|
102
|
157 |
|
fseek($stdErr, 0, SEEK_END); |
|
103
|
157 |
|
$hasStdErr = (ftell($stdErr) > 0); |
|
104
|
157 |
|
fseek($stdErr, 0, SEEK_SET); |
|
105
|
|
|
|
|
106
|
157 |
|
$this->cliCall = $cliCall; |
|
107
|
157 |
|
$this->stdOut = $stdOut; |
|
108
|
157 |
|
$this->hasStdOut = $hasStdOut; |
|
109
|
157 |
|
$this->stdErr = $stdErr; |
|
110
|
157 |
|
$this->hasStdErr = $hasStdErr; |
|
111
|
157 |
|
$this->returnCode = (int)$returnCode; |
|
112
|
157 |
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* Destructor closes the result and the internal stream resources |
|
116
|
|
|
*/ |
|
117
|
157 |
|
public function __destruct() |
|
118
|
|
|
{ |
|
119
|
157 |
|
$this->close(); |
|
120
|
157 |
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Returns the reference to the call that resulted in this result |
|
124
|
|
|
* |
|
125
|
|
|
* @return Call |
|
126
|
|
|
*/ |
|
127
|
2 |
|
public function getCliCall() |
|
128
|
|
|
{ |
|
129
|
2 |
|
return $this->cliCall; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* Returns the stdout stream |
|
134
|
|
|
* |
|
135
|
|
|
* @return resource |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getStdOutStream() |
|
138
|
|
|
{ |
|
139
|
|
|
return $this->stdOut; |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Returns the contents of stdout |
|
144
|
|
|
* |
|
145
|
|
|
* @return string |
|
146
|
|
|
*/ |
|
147
|
155 |
|
public function getStdOut() |
|
148
|
|
|
{ |
|
149
|
155 |
|
fseek($this->stdOut, 0, SEEK_SET); |
|
150
|
155 |
|
return rtrim(stream_get_contents($this->stdOut)); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Returns true if the call resulted in stdout to be populated |
|
155
|
|
|
* |
|
156
|
|
|
* @return boolean |
|
157
|
|
|
*/ |
|
158
|
4 |
|
public function hasStdOut() |
|
159
|
|
|
{ |
|
160
|
4 |
|
return $this->hasStdOut; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Returns the stderr stream |
|
165
|
|
|
* |
|
166
|
|
|
* @return resource |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getStdErrStream() |
|
169
|
|
|
{ |
|
170
|
|
|
return $this->stdErr; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* Returns the contents of stderr |
|
175
|
|
|
* |
|
176
|
|
|
* @return string |
|
177
|
|
|
*/ |
|
178
|
4 |
|
public function getStdErr() |
|
179
|
|
|
{ |
|
180
|
4 |
|
fseek($this->stdErr, 0, SEEK_SET); |
|
181
|
4 |
|
return rtrim(stream_get_contents($this->stdErr)); |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* Returns true if the call resulted in stderr to be populated |
|
186
|
|
|
* |
|
187
|
|
|
* @return boolean |
|
188
|
|
|
*/ |
|
189
|
4 |
|
public function hasStdErr() |
|
190
|
|
|
{ |
|
191
|
4 |
|
return $this->hasStdErr; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
/** |
|
195
|
|
|
* Returns the return code |
|
196
|
|
|
* |
|
197
|
|
|
* @return integer |
|
198
|
|
|
*/ |
|
199
|
157 |
|
public function getReturnCode() |
|
200
|
|
|
{ |
|
201
|
157 |
|
return $this->returnCode; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Closes the call result and the internal stream resources |
|
206
|
|
|
* |
|
207
|
|
|
* Prevents further usage |
|
208
|
|
|
*/ |
|
209
|
157 |
|
public function close() |
|
210
|
|
|
{ |
|
211
|
157 |
|
if ($this->stdOut !== null) { |
|
212
|
157 |
|
fclose($this->stdOut); |
|
213
|
157 |
|
$this->stdOut = null; |
|
214
|
|
|
} |
|
215
|
157 |
|
if ($this->stdErr !== null) { |
|
216
|
157 |
|
fclose($this->stdErr); |
|
217
|
157 |
|
$this->stdErr = null; |
|
218
|
|
|
} |
|
219
|
157 |
|
} |
|
220
|
|
|
|
|
221
|
|
|
/** |
|
222
|
|
|
* Checks if the CLI call has succeeded and throws an Exception otherwise |
|
223
|
|
|
* |
|
224
|
|
|
* @param string $message The exception message |
|
225
|
|
|
* @throws CallException If there has been an error when executing |
|
226
|
|
|
*/ |
|
227
|
153 |
|
public function assertSuccess($message) |
|
228
|
|
|
{ |
|
229
|
153 |
|
self::throwIfError($this, $message); |
|
230
|
153 |
|
} |
|
231
|
|
|
|
|
232
|
|
|
/** |
|
233
|
|
|
* Checks if the CLI call has succeeded and throws an Exception otherwise |
|
234
|
|
|
* |
|
235
|
|
|
* @param CallResult $result The CLI result |
|
236
|
|
|
* @param string $message The exception message |
|
237
|
|
|
* @throws CallException If there has been an error when executing |
|
238
|
|
|
*/ |
|
239
|
153 |
|
public static function throwIfError(CallResult $result, $message) |
|
240
|
|
|
{ |
|
241
|
153 |
|
if ($result->getReturnCode() > 0) { |
|
242
|
|
|
throw new CallException($message, $result); |
|
243
|
|
|
} |
|
244
|
153 |
|
} |
|
245
|
|
|
} |
|
246
|
|
|
|
|
247
|
|
|
|