2017 © Pedro Peláez
 

library laravel-command-bus

Laravel Command Bus

image

upgate/laravel-command-bus

Laravel Command Bus

  • Monday, October 2, 2017
  • by kbaryshnikov
  • Repository
  • 2 Watchers
  • 0 Stars
  • 2 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Simple Command Bus for Laravel 5+

Build Status, (*1)

Setup

  1. composer require upgate/laravel-command-bus
  2. Register Upgate\LaravelCommandBus\CommandBusServiceProvider as a service provider
  3. In your service provider, bind Upgate\LaravelCommandBus\HandlerResolver to an implementation of your choice.

HandlerResolver binding examples:, (*2)

a) PatternHandlerResolver:, (*3)

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

b) MapHandlerResolver:, (*4)

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,
                // ...
            ]
        );
    }
);

c) Bind your own implementation (must extend \Upgate\LaravelCommandBus\HandlerResolver)., (*5)

Usage

Simplified example:, (*6)

// 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')
        ));
    }

}

Of course, you might (and should) want to introduce Controller and ConsoleCommand abstract classes with executeCommand() methods implemented., (*7)

The Versions

02/10 2017

dev-master

9999999-dev

Laravel Command Bus

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

laravel command bus

01/10 2017

0.1.0

0.1.0.0

Laravel Command Bus

  Sources   Download

BSD-2-Clause

The Requires

 

The Development Requires

laravel command bus