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
|
|
|
* Merge another result into this result. Data already |
135
|
|
|
* existing in this result takes precedence over the |
136
|
|
|
* data in the Result being merged. |
137
|
|
|
* |
138
|
|
|
* $data['message'] is handled specially, and is appended |
139
|
|
|
* to $this->message if set. |
140
|
|
|
* |
141
|
|
|
* @param array $data |
142
|
|
|
* |
143
|
|
|
* @return array |
144
|
|
|
*/ |
145
|
|
|
public function mergeData(array $data) |
146
|
|
|
{ |
147
|
|
|
$mergedData = $this->getArrayCopy() + $data; |
148
|
|
|
$this->exchangeArray($mergedData); |
149
|
|
|
return $mergedData; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function hasExecutionTime() |
156
|
|
|
{ |
157
|
|
|
return isset($this['time']); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @return null|float |
162
|
|
|
*/ |
163
|
|
|
public function getExecutionTime() |
164
|
|
|
{ |
165
|
|
|
if (!$this->hasExecutionTime()) { |
166
|
|
|
return null; |
167
|
|
|
} |
168
|
|
|
return $this['time']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Accumulate execution time |
173
|
|
|
*/ |
174
|
|
|
public function accumulateExecutionTime($duration) |
175
|
|
|
{ |
176
|
|
|
// Convert data arrays to scalar |
177
|
|
|
if (is_array($duration)) { |
178
|
|
|
$duration = isset($duration['time']) ? $duration['time'] : 0; |
179
|
|
|
} |
180
|
|
|
$this['time'] = $this->getExecutionTime() + $duration; |
181
|
|
|
return $this->getExecutionTime(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Accumulate the message. |
186
|
|
|
*/ |
187
|
|
|
public function accumulateMessage($message) |
188
|
|
|
{ |
189
|
|
|
if (!empty($this->message)) { |
190
|
|
|
$this->message .= "\n"; |
191
|
|
|
} |
192
|
|
|
$this->message .= $message; |
193
|
|
|
return $this->getMessage(); |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
|