コード例 #1
0
 private function registerAggregateCommandHandlers()
 {
     $this->ensureRepositoryConfiguration();
     if (!$this->explicitCommandHandlersSet) {
         AnnotatedAggregateCommandHandler::subscribe($this->aggregateType, $this->repository, $this->commandBus, $this->parameterResolver, null, new SimpleAnnotationReaderFactory());
     }
 }
コード例 #2
0
// set up logging
$logger = new Logger('governor');
$logger->pushHandler(new StreamHandler('php://stdout', Logger::DEBUG));
// 1. create a command bus and command gateway
$commandBus = new SimpleCommandBus();
$commandBus->setLogger($logger);
$commandGateway = new DefaultCommandGateway($commandBus);
$rootDirectory = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'CommandHandlerExample';
@mkdir($rootDirectory);
echo sprintf("Initializing FileSystemEventStore in %s\n", $rootDirectory);
// 2. initialize the event store
$eventStore = new FilesystemEventStore(new SimpleEventFileResolver($rootDirectory), new JMSSerializer());
// 3. create the event bus
$eventBus = new SimpleEventBus();
$eventBus->setLogger($logger);
// 4. create an event sourcing repository
$repository = new EventSourcingRepository(User::class, $eventBus, new NullLockManager(), $eventStore, new GenericAggregateFactory(User::class));
//5. create and register our commands
AnnotatedAggregateCommandHandler::subscribe(User::class, $repository, $commandBus);
//6. create and register the eventlistener
$eventListener = new UserEventListener();
$eventBus->subscribe($eventListener);
//7. send commands
$aggregateIdentifier = Uuid::uuid1()->toString();
$commandGateway->send(new CreateUserCommand($aggregateIdentifier, '*****@*****.**'));
$commandGateway->send(new ChangeUserEmailCommand($aggregateIdentifier, '*****@*****.**'));
//8. read back aggregate from store
$uow = DefaultUnitOfWork::startAndGet($logger);
$aggregate = $repository->load($aggregateIdentifier);
echo sprintf("\n\nUser identifier:%s, email:%s, version:%s\n\n", $aggregate->getIdentifier(), $aggregate->getEmail(), $aggregate->getVersion());
$uow->rollback();