Skip to content

Commit

Permalink
inner buses must be annotation driven, simplified API, no MessageBusF…
Browse files Browse the repository at this point in the history
…actory
  • Loading branch information
Janos Szurovecz committed Oct 11, 2014
1 parent 859bf03 commit 5a6bdf8
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 249 deletions.
5 changes: 1 addition & 4 deletions src/predaddy/commandhandling/DirectCommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
namespace predaddy\commandhandling;

use predaddy\domain\Repository;
use predaddy\messagehandling\MessageBusFactory;
use predaddy\messagehandling\MessageHandlerDescriptorFactory;
use predaddy\messagehandling\SubscriberExceptionHandler;

Expand All @@ -45,21 +44,19 @@ class DirectCommandBus extends CommandBus
{
/**
* @param Repository $repository Is being passed to the registered DirectCommandForwarder
* @param MessageBusFactory $messageBusFactory Is being passed to the registered DirectCommandForwarder
* @param MessageHandlerDescriptorFactory $handlerDescFactory
* @param array $interceptors
* @param SubscriberExceptionHandler $exceptionHandler
* @param string $identifier
*/
public function __construct(
Repository $repository,
MessageBusFactory $messageBusFactory,
MessageHandlerDescriptorFactory $handlerDescFactory,
array $interceptors = [],
SubscriberExceptionHandler $exceptionHandler = null,
$identifier = self::DEFAULT_NAME
) {
parent::__construct($handlerDescFactory, $interceptors, $exceptionHandler, $identifier);
$this->register(new DirectCommandForwarder($repository, $messageBusFactory));
$this->register(new DirectCommandForwarder($repository));
}
}
30 changes: 17 additions & 13 deletions src/predaddy/commandhandling/DirectCommandForwarder.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
use precore\lang\ObjectClass;
use predaddy\domain\DefaultAggregateId;
use predaddy\domain\Repository;
use predaddy\messagehandling\annotation\AnnotatedMessageHandlerDescriptorFactory;
use predaddy\messagehandling\annotation\Subscribe;
use predaddy\messagehandling\DeadMessage;
use predaddy\messagehandling\MessageBusFactory;
use predaddy\messagehandling\util\MessageCallbackClosures;

/**
Expand All @@ -43,28 +43,31 @@
*
* @author Szurovecz János <szjani@szjani.hu>
*/
class DirectCommandForwarder extends Object
final class DirectCommandForwarder extends Object
{
/**
* @var Repository
* @var AnnotatedMessageHandlerDescriptorFactory
*/
private $repository;
private static $descriptorFactory;

/**
* @var MessageBusFactory
* @var Repository
*/
private $messageBusFactory;
private $repository;

/**
* @param Repository $repository
* @param MessageBusFactory $messageBusFactory
*/
public function __construct(
Repository $repository,
MessageBusFactory $messageBusFactory
) {
public function __construct(Repository $repository)
{
$this->repository = $repository;
$this->messageBusFactory = $messageBusFactory;
}

public static function init()
{
self::$descriptorFactory = new AnnotatedMessageHandlerDescriptorFactory(
new CommandFunctionDescriptorFactory()
);
}

/**
Expand Down Expand Up @@ -101,7 +104,7 @@ protected function forwardCommand(DirectCommand $command)
$aggregate->failWhenStateHashViolation($command->stateHash());
}
}
$forwarderBus = $this->messageBusFactory->createBus($aggregateClass);
$forwarderBus = new CommandBus(self::$descriptorFactory, [], null, $aggregateClass);
$forwarderBus->register($aggregate);
$result = null;
$thrownException = null;
Expand All @@ -127,3 +130,4 @@ function (Exception $exp) use (&$thrownException) {
return $result;
}
}
DirectCommandForwarder::init();
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,9 @@
use Iterator;
use predaddy\domain\AbstractAggregateRoot;
use predaddy\domain\DomainEvent;
use predaddy\eventhandling\EventBus;
use predaddy\eventhandling\EventFunctionDescriptorFactory;
use predaddy\messagehandling\MessageBus;
use predaddy\messagehandling\MessageBusFactory;
use predaddy\messagehandling\SimpleMessageBusFactory;
use predaddy\messagehandling\annotation\Subscribe;

/**
Expand All @@ -44,29 +43,27 @@
*/
abstract class AbstractEventSourcedAggregateRoot extends AbstractAggregateRoot implements EventSourcedAggregateRoot
{
private static $messageBusFactory = null;

/**
* @param MessageBusFactory $messageBusFactory
* @var EventSourcingEventHandlerDescriptorFactory
*/
public static function setInnerMessageBusFactory(MessageBusFactory $messageBusFactory = null)
private static $descriptorFactory;

public static function init()
{
self::$messageBusFactory = $messageBusFactory;
self::$descriptorFactory = new EventSourcingEventHandlerDescriptorFactory(
new EventFunctionDescriptorFactory()
);
}

/**
* @return SimpleMessageBusFactory
* @param AbstractEventSourcedAggregateRoot $aggregateRoot
* @return EventBus
*/
public static function getInnerMessageBusFactory()
private static function createInnerEventBus(AbstractEventSourcedAggregateRoot $aggregateRoot)
{
if (self::$messageBusFactory === null) {
self::$messageBusFactory = new SimpleMessageBusFactory(
new EventSourcingEventHandlerDescriptorFactory(
new EventFunctionDescriptorFactory()
)
);
}
return self::$messageBusFactory;
$bus = new EventBus(self::$descriptorFactory, [], null, static::className());
$bus->register($aggregateRoot);
return $bus;
}

/**
Expand All @@ -77,8 +74,7 @@ public static function getInnerMessageBusFactory()
*/
final public function loadFromHistory(Iterator $events)
{
$bus = self::getInnerMessageBusFactory()->createBus($this->getClassName());
$bus->register($this);
$bus = self::createInnerEventBus($this);
foreach ($events as $event) {
$this->handleEventInAggregate($event, $bus);
}
Expand Down Expand Up @@ -109,9 +105,9 @@ final protected function updateStateHash(DomainEvent $event)
private function handleEventInAggregate(DomainEvent $event, MessageBus $innerBus = null)
{
if ($innerBus === null) {
$innerBus = self::getInnerMessageBusFactory()->createBus($this->getClassName());
$innerBus->register($this);
$innerBus = self::createInnerEventBus($this);
}
$innerBus->post($event);
}
}
AbstractEventSourcedAggregateRoot::init();
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected function innerCreate($handler)
{
return new EventSourcingEventHandlerDescriptor(
$handler,
$this->getReader(),
self::getReader(),
$this->getFunctionDescriptorFactory()
);
}
Expand Down
38 changes: 0 additions & 38 deletions src/predaddy/messagehandling/MessageBusFactory.php

This file was deleted.

46 changes: 0 additions & 46 deletions src/predaddy/messagehandling/SimpleMessageBusFactory.php

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\Reader;
use predaddy\messagehandling\CachedMessageHandlerDescriptorFactory;
use predaddy\messagehandling\FunctionDescriptorFactory;

/**
* Uses Doctrine annotation reader and creates AnnotatedMessageHandlerDescriptor object for each handlers.
Expand All @@ -36,38 +35,28 @@
*/
class AnnotatedMessageHandlerDescriptorFactory extends CachedMessageHandlerDescriptorFactory
{
private static $defaultReader;

/**
* @var Reader
*/
private $reader;

public static function registerAnnotations()
{
AnnotationRegistry::registerFile(__DIR__ . '/MessageHandlingAnnotations.php');
}
private static $reader;

private static function getDefaultReader()
/**
* @return Reader
*/
public static function getReader()
{
if (self::$defaultReader === null) {
self::$defaultReader = new AnnotationReader();
if (self::$reader === null) {
self::$reader = new AnnotationReader();
}
return self::$defaultReader;
return self::$reader;
}

/**
* @param FunctionDescriptorFactory $functionDescFactory
* @param Reader $reader if null, an AnnotationReader instance will be used
* @param Reader $reader
*/
public function __construct(FunctionDescriptorFactory $functionDescFactory, Reader $reader = null)
public static function setReader(Reader $reader)
{
self::registerAnnotations();
if ($reader === null) {
$reader = self::getDefaultReader();
}
$this->reader = $reader;
parent::__construct($functionDescFactory);
self::$reader = $reader;
}

/**
Expand All @@ -78,16 +67,9 @@ protected function innerCreate($handler)
{
return new AnnotatedMessageHandlerDescriptor(
$handler,
$this->reader,
self::getReader(),
$this->getFunctionDescriptorFactory()
);
}

/**
* @return Reader
*/
public function getReader()
{
return $this->reader;
}
}
AnnotationRegistry::registerFile(__DIR__ . '/MessageHandlingAnnotations.php');
Loading

0 comments on commit 5a6bdf8

Please sign in to comment.