This is a little trait I use to stack output over the course of a command and display all the output at the end.
trait StackableOutput
{
protected $stackedOutput = [];
private function stackOutput($content, $type = 'line')
{
array_push($this->stackedOutput, [
'type' => $type,
'content' => $content,
]);
}
private function stackedOutput()
{
collect($this->stackedOutput)->each(function ($message) {
call_user_func_array([$this, $message['type']], [$message['content']]);
});
}
}
Example Usage:
class ExampleCommand extends Command
{
use StackableOutput;
public function handle()
{
// Do some stuff
$this->stackOutput('Some action was performed', 'info');
// Do some more stuff
$this->stackOutput('Some action had a trivial errors', 'error');
// Finish doing stuff
$this->stackedOutput();
}
}