2017 © Pedro Peláez
 

library command-bus

Middleware Command Bus

image

kodcube/command-bus

Middleware Command Bus

  • Sunday, July 17, 2016
  • by srmiles
  • Repository
  • 1 Watchers
  • 0 Stars
  • 5 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Middleware Based Command Bus

A simple command bus that uses the middleware pattern for processing commands., (*1)

It has been designed to leaverage Dependency Injection with Autowiring support. All of it's dependencies and be configured & overridden by a dependency injection container, (*2)

Main Features

  • All Dependancies are injected at construction
  • All Dependencies are based on interfaces
  • Route Commands differently based on custom middleware
  • Use simple arrays for middleware configuration
  • Uses Dependency Injection Container when loading middleware
  • Uses Dependency Injection Container when loading handlers

Limitations

  • Does not inject dependencies for methods other than __constructor
  • Does not inject dependencies for setters

Requirements

  • PHP 7
  • Container-Interop - Dependancy Injection Container
  • GunnaPHP\Invoker

Optional

  • KodCube\DependencyInjection - Dependancy Injection Container
  • KodCube\MessageBus - Dispatching commands to background processes

Usage

Create Command Bus

This will create a command bus with the default handler detection, (*3)

``` PHP $commandbus = new KodCube\CommandBus\CommandBus( new KodCube\CommandBus\MiddlewareManager( new KodCube\DependencyInjection\Container() ) );, (*4)


or using dependancy injection container ``` PHP DI Config [ MiddlewareManagerInterface::class => MiddlewareManager::class, ContainerInterface::class => Container::class ] $commandbus = $di->get(KodCube\CommandBus\CommandBus::class);

Dispatch Command

Assuming you have the following command and handler, (*5)

Command Class: MyCommand, (*6)

Handler Class: MyHandler, (*7)

``` PHP $command = new MyCommand();, (*8)

$response = $commandbus($command); or $response = $commandbus->handle($command);, (*9)

is the equivalant to 

``` PHP
$command = new MyCommand();
$handler = new MyHandler();

$response = $handler($command);

Override Command Handler

The default handler middleware allows you to override which handler is responsible for handling the command, (*10)

``` PHP $commandbus = new KodCube\CommandBus\CommandBus( new KodCube\CommandBus\MiddlewareManager( new KodCube\DependencyInjection\Container(), new KodCube\CommandBus\Middleware\Handler( [ MyCommand::class => MyNewHandler::class ] ) ) );, (*11)

is the equivalant to 

``` PHP
$command = new MyCommand();
$handler = new MyNewHandler();

$response = $handler($command);

or using dependancy injection container, (*12)

``` PHP DI Config [ MiddlewareManagerInterface::class => MiddlewareManager::class, ContainerInterface::class => Container::class Handler::class => [ [ MyCommand::class => MyNewHandler::class ] ] ], (*13)

$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);, (*14)


### Multiple Middlerware Handlers Using the middleware capablies we can easily add multiple command handlers, to process some commands differently from others. In this example we add a Queueable Middleware Handler, that will look for commands that implement a *CommandQueueInterface::class*. Theses commands will be routed to a message bus for processing on a background process, were as all other command will be handled by the final middleware handler. ``` PHP $commandbus = new KodCube\CommandBus\CommandBus( new KodCube\CommandBus\MiddlewareManager( [ KodCube\CommandBus\Middleware\Queue::class, KodCube\CommandBus\Middleware\Handler::class ], new KodCube\DependencyInjection\Container(), ) );

or using dependancy injection container, (*15)

``` PHP DI Config [ MiddlewareManagerInterface::class => MiddlewareManager::class, MiddlewareManager::class => [ Middleware\Queue::class, Middleware\Handler::class ], ContainerInterface::class => Container::class, Handler::class => [ [ MyCommand::class => MyNewHandler::class ] ] ], (*16)

$commandbus = $di->get(KodCube\CommandBus\CommandBus::class); ```, (*17)

The Versions

17/07 2016

dev-master

9999999-dev

Middleware Command Bus

  Sources   Download

The Requires