Laravel 5命令-逐个执行

Laravel 5命令-逐个执行,laravel,command,queue,laravel-5,Laravel,Command,Queue,Laravel 5,我有一个CustomCommand_1和一个CustomCommand_2 有没有办法创建命令管道,并在执行CustomCommand\u 1之后立即执行CustomCommand\u 2?(无需在另一个内部调用命令)。您可以使用回调来决定何时运行或何时不运行,使用when()或skip() 转介:及 您还可以阅读如何在队列中添加命令。 看,如果这有帮助的话。我找不到任何方法来做这件事,所以我找到了解决办法(在laravelsyncdriver上进行了测试) 首先,必须创建/调整基本命令: na

我有一个
CustomCommand_1
和一个
CustomCommand_2


有没有办法创建命令管道,并在执行
CustomCommand\u 1
之后立即执行
CustomCommand\u 2
?(无需在另一个内部调用命令)。

您可以使用回调来决定何时运行或何时不运行,使用when()或skip()

转介:及

您还可以阅读如何在队列中添加命令。
看,如果这有帮助的话。

我找不到任何方法来做这件事,所以我找到了解决办法(在laravel
sync
driver上进行了测试)

首先,必须创建/调整基本命令:

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 as $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);
    }
}
每个命令都必须调用
$this->handlingCommandFinished()当它们完成执行时

使用此功能,您可以链接命令:

$command = new FirstCommand();
$command->addNextCommand(new SecondCommand());
$command->addNextCommand(new ThirdCommand());
$this->dispatch($command);

管道

您可以使用命令管道,而不是在每个命令中调用
handlingCommandFinished

App\Providers\BusServiceProvider::boot
中添加:

$dispatcher->pipeThrough([
    'App\Commands\Pipeline\ChainCommands'
]);
添加创建
App\Commands\Pipeline\ChainCommands

class ChainCommands {
    public function handle(Command $command, $next) {
        $result = $next($command);
        $command->handlingCommandFinished();
        return $result;
    }
}

是什么阻止你做以下事情

$this->dispatch(new CustomCommand_1);
$this->dispatch(new CustomCommand_2);
// And so on

因为每个自定义命令调用更多命令。在执行
CustomCommand\u 1
及其子命令之后,我想执行
CustomCommand\u 2
。@idknow通过将它们添加到队列中,它们会排队并依次运行?我已经编辑了我的帖子-使用Laravel的命令管道使它更容易:)
$this->dispatch(new CustomCommand_1);
$this->dispatch(new CustomCommand_2);
// And so on