black/cqrs-php
Minimal CQRS command bus for PHP/DDD without event sourcing. Define Command and CommandHandler, register handlers to commands, then dispatch via a single Bus. Includes optional Symfony bundle integration with service tags for handler registration.
CQRS in PHP is a simple project (a folder structure) for your project build with Domain Driven Design.
The recomanded way to install CQRS in PHP is through Composer:
{
"require": {
"black/cqrs-php": "@stable"
}
}
Protip: You should browse the black/cqrs-php page to choose a stable version to use, avoid the @stable meta
constraint.
I want to use a very basic library for CQRS without Event Sourcing. There is one Bus, register your handler with the related command and go for it!
1 - Create a Command implementing Black\DDD\CQRSinPHP\Infrastructure\CQRS\Command
2 - Create an Handler implementing Black\DDD\CQRSinPHP\Infrastructure\CQRS\CommandHandler
3 - Register the Handler/Command to the Bus
<?php
$bus = new Black\DDD\CQRSinPHP\Infrastructure\CQRS\Bus;
$handler = new My\Handler();
$bus->register('My\Command', $handler);
// Do stuff
$command = new My\Command($foo, $bar);
$bus->handle($command);
Register the bundle:
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new Black\Bundle\CQRSBundle\BlackCQRSBundle(),
);
}
Declare your handler as a service and add this tag:
services:
my.handler:
class: 'My\Handler'
tags:
- { name: black_cqrs.handler, command: "My\Command" }
And use it:
<?php
public function __construct(Black\DDD\CQRSinPHP\Infrastructure\CQRS\Bus $bus)
{
$this->bus = $bus;
}
public function myFunction($foo, $bar)
{
$command = new My\Command($foo, $bar);
$this->bus->handle($command);
}
This project is a work in progress so don't hesitate to see CONTRIBUTING file and submit your PR.
The code is heavily inspired by Benjamin Eberlei blog posts who did a very great job on many projects (including Doctrine and litecqrs-php.
This README is heavily inspired by Hateoas library by the great @willdurand. This guy needs your PR for the sake of the REST in PHP.
Alexandre "pocky" Balmes alexandre@lablackroom.com. Send me Flattrs if you love my work, buy me gift or hire me!
CQRS in PHP is released under the MIT License. See the bundled LICENSE file for details.
How can I help you explore Laravel packages today?