MalocherCqrsModule
Zend Framework 2 Module that integrates the Malocher CQRS + Service Bus, (*1)
, (*2)
Installation
Installation of MalocherCqrsModule uses composer. For composer documentation, please refer to
getcomposer.org. Add following requirement to your composer.json, (*3)
"malocher/zf2-cqrs-module" : "1.*"
Then add MalocherCqrsModule
to your `config/application.config.php``, (*4)
Installation without composer is not officially supported, and requires you to install and autoload
the dependencies specified in the composer.json
., (*5)
Setup
Setup the CQRS System using your module or application configuration. Put all CQRS options under the key cqrs., (*6)
'cqrs' => array(
'adapters' => array(
'Malocher\Cqrs\Adapter\ArrayMapAdapter' => array(
'buses' => array(
'My\Bus\DomainBus' => array(
'My\Command\AddEntityCommand' => array(
'alias' => 'my_add_entity_command_handler',
'method' => 'addEntity'
),
),
),
),
),
),
'service_manager' => array(
'invokables' => array(
'my_add_entity_command_handler' => 'My\Repository\EntityRepository',
),
),
The ZF2 ServiceManager acts as Handler- and EventListenerLoader. That means, you can use your ServiceManager aliases
to pipe your commands, queries and events to your repositories, services and whatever you use in your application., (*7)
Usage
You can request the Malocher CQRS Gate from the ServiceManager. The hole setup of the CQRS and Service Bus System is done in the background.
Here is a simple example that invokes the AddEntityCommand on the DomainBus from within a controller:, (*8)
<?php
namespace My\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use My\Command\AddEntityCommand;
use My\Bus\DomainBus;
class MyController extends AbstractActionController {
public function addEntityAction() {
$entityName = $this->getEvent()->getRouteMatch()->getParam('entityname');
$addEntityCommand = new AddEntityCommand();
$addEntityCommand->setName($entityName);
$this->getServiceLocator()
->get('malocher.cqrs.gate')
->getBus(DomainBus::NAME)
->invokeCommand($addEntityCommand);
}
}
Sample Application
You can find a sample application with annotated sourcecode on malocher/zf2-cqrs-sample, (*9)