Laravel 5 Commands - Execute one after other -
i have customcommand_1
, customcommand_2
.
any way create pipeline of commands , executing customcommand_2
right after customcommand_1
execution? (without call command inside other one).
i not find way this, came workaround (tested on laravel sync
driver).
first, have create/adjust base command:
namespace app\commands; use illuminate\foundation\bus\dispatchescommands; abstract class command { use dispatchescommands; /** * @var command[] */ protected $commands = []; /** * @param command|command[] $command */ public function addnextcommand($command) { if (is_array($command)) { foreach ($command $item) { $this->commands[] = $item; } } else { $this->commands[] = $command; } } public function handlingcommandfinished() { if (!$this->commands) return; $command = array_shift($this->commands); $command->addnextcommand($this->commands); $this->dispatch($command); } }
every command has call $this->handlingcommandfinished();
when finish execution.
with this, can chain commands:
$command = new firstcommand(); $command->addnextcommand(new secondcommand()); $command->addnextcommand(new thirdcommand()); $this->dispatch($command);
pipeline
instead of calling handlingcommandfinished
in each command, can use command pipeline!
in app\providers\busserviceprovider::boot
add:
$dispatcher->pipethrough([ 'app\commands\pipeline\chaincommands' ]);
add create app\commands\pipeline\chaincommands
:
class chaincommands { public function handle(command $command, $next) { $result = $next($command); $command->handlingcommandfinished(); return $result; } }
Comments
Post a Comment