2017 © Pedro Peláez
 

library bus

Command Bus component

image

linkorb/bus

Command Bus component

  • Sunday, January 18, 2015
  • by joostfaassen
  • Repository
  • 9 Watchers
  • 2 Stars
  • 1 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 1 Versions
  • 0 % Grown

The README.md

Command Bus Component

This standalone component implements the Command Pattern., (*1)

It is inspired by the Laravel illuminate/bus and illuminate/queue components, but aims to be framework agnostic., (*2)

For more information about the Command Bus Pattern:, (*3)

Usage

Any Command requires two classes: the DoSomethingCommand class and the DoSomethingHandler class., (*4)

You can instantiate the Command from your controllers when needed, and dispatch it onto the Bus., (*5)

The Bus will then resolve the matching Handler class, and ask it to handle the new Command., (*6)

Here's how you use it:, (*7)

An example Command class:

<?php

use LinkORB\Component\Bus\CommandInterface;

class OrderConfirmCommand implements CommandInterface
{
    private $orderId;
    public function __construct($orderId)
    {
        $this->orderId = $orderId;
    }

    public function getOrderId()
    {
        return $this->orderId;
    }
}

An example Handler class:

<?php

use LinkORB\Component\Bus\HandlerInterface;
use LinkORB\Component\Bus\CommandInterface;

class OrderConfirmHandler implements HandlerInterface
{
    private $db;
    private $mailer;

    // the constructor automatically receives requested services from the container
    public function __construct($db, $mailer)
    {
        $this->db = $db;
        $this->mailer = $mailer;
    }

    // this method is called by the bus to handle commands
    public function handle(CommandInterface $command)
    {
        $order = $this->db->fetchOneById('orders', $command->getOrderId());
        $this->mailer->mail($order['email'], "Thanks for your order, " . $order['customername']);

    }
}

Setting up the bus in your init script


use LinkORB\Component\Bus\Bus; // Reuse an existing container, or create a plain php array // Any array type dependency injection container will work too (symfony, pimple, etc) $container = array( 'db'=>$db, 'mailer'=>$mailer ); // Instantiate the bus, and optionally pass a container $bus = new Bus($container); // now add the bus to the container, or use any other method to pass the bus to your controllers

Using the bus in your Controllers:

class basketController
{
    public function confirmAction()
    {
        $orderid = 5;

        // Instantiate a Command
        $command = new OrderConfirmCommand($orderid);

        // Dispatch the command
        $this->bus->dispatch($command);
    }
}

Calling commands from the command-line

A simple command-line dispatcher is included. It can be used like this:, (*8)

./vendor/bin/bus bus:dispatch "Acme\Bus\DoSomethingCommand" -p color="red" -g os="linux"

The -p arguments will be passed as parameters to the command. You can pass as many as you want. The -g arguments will be inserted into a simple array "container", so it can be used by the Handlers, (*9)

One example command is included:, (*10)

./vendor/bin/bus bus:dispatch "LinkORB\Component\Bus\Bus\HelloCommand" -p name="World" -g sender="me"

This will output:, (*11)

Hello, World from me!

License

MIT (see LICENSE.md), (*12)

Brought to you by the LinkORB Engineering team


Check out our other projects at linkorb.com/engineering., (*13)

Btw, we're hiring!, (*14)

The Versions

18/01 2015

dev-master

9999999-dev https://github.com/linkorb/bus

Command Bus component

  Sources   Download

MIT

The Development Requires

command bus cqrs ddd commandbus