Laravel 5 Commands - Execute one after other

Christopher

I have a CustomCommand_1 and a CustomCommand_2.

Any way to create a pipeline of commands and executing CustomCommand_2 right after CustomCommand_1 execution? (without call a command inside the other one).

Daniel Antos

I could not find any way to do this, so I came up workaround (tested on laravel sync driver).

First, you have to 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 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);
    }
}

Every command has to call $this->handlingCommandFinished(); when they finish execution.

With this, you can chain your commands:

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

Pipeline

Instead of calling handlingCommandFinished in each command, you 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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel 5 Commands - Execute one after other

From Dev

How to execute several commands after each other with one request to the terminal (without using a file)?

From Dev

How to execute several commands after each other with one request to the terminal (without using a file)?

From Dev

execute asynctask twice one after other

From Dev

Execute 2 functions one after the other in AngularJS

From Dev

How to execute jquery function one after other

From Dev

How to execute "php artisan migrate" and other Laravel commands in remote server?

From Dev

How to execute "php artisan migrate" and other Laravel commands in remote server?

From Dev

I want to execute the commands in a loop at the same time, not one after another

From Java

Does JDBC's executeBatch() execute statements in parallel or one after the other

From Dev

make javascript loading functions to execute one after the other

From Dev

Execute a line of commands with one sudo

From Dev

Execute multiple commands after [ test

From Dev

Execute multiple commands after [ test

From Dev

execute multiple commands after ssh

From Dev

Unable to execute Laravel artisan commands

From Dev

Creating a .bat file to execute mysql and other commands

From Dev

pass multiple commands for cmd to execute as one block

From Java

Execute combine multiple Linux commands in one line

From Dev

Execute two commands if the first one fails

From Dev

Is there a way to search through the eclipse commands to execute one?

From Dev

execute commands as user after Vagrant provisioning

From Dev

bash script execute commands after ssh

From Dev

Is there a way to execute commands after file changed?

From Dev

Batch script to execute jobs one after the other using a GUID as an input argument

From Dev

How to execute 2 SQL queries one after the other using mysqli_multi_query in PHP

From Dev

Cannot execute Laravel Artisan commands on shared hosting

From Dev

Why is one of these date commands valid and the other not?

From Dev

Laravel 5 execute own query

Related Related

  1. 1

    Laravel 5 Commands - Execute one after other

  2. 2

    How to execute several commands after each other with one request to the terminal (without using a file)?

  3. 3

    How to execute several commands after each other with one request to the terminal (without using a file)?

  4. 4

    execute asynctask twice one after other

  5. 5

    Execute 2 functions one after the other in AngularJS

  6. 6

    How to execute jquery function one after other

  7. 7

    How to execute "php artisan migrate" and other Laravel commands in remote server?

  8. 8

    How to execute "php artisan migrate" and other Laravel commands in remote server?

  9. 9

    I want to execute the commands in a loop at the same time, not one after another

  10. 10

    Does JDBC's executeBatch() execute statements in parallel or one after the other

  11. 11

    make javascript loading functions to execute one after the other

  12. 12

    Execute a line of commands with one sudo

  13. 13

    Execute multiple commands after [ test

  14. 14

    Execute multiple commands after [ test

  15. 15

    execute multiple commands after ssh

  16. 16

    Unable to execute Laravel artisan commands

  17. 17

    Creating a .bat file to execute mysql and other commands

  18. 18

    pass multiple commands for cmd to execute as one block

  19. 19

    Execute combine multiple Linux commands in one line

  20. 20

    Execute two commands if the first one fails

  21. 21

    Is there a way to search through the eclipse commands to execute one?

  22. 22

    execute commands as user after Vagrant provisioning

  23. 23

    bash script execute commands after ssh

  24. 24

    Is there a way to execute commands after file changed?

  25. 25

    Batch script to execute jobs one after the other using a GUID as an input argument

  26. 26

    How to execute 2 SQL queries one after the other using mysqli_multi_query in PHP

  27. 27

    Cannot execute Laravel Artisan commands on shared hosting

  28. 28

    Why is one of these date commands valid and the other not?

  29. 29

    Laravel 5 execute own query

HotTag

Archive