dev-master
9999999-dev https://github.com/linkorb/busCommand Bus component
MIT
The Development Requires
command bus cqrs ddd commandbus
Wallogit.com
2017 © Pedro Peláez
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)
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)
<?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;
}
}
<?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']);
}
}
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
class basketController
{
public function confirmAction()
{
$orderid = 5;
// Instantiate a Command
$command = new OrderConfirmCommand($orderid);
// Dispatch the command
$this->bus->dispatch($command);
}
}
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!
MIT (see LICENSE.md), (*12)

Check out our other projects at linkorb.com/engineering., (*13)
Btw, we're hiring!, (*14)
Command Bus component
MIT
command bus cqrs ddd commandbus