honeybadger-io /
honeybadger-laravel
| 1 | <?php |
||
| 2 | |||
| 3 | namespace Honeybadger\HoneybadgerLaravel; |
||
| 4 | |||
| 5 | use Honeybadger\HoneybadgerLaravel\Exceptions\TaskFailed; |
||
| 6 | use Illuminate\Console\OutputStyle; |
||
| 7 | use Illuminate\Support\Collection; |
||
| 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 | public function addTask(string $name, callable $task, bool $throwOnFail = false): self |
||
| 48 | { |
||
| 49 | $this->tasks[$name] = [ |
||
| 50 | 'task' => $task, |
||
| 51 | 'throw_on_fail' => $throwOnFail, |
||
| 52 | ]; |
||
| 53 | |||
| 54 | return $this; |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Send tasks to the command output. |
||
| 59 | * |
||
| 60 | * @return void |
||
| 61 | * |
||
| 62 | * @throws \Honeybadger\HoneybadgerLaravel\TaskFailed |
||
| 63 | */ |
||
| 64 | public function runTasks(): void |
||
| 65 | { |
||
| 66 | Collection::make($this->tasks)->each(function ($task, $description) { |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 67 | $result = $task['task'](); |
||
| 68 | |||
| 69 | if ($this->output) { |
||
| 70 | $this->output->writeLn(vsprintf('%s: %s', [ |
||
| 71 | $description, |
||
| 72 | $result ? '<fg=green>✔</>' : '<fg=red>✘</>', |
||
| 73 | ])); |
||
| 74 | } |
||
| 75 | |||
| 76 | $this->results[$description] = $result; |
||
| 77 | |||
| 78 | if (! $result && $task['throw_on_fail'] && $this->throwOnError) { |
||
| 79 | throw new TaskFailed(sprintf('%s failed, please review output and try again.', $description)); |
||
| 80 | } |
||
| 81 | }); |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Get all task results. |
||
| 86 | * |
||
| 87 | * @return array |
||
| 88 | */ |
||
| 89 | public function getResults(): array |
||
| 90 | { |
||
| 91 | return $this->results; |
||
| 92 | } |
||
| 93 | |||
| 94 | /** |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | public function hasFailedTasks(): bool |
||
| 98 | { |
||
| 99 | return in_array(false, $this->results); |
||
| 100 | } |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @return self |
||
| 104 | */ |
||
| 105 | public function doNotThrowOnError(): self |
||
| 106 | { |
||
| 107 | $this->throwOnError = false; |
||
| 108 | |||
| 109 | return $this; |
||
| 110 | } |
||
| 111 | } |
||
| 112 |