|
1
|
|
|
<?php |
|
2
|
|
|
namespace Robo; |
|
3
|
|
|
|
|
4
|
|
|
use Consolidation\AnnotatedCommand\ExitCodeInterface; |
|
5
|
|
|
use Consolidation\AnnotatedCommand\OutputDataInterface; |
|
6
|
|
|
|
|
7
|
|
|
class ResultData extends \ArrayObject implements ExitCodeInterface, OutputDataInterface |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* @var int |
|
11
|
|
|
*/ |
|
12
|
|
|
protected $exitCode; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $message; |
|
18
|
|
|
|
|
19
|
|
|
const EXITCODE_OK = 0; |
|
20
|
|
|
const EXITCODE_ERROR = 1; |
|
21
|
|
|
/** Symfony Console handles these conditions; Robo returns the status |
|
22
|
|
|
code selected by Symfony. These are here for documentation purposes. */ |
|
23
|
|
|
const EXITCODE_MISSING_OPTIONS = 2; |
|
24
|
|
|
const EXITCODE_COMMAND_NOT_FOUND = 127; |
|
25
|
|
|
|
|
26
|
|
|
/** The command was aborted because the user chose to cancel it at some prompt. |
|
27
|
|
|
This exit code is arbitrarily the same as EX_TEMPFAIL in sysexits.h, although |
|
28
|
|
|
note that shell error codes are distinct from C exit codes, so this alignment |
|
29
|
|
|
not particularly meaningful. */ |
|
30
|
|
|
const EXITCODE_USER_CANCEL = 75; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param int $exitCode |
|
34
|
|
|
* @param string $message |
|
35
|
|
|
* @param array $data |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __construct($exitCode = self::EXITCODE_OK, $message = '', $data = []) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->exitCode = $exitCode; |
|
40
|
|
|
$this->message = $message; |
|
41
|
|
|
|
|
42
|
|
|
parent::__construct($data); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param string $message |
|
47
|
|
|
* @param array $data |
|
48
|
|
|
* |
|
49
|
|
|
* @return \Robo\ResultData |
|
50
|
|
|
*/ |
|
51
|
|
|
public static function message($message, $data = []) |
|
52
|
|
|
{ |
|
53
|
|
|
return new self(self::EXITCODE_OK, $message, $data); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param string $message |
|
58
|
|
|
* @param array $data |
|
59
|
|
|
* |
|
60
|
|
|
* @return \Robo\ResultData |
|
61
|
|
|
*/ |
|
62
|
|
|
public static function cancelled($message = '', $data = []) |
|
63
|
|
|
{ |
|
64
|
|
|
return new ResultData(self::EXITCODE_USER_CANCEL, $message, $data); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getData() |
|
71
|
|
|
{ |
|
72
|
|
|
return $this->getArrayCopy(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @return int |
|
77
|
|
|
*/ |
|
78
|
|
|
public function getExitCode() |
|
79
|
|
|
{ |
|
80
|
|
|
return $this->exitCode; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return null|string |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getOutputData() |
|
87
|
|
|
{ |
|
88
|
|
|
if (!empty($this->message) && !isset($this['already-printed'])) { |
|
89
|
|
|
return $this->message; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getMessage() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->message; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return bool |
|
103
|
|
|
*/ |
|
104
|
|
|
public function wasSuccessful() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->exitCode === self::EXITCODE_OK; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @return bool |
|
111
|
|
|
*/ |
|
112
|
|
|
public function wasCancelled() |
|
113
|
|
|
{ |
|
114
|
|
|
return $this->exitCode == self::EXITCODE_USER_CANCEL; |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
/** |
|
118
|
|
|
* Merge another result into this result. Data already |
|
119
|
|
|
* existing in this result takes precedence over the |
|
120
|
|
|
* data in the Result being merged. |
|
121
|
|
|
* |
|
122
|
|
|
* @param \Robo\ResultData $result |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function merge(ResultData $result) |
|
127
|
|
|
{ |
|
128
|
|
|
$mergedData = $this->getArrayCopy() + $result->getArrayCopy(); |
|
129
|
|
|
$this->exchangeArray($mergedData); |
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Update the current data with the data provided in the parameter. |
|
135
|
|
|
* Provided data takes precedence. |
|
136
|
|
|
* |
|
137
|
|
|
* @param \ArrayObject $update |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function update(\ArrayObject $update) |
|
142
|
|
|
{ |
|
143
|
|
|
$iterator = $update->getIterator(); |
|
144
|
|
|
|
|
145
|
|
|
while ($iterator->valid()) { |
|
146
|
|
|
$this[$iterator->key()] = $iterator->current(); |
|
147
|
|
|
$iterator->next(); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Merge another result into this result. Data already |
|
155
|
|
|
* existing in this result takes precedence over the |
|
156
|
|
|
* data in the Result being merged. |
|
157
|
|
|
* |
|
158
|
|
|
* $data['message'] is handled specially, and is appended |
|
159
|
|
|
* to $this->message if set. |
|
160
|
|
|
* |
|
161
|
|
|
* @param array $data |
|
162
|
|
|
* |
|
163
|
|
|
* @return array |
|
164
|
|
|
*/ |
|
165
|
|
|
public function mergeData(array $data) |
|
166
|
|
|
{ |
|
167
|
|
|
$mergedData = $this->getArrayCopy() + $data; |
|
168
|
|
|
$this->exchangeArray($mergedData); |
|
169
|
|
|
return $mergedData; |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* @return bool |
|
174
|
|
|
*/ |
|
175
|
|
|
public function hasExecutionTime() |
|
176
|
|
|
{ |
|
177
|
|
|
return isset($this['time']); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* @return null|float |
|
182
|
|
|
*/ |
|
183
|
|
|
public function getExecutionTime() |
|
184
|
|
|
{ |
|
185
|
|
|
if (!$this->hasExecutionTime()) { |
|
186
|
|
|
return null; |
|
187
|
|
|
} |
|
188
|
|
|
return $this['time']; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Accumulate execution time |
|
193
|
|
|
*/ |
|
194
|
|
|
public function accumulateExecutionTime($duration) |
|
195
|
|
|
{ |
|
196
|
|
|
// Convert data arrays to scalar |
|
197
|
|
|
if (is_array($duration)) { |
|
198
|
|
|
$duration = isset($duration['time']) ? $duration['time'] : 0; |
|
199
|
|
|
} |
|
200
|
|
|
$this['time'] = $this->getExecutionTime() + $duration; |
|
201
|
|
|
return $this->getExecutionTime(); |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Accumulate the message. |
|
206
|
|
|
*/ |
|
207
|
|
|
public function accumulateMessage($message) |
|
208
|
|
|
{ |
|
209
|
|
|
if (!empty($this->message)) { |
|
210
|
|
|
$this->message .= "\n"; |
|
211
|
|
|
} |
|
212
|
|
|
$this->message .= $message; |
|
213
|
|
|
return $this->getMessage(); |
|
214
|
|
|
} |
|
215
|
|
|
} |
|
216
|
|
|
|