|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Honeybadger\HoneybadgerLaravel; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Illuminate\Console\OutputStyle; |
|
7
|
|
|
use Honeybadger\HoneybadgerLaravel\Exceptions\TaskFailed; |
|
8
|
|
|
|
|
9
|
|
|
class CommandTasks |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var \Illuminate\Console\OutputStyle |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $output; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $results = []; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $tasks = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @var bool |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $throwOnError = true; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Set command output. |
|
33
|
|
|
* |
|
34
|
|
|
* @param \Illuminate\Console\OutputStyle $output |
|
35
|
|
|
* @return self |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setOutput(OutputStyle $output) : self |
|
38
|
|
|
{ |
|
39
|
|
|
$this->output = $output; |
|
40
|
|
|
|
|
41
|
|
|
return $this; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Add task with result to the stack. |
|
46
|
|
|
* |
|
47
|
|
|
* @param string $name |
|
48
|
|
|
* @param callable $task |
|
49
|
|
|
* @return self |
|
50
|
|
|
*/ |
|
51
|
|
|
public function addTask(string $name, callable $task) : self |
|
52
|
|
|
{ |
|
53
|
|
|
$this->tasks[$name] = $task; |
|
54
|
|
|
|
|
55
|
|
|
return $this; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Send tasks to the command output. |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
* |
|
63
|
|
|
* @throws \Honeybadger\HoneybadgerLaravel\TaskFailed |
|
64
|
|
|
*/ |
|
65
|
|
|
public function runTasks() : void |
|
66
|
|
|
{ |
|
67
|
|
|
Collection::make($this->tasks)->each(function ($task, $description) { |
|
68
|
|
|
$result = $task(); |
|
69
|
|
|
|
|
70
|
|
|
if ($this->output) { |
|
71
|
|
|
$this->output->writeLn(vsprintf('%s: %s', [ |
|
72
|
|
|
$description, |
|
73
|
|
|
$result ? '<fg=green>✔</>' : '<fg=red>✘</>', |
|
74
|
|
|
])); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$this->results[$description] = $result; |
|
78
|
|
|
|
|
79
|
|
|
if (! $result && $this->throwOnError) { |
|
80
|
|
|
throw new TaskFailed(sprintf('%s failed, please review output and try again.', $description)); |
|
81
|
|
|
} |
|
82
|
|
|
}); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* Get all task results. |
|
87
|
|
|
* |
|
88
|
|
|
* @return array |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getResults() : array |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->results; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return bool |
|
97
|
|
|
*/ |
|
98
|
|
|
public function hasFailedTasks() : bool |
|
99
|
|
|
{ |
|
100
|
|
|
return in_array(false, $this->results); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @return self |
|
105
|
|
|
*/ |
|
106
|
|
|
public function doNotThrowOnError() : self |
|
107
|
|
|
{ |
|
108
|
|
|
$this->throwOnError = false; |
|
109
|
|
|
|
|
110
|
|
|
return $this; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|