Git Product home page Git Product logo

commandbus's Introduction

CommandBus

PHP 5.4+ framework agnostic CommandBus

Usage

This is a simple CommandBus implementation. It assumes you don't need to do a whole lot of customization, and leaves you to implement your own command and handlers.

Within your controller, instantiate a new CommandBus or ValidationCommandBus:

use Commandbus\BaseCommandBus;
use Commandbus\ValidationCommandBus;


class myController {

    private $commandBus;
    
    //Laravel 4+ constructor
    public function __construct(ValidationCommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }
    
    //General constructor
    public function __construct()
    {
        $this->commandBus = new ValidationCommandBus();
    }

This doesn't do anything until you create a command object and execute it with the bus. The command object itself is really just a fancy object that contains data being passed to the validator and handler. The object should implement CommandRequest as so:

class MyCommand implements Commandbus\Contracts\CommandRequest {
    private $data;
    
    public function __construct($data)
    {
        $this->data = $data;
    }
}

That's all you need for the command class.Create your own and instantiate it however you like in the controller. We will then want to pass the command to the command bus for execution:

$command = new MyCommand($input);
$results = $this->commandBus->execute($command);
//do what you need to with the results, we'll return whatever you return from your handler

When you create your handler, they should implement Commandbus\Contracts\CommandHandler:

class MyCommandHandler implements Commandbus\Contracts\CommandHandler {
    public function handle(CommandRequest $command)
    {
        //handle your command
    }
}

Any data you wish to pass back should be in the form of a class implementing Commandbus\Contracts\CommandResponse. If you just need to return simple data like a string or array, you can use the provided Commandbus\BaseCommandResponse in a simple manner:

return new Commandbus\BaseCommandResponse('success');

The constructor accepts $data and $errors, both of which default to null.

If you are using the ValidationCommandBus, you will want to also have a validator available to handle this. It should implement the CommandValidator class:

class MyCommandValidator implementes Commandbus\Contracts\CommandValidator {
    public function validate(CommandRequest $command)
    {
        //do validation
    }
    
    //if validation passes, return false
}

Please note in the above that a passing validation should return false. Anything else will be prematurely returned by the CommandBus.

###Class Name Resolution

This package doesn't use any IoC or dependency injection features to resolve class names. Rather, we will simply look for the handler in the same namespace as the command that was passed to it. The following is how the validator and handler are resolved:

Foo\App\RegisterUserCommand would correspond to the handler and validator Foo\App\RegisterUserHandler and Foo\App\RegisterUserValidator, respectively.

You can pass in your own CommandTranslator (implementing Commandbus\Contracts\CommandTranslator) to both the BaseCommandBus and ValidationCommandBus if you need custom class name resolution.

commandbus's People

Watchers

James Cloos avatar Mike Dugan 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.