forked from consolidation/robo
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Result.php
162 lines (141 loc) · 4.88 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
<?php
namespace Robo;
use Robo\Contract\TaskInterface;
use Robo\Contract\LogResultInterface;
use Robo\Exception\TaskExitException;
class Result extends ResultData
{
public static $stopOnFail = false;
protected $task;
public function __construct(TaskInterface $task, $exitCode, $message = '', $data = [])
{
parent::__construct($exitCode, $message, $data);
$this->task = $task;
$this->printResult();
if (self::$stopOnFail) {
$this->stopOnFail();
}
}
protected function printResult()
{
// For historic reasons, the Result constructor is responsible
// for printing task results.
// TODO: Make IO the responsibility of some other class. Maintaining
// existing behavior for backwards compatibility. This is undesirable
// in the long run, though, as it can result in unwanted repeated input
// in task collections et. al.
$resultPrinter = Robo::resultPrinter();
if ($resultPrinter) {
if ($resultPrinter->printResult($this)) {
$this->data['already-printed'] = true;
}
}
}
public static function errorMissingExtension(TaskInterface $task, $extension, $service)
{
$messageTpl = 'PHP extension required for %s. Please enable %s';
$message = sprintf($messageTpl, $service, $extension);
return self::error($task, $message);
}
public static function errorMissingPackage(TaskInterface $task, $class, $package)
{
$messageTpl = 'Class %s not found. Please install %s Composer package';
$message = sprintf($messageTpl, $class, $package);
return self::error($task, $message);
}
public static function error(TaskInterface $task, $message, $data = [])
{
return new self($task, self::EXITCODE_ERROR, $message, $data);
}
public static function fromException(TaskInterface $task, \Exception $e, $data = [])
{
$exitCode = $e->getCode();
if (!$exitCode) {
$exitCode = self::EXITCODE_ERROR;
}
return new self($task, $exitCode, $e->getMessage(), $data);
}
public static function success(TaskInterface $task, $message = '', $data = [])
{
return new self($task, self::EXITCODE_OK, $message, $data);
}
/**
* Return a context useful for logging messages.
*/
public function getContext()
{
$task = $this->getTask();
return TaskInfo::getTaskContext($task) + [
'code' => $this->getExitCode(),
'data' => $this->getArrayCopy(),
'time' => $this->getExecutionTime(),
'message' => $this->getMessage(),
];
}
/**
* Add the results from the most recent task to the accumulated
* results from all tasks that have run so far, merging data
* as necessary.
*/
public function accumulate($key, Result $taskResult)
{
// If the task is unnamed, then all of its data elements
// just get merged in at the top-level of the final Result object.
if (static::isUnnamed($key)) {
$this->merge($taskResult);
} elseif (isset($this[$key])) {
// There can only be one task with a given name; however, if
// there are tasks added 'before' or 'after' the named task,
// then the results from these will be stored under the same
// name unless they are given a name of their own when added.
$current = $this[$key];
$this[$key] = $taskResult->merge($current);
} else {
$this[$key] = $taskResult;
}
}
/**
* We assume that named values (e.g. for associative array keys)
* are non-numeric; numeric keys are presumed to simply be the
* index of an array, and therefore insignificant.
*/
public static function isUnnamed($key)
{
return is_numeric($key);
}
/**
* @return TaskInterface
*/
public function getTask()
{
return $this->task;
}
public function cloneTask()
{
$reflect = new \ReflectionClass(get_class($this->task));
return $reflect->newInstanceArgs(func_get_args());
}
/**
* @deprecated since 1.0. @see wasSuccessful()
*/
public function __invoke()
{
trigger_error(__METHOD__ . ' is deprecated: use wasSuccessful() instead.', E_USER_DEPRECATED);
return $this->wasSuccessful();
}
public function stopOnFail()
{
if (!$this->wasSuccessful()) {
$resultPrinter = Robo::resultPrinter();
if ($resultPrinter) {
$resultPrinter->printStopOnFail($this);
}
$this->exitEarly($this->getExitCode());
}
return $this;
}
private function exitEarly($status)
{
throw new TaskExitException($this->getTask(), $this->getMessage(), $status);
}
}