PHP code example of upgate / laravel-command-bus

1. Go to this page and download the library: Download upgate/laravel-command-bus library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

upgate / laravel-command-bus example snippets


$this->app->singleton(
    \Upgate\LaravelCommandBus\HandlerResolver::class,
    function () {
        return new \Upgate\LaravelCommandBus\PatternHandlerResolver(
            '\YourAppNamespace\CommandHandlers\%sHandler'
        );
    }
);

use YourAppNamespace\Commands;
use YourAppNamespace\CommandHandlers;
// ...
$this->app->singleton(
    \Upgate\LaravelCommandBus\HandlerResolver::class,
    function () {
        return new \Upgate\LaravelCommandBus\MapHandlerResolver(
            [
                Commands\FooCommand::class => Handlers\FooHandler::class,
                Commands\BarCommand::class => Handlers\BarHandler::class,
                // ...
            ]
        );
    }
);

// Command
class SignUpCommand {

    public function __construct($email, $password)
    {
        $this->email = $email;
        $this->password = $password;
    }
    
    public function email()
    {
        return $this->email;
    }
    
    public function password()
    {
        return $this->password;
    }
}

// Handler
class SignUpHandler {

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    
    public function handle(SignUpCommand $command)
    {
        $user = User::signUp($command->email(), $command->password());
        $this->userRepository->store($user);
    }

}

// HTTP Controller
use Upgate\LaravelCommandBus\CommandBus;

class UserController {

    private $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }
    
    public function signUp(Request $request)
    {
        $this->commandBus->execute(new SignUpCommand(
            $request->get('email'),
            $request->get('password')
        ));
    }

}

// Console command
use Upgate\LaravelCommandBus\CommandBus;

class SignUpUserConsoleCommand
{

    private $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }
    
    public function handle()
    {
        $this->commandBus->execute(new SignUpCommand(
            $this->argument('email'),
            $this->argument('password')
        ));
    }
    
}