Skip to content

Instantly share code, notes, and snippets.

@sixlive
Last active August 31, 2017 19:40
Show Gist options
  • Save sixlive/cdf71c471aa1423eefd03dc8a7f37cf6 to your computer and use it in GitHub Desktop.
Save sixlive/cdf71c471aa1423eefd03dc8a7f37cf6 to your computer and use it in GitHub Desktop.
Stackable Command Output

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();
    }
}
published: true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment