1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright (c) 2011-present Mediasift Ltd |
5
|
|
|
* All rights reserved. |
6
|
|
|
* |
7
|
|
|
* Redistribution and use in source and binary forms, with or without |
8
|
|
|
* modification, are permitted provided that the following conditions |
9
|
|
|
* are met: |
10
|
|
|
* |
11
|
|
|
* * Redistributions of source code must retain the above copyright |
12
|
|
|
* notice, this list of conditions and the following disclaimer. |
13
|
|
|
* |
14
|
|
|
* * Redistributions in binary form must reproduce the above copyright |
15
|
|
|
* notice, this list of conditions and the following disclaimer in |
16
|
|
|
* the documentation and/or other materials provided with the |
17
|
|
|
* distribution. |
18
|
|
|
* |
19
|
|
|
* * Neither the names of the copyright holders nor the names of his |
20
|
|
|
* contributors may be used to endorse or promote products derived |
21
|
|
|
* from this software without specific prior written permission. |
22
|
|
|
* |
23
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
24
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
25
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
26
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
27
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
28
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
29
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
30
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
31
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
32
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
33
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
34
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
35
|
|
|
* |
36
|
|
|
* @category Libraries |
37
|
|
|
* @package Storyplayer/Console |
38
|
|
|
* @author Stuart Herbert <[email protected]> |
39
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
40
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
41
|
|
|
* @link http://datasift.github.io/storyplayer |
42
|
|
|
*/ |
43
|
|
|
|
44
|
|
|
namespace DataSift\Storyplayer\Console; |
45
|
|
|
|
46
|
|
|
use DataSift\Storyplayer\OutputLib\CodeFormatter; |
47
|
|
|
use DataSift\Storyplayer\Phases\Phase; |
48
|
|
|
use DataSift\Storyplayer\PlayerLib\Phase_Result; |
49
|
|
|
use DataSift\Storyplayer\PlayerLib\PhaseGroup_Result; |
50
|
|
|
use DataSift\Storyplayer\PlayerLib\Story_Result; |
51
|
|
|
use DataSift\Storyplayer\PlayerLib\Story; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* the console plugin we use to show the user how each story is going |
55
|
|
|
* |
56
|
|
|
* @category Libraries |
57
|
|
|
* @package Storyplayer/Console |
58
|
|
|
* @author Stuart Herbert <[email protected]> |
59
|
|
|
* @copyright 2011-present Mediasift Ltd www.datasift.com |
60
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
61
|
|
|
* @link http://datasift.github.io/storyplayer |
62
|
|
|
*/ |
63
|
|
|
class UbLangConsole extends Console |
64
|
|
|
{ |
65
|
|
|
protected $currentPhase; |
66
|
|
|
protected $currentPhaseStep; |
67
|
|
|
protected $phaseGroupHasOutput = false; |
68
|
|
|
protected $phaseNumber = 0; |
69
|
|
|
protected $phaseMessages = array(); |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* a list of the results we have received from stories |
73
|
|
|
* @var array |
74
|
|
|
*/ |
75
|
|
|
protected $storyResults = []; |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* called when storyplayer starts |
79
|
|
|
* |
80
|
|
|
* @param string $version |
81
|
|
|
* @param string $url |
82
|
|
|
* @param string $copyright |
83
|
|
|
* @param string $license |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
|
|
public function startStoryplayer($version, $url, $copyright, $license) |
87
|
|
|
{ |
88
|
|
|
$this->write("Storyplayer {$version}", $this->writer->highlightStyle); |
89
|
|
|
$this->write(" - "); |
90
|
|
|
$this->write($url, $this->writer->urlStyle); |
91
|
|
|
$this->write(PHP_EOL); |
92
|
|
|
$this->write($copyright . PHP_EOL); |
93
|
|
|
$this->write($license . PHP_EOL . PHP_EOL); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function endStoryplayer($duration) |
97
|
|
|
{ |
98
|
|
|
$this->writeFinalReport($duration, true); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* called when we start a new set of phases |
103
|
|
|
* |
104
|
|
|
* @param string $activity |
105
|
|
|
* what are we doing? (e.g. 'creating', 'running') |
106
|
|
|
* @param string $name |
107
|
|
|
* the name of the phase group |
108
|
|
|
* @param array|null $details |
109
|
|
|
* optional explanation of what this PhaseGroup is trying |
110
|
|
|
* to achieve |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
|
|
public function startPhaseGroup($activity, $name, $details = null) |
114
|
|
|
{ |
115
|
|
|
$this->write($activity . ' ', $this->writer->activityStyle); |
116
|
|
|
$this->write($name, $this->writer->nameStyle); |
117
|
|
|
$this->write(' ...' . PHP_EOL, $this->writer->punctuationStyle); |
118
|
|
|
|
119
|
|
|
if (is_array($details)) { |
120
|
|
|
$this->write(' Scenario: ' . PHP_EOL, $this->writer->stepStyle); |
|
|
|
|
121
|
|
|
foreach ($details as $line) { |
122
|
|
|
$this->write(' ' . $line . PHP_EOL); |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
// reset our tracker of any output |
127
|
|
|
$this->phaseGroupHasOutput = false; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
View Code Duplication |
public function endPhaseGroup($result) |
131
|
|
|
{ |
132
|
|
|
// tell the user what happened |
133
|
|
|
$this->write(' Result: '); |
134
|
|
|
if ($result->getPhaseGroupSkipped()) { |
135
|
|
|
$this->writePhaseGroupSkipped($result->getResultString()); |
136
|
|
|
} |
137
|
|
|
else if ($result->getPhaseGroupSucceeded()) { |
138
|
|
|
$this->writePhaseGroupSucceeded($result->getResultString()); |
139
|
|
|
} |
140
|
|
|
else { |
141
|
|
|
$this->writePhaseGroupFailed($result->getResultString()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
// write out the duration too |
145
|
|
|
$this->write(' (', $this->writer->punctuationStyle); |
146
|
|
|
$this->writeDuration($result->getDuration()); |
147
|
|
|
$this->write(')' . PHP_EOL, $this->writer->punctuationStyle); |
148
|
|
|
|
149
|
|
|
// remember the result for the final report |
150
|
|
|
// |
151
|
|
|
// we have to clone as the result object apparently changes |
152
|
|
|
// afterwards. no idea why (yet) |
153
|
|
|
$this->results[] = clone $result; |
154
|
|
|
|
155
|
|
|
// if we are not connected to a terminal, we need to write out |
156
|
|
|
// a detailed error report |
157
|
|
|
if (!function_exists("posix_isatty") || !posix_isatty(STDOUT)) { |
158
|
|
|
$this->writeDetailedErrorReport($result); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* called when a story starts a new phase |
164
|
|
|
* |
165
|
|
|
* @return void |
166
|
|
|
*/ |
167
|
|
|
public function startPhase($phase) |
168
|
|
|
{ |
169
|
|
|
// shorthand |
170
|
|
|
$phaseType = $phase->getPhaseType(); |
171
|
|
|
$phaseSeqNo = $phase->getPhaseSequenceNo(); |
172
|
|
|
|
173
|
|
|
$this->currentPhaseType = $phaseType; |
|
|
|
|
174
|
|
|
|
175
|
|
|
// we're only interested in telling the user about the |
176
|
|
|
// phases of a story |
177
|
|
|
if ($phaseType !== Phase::STORY_PHASE) { |
178
|
|
|
return; |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
// remember the phase for later use |
182
|
|
|
$this->currentPhase = $phaseSeqNo; |
183
|
|
|
$this->currentPhaseName = $phase->getPhaseName(); |
|
|
|
|
184
|
|
|
$this->currentPhaseStep = 0; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* called when a story ends a phase |
189
|
|
|
* |
190
|
|
|
* @return void |
191
|
|
|
*/ |
192
|
|
|
public function endPhase($phase, $phaseResult) |
193
|
|
|
{ |
194
|
|
|
// shorthand |
195
|
|
|
$phaseType = $phase->getPhaseType(); |
196
|
|
|
|
197
|
|
|
// we're only interested in telling the user about the |
198
|
|
|
// phases of a story |
199
|
|
|
if ($phaseType !== Phase::STORY_PHASE) { |
200
|
|
|
return; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
// if there was no output for the phase, skip the report |
204
|
|
|
if ($this->currentPhaseStep === 0) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
if ($phaseResult->getPhaseFailed() || $phaseResult->getPhaseHasErrored()) { |
209
|
|
|
$this->writePhaseGroupFailed($phaseResult->getPhaseResultString()); |
210
|
|
|
} |
211
|
|
|
else if ($phaseResult->getPhaseIsIncomplete() || $phaseResult->getPhaseIsBlacklisted()) { |
212
|
|
|
$this->writePhaseGroupSkipped( $phaseResult->getPhaseResultString()); |
213
|
|
|
} |
214
|
|
|
$this->write(PHP_EOL); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* called when a story logs an action |
219
|
|
|
* |
220
|
|
|
* @param string $msg |
221
|
|
|
* @return void |
222
|
|
|
*/ |
223
|
|
|
public function logPhaseActivity($msg, $codeLine = null) |
224
|
|
|
{ |
225
|
|
|
// has output been suppressed? |
226
|
|
|
if ($this->isSilent()) { |
227
|
|
|
return; |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
// we only want Story phases |
231
|
|
|
if ($this->currentPhaseType !== Phase::STORY_PHASE) { |
|
|
|
|
232
|
|
|
return; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
// skip any empty messages (just in case) |
236
|
|
|
if (strlen($msg) === 0) { |
237
|
|
|
return; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
// we only want the top-level messages |
241
|
|
|
if ($msg{0} == ' ') { |
242
|
|
|
return; |
243
|
|
|
} |
244
|
|
|
|
245
|
|
|
// special case - first message for a phase group |
246
|
|
|
if (!$this->phaseGroupHasOutput && !$this->getIsVerbose()) { |
247
|
|
|
$this->write(' Steps:' . PHP_EOL, $this->writer->stepStyle); |
|
|
|
|
248
|
|
|
} |
249
|
|
|
|
250
|
|
|
// special case - first message for a phase |
251
|
|
|
if ($this->currentPhaseStep === 0 && $this->getIsVerbose()) { |
252
|
|
|
$this->write(' ' . $this->currentPhaseName . ':' . PHP_EOL, $this->writer->stepStyle); |
|
|
|
|
253
|
|
|
} |
254
|
|
|
// special case - not the first message for a phase |
255
|
|
|
if ($this->currentPhaseStep !== 0) { |
256
|
|
|
$this->write(PHP_EOL); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$this->write(' ' . $this->currentPhase . chr(ord('a') + $this->currentPhaseStep) . '. ', $this->writer->stepStyle); |
260
|
|
|
$this->write($msg . ' '); |
261
|
|
|
|
262
|
|
|
$this->currentPhaseStep++; |
263
|
|
|
$this->phaseGroupHasOutput = true; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* called when a story logs the (possibly partial) output from |
268
|
|
|
* running a subprocess |
269
|
|
|
* |
270
|
|
|
* @param string $msg the output to log |
271
|
|
|
* @return void |
272
|
|
|
*/ |
273
|
|
|
public function logPhaseSubprocessOutput($msg) |
274
|
|
|
{ |
275
|
|
|
// no-op? |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* called when a story logs an error |
280
|
|
|
* |
281
|
|
|
* @param string $phaseName |
282
|
|
|
* @param string $msg |
283
|
|
|
* @return void |
284
|
|
|
*/ |
285
|
|
|
public function logPhaseError($phaseName, $msg) |
286
|
|
|
{ |
287
|
|
|
// no-op? |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* called when a story is skipped |
292
|
|
|
* |
293
|
|
|
* @param string $phaseName |
294
|
|
|
* @param string $msg |
295
|
|
|
* @return void |
296
|
|
|
*/ |
297
|
|
|
public function logPhaseSkipped($phaseName, $msg) |
298
|
|
|
{ |
299
|
|
|
// no-op? |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
public function logPhaseCodeLine($codeLine) |
|
|
|
|
303
|
|
|
{ |
304
|
|
|
// this is a no-op for us |
305
|
|
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* called when the outer CLI shell encounters a fatal error |
309
|
|
|
* |
310
|
|
|
* @param string $msg |
311
|
|
|
* the error message to show the user |
312
|
|
|
* |
313
|
|
|
* @return void |
314
|
|
|
*/ |
315
|
|
|
public function logCliError($msg) |
316
|
|
|
{ |
317
|
|
|
$this->write("*** error: $msg" . PHP_EOL); |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
/** |
321
|
|
|
* |
322
|
|
|
* @param string $msg |
323
|
|
|
* @param Exception $e |
324
|
|
|
* @return void |
325
|
|
|
*/ |
326
|
|
|
public function logCliErrorWithException($msg, $e) |
327
|
|
|
{ |
328
|
|
|
$this->write("*** error: $msg" . PHP_EOL . PHP_EOL |
329
|
|
|
. "This was caused by an unexpected exception " . get_class($e) . PHP_EOL . PHP_EOL |
330
|
|
|
. $e->getTraceAsString() . PHP_EOL); |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* called when the outer CLI shell needs to publish a warning |
335
|
|
|
* |
336
|
|
|
* @param string $msg |
337
|
|
|
* the warning message to show the user |
338
|
|
|
* |
339
|
|
|
* @return void |
340
|
|
|
*/ |
341
|
|
|
public function logCliWarning($msg) |
342
|
|
|
{ |
343
|
|
|
$this->write("*** warning: $msg" . PHP_EOL); |
344
|
|
|
} |
345
|
|
|
|
346
|
|
|
/** |
347
|
|
|
* called when the outer CLI shell needs to tell the user something |
348
|
|
|
* |
349
|
|
|
* @param string $msg |
350
|
|
|
* the message to show the user |
351
|
|
|
* |
352
|
|
|
* @return void |
353
|
|
|
*/ |
354
|
|
|
public function logCliInfo($msg) |
355
|
|
|
{ |
356
|
|
|
$this->write($msg . PHP_EOL); |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* an alternative to using PHP's built-in var_dump() |
361
|
|
|
* |
362
|
|
|
* @param string $name |
363
|
|
|
* a human-readable name to describe $var |
364
|
|
|
* |
365
|
|
|
* @param mixed $var |
366
|
|
|
* the variable to dump |
367
|
|
|
* |
368
|
|
|
* @return void |
369
|
|
|
*/ |
370
|
|
|
public function logVardump($name, $var) |
371
|
|
|
{ |
372
|
|
|
// this is a no-op for us |
373
|
|
|
} |
374
|
|
|
} |
375
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.