Git Product home page Git Product logo

laravel-command-bus's Introduction

Simple Command Bus for Laravel 5+

Build Status

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:

a) PatternHandlerResolver:

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

b) MapHandlerResolver:

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).

Usage

Simplified example:

// 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.

laravel-command-bus's People

Contributors

kbaryshnikov avatar

Stargazers

Mina Nabil Sami avatar

Watchers

Cirdog avatar James Cloos avatar  avatar Nikita Stupin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.