/** * Returns null if no stream events can be found for aggregate root otherwise the reconstituted aggregate root * * @param string $aggregateId * @return null|object */ public function getAggregateRoot($aggregateId) { Assertion::string($aggregateId, 'AggregateId needs to be string'); if ($this->snapshotStore) { $eventSourcedAggregateRoot = $this->loadFromSnapshotStore($aggregateId); if ($eventSourcedAggregateRoot) { //Cache aggregate root in the identity map $this->identityMap[$aggregateId] = $eventSourcedAggregateRoot; return $eventSourcedAggregateRoot; } } $streamName = $this->determineStreamName($aggregateId); $streamEvents = null; if ($this->oneStreamPerAggregate) { $streamEvents = $this->eventStore->load($streamName)->streamEvents(); } else { $streamEvents = $this->eventStore->loadEventsByMetadataFrom($streamName, ['aggregate_type' => $this->aggregateType->toString(), 'aggregate_id' => $aggregateId]); } if (!$streamEvents->valid()) { return; } $eventSourcedAggregateRoot = $this->aggregateTranslator->reconstituteAggregateFromHistory($this->aggregateType, $streamEvents); //Cache aggregate root in the identity map but without pending events $this->identityMap[$aggregateId] = $eventSourcedAggregateRoot; return $eventSourcedAggregateRoot; }
*/ require __DIR__ . '/../vendor/autoload.php'; use Prooph\Common\Event\ProophActionEventEmitter; use Prooph\Common\Messaging\FQCNMessageFactory; use Prooph\Common\Messaging\NoOpMessageConverter; use Prooph\EventStore\Adapter\Flywheel\FlywheelEventStoreAdapter; use Prooph\EventStore\EventStore; use Prooph\EventStore\Stream\Stream; use Prooph\EventStore\Stream\StreamName; use ProophTest\EventStore\Mock\UserCreated; use ProophTest\EventStore\Mock\UsernameChanged; $rootDir = __DIR__ . '/event_store'; $adapter = new FlywheelEventStoreAdapter($rootDir, new FQCNMessageFactory(), new NoOpMessageConverter()); $actionEmitter = new ProophActionEventEmitter(); $eventStore = new EventStore($adapter, $actionEmitter); $streamName = new StreamName('event_stream'); $stream = new Stream($streamName, new \ArrayIterator([])); // // Persist some events in the event store // $eventStore->beginTransaction(); $eventStore->create($stream); $eventStore->appendTo($streamName, new \ArrayIterator([UserCreated::with(['name' => 'Max Mustermann'], 1)->withAddedMetadata('tag', 'person'), UsernameChanged::with(['name' => 'John Doe'], 2)->withAddedMetadata('tag', 'person')])); $eventStore->commit(); // // Load all the stored events // $persistedEventStream = $eventStore->load($streamName); foreach ($persistedEventStream->streamEvents() as $event) { echo $event->payload()['name'] . PHP_EOL; }
/** * @test * @expectedException Prooph\EventStore\Exception\StreamNotFoundException */ public function it_throws_stream_not_found_exception_if_adapter_loads_nothing() { $stream = $this->getTestStream(); $adapter = $this->prophesize(Adapter::class); $eventStore = new EventStore($adapter->reveal(), new ProophActionEventEmitter()); $eventStore->beginTransaction(); $eventStore->create($stream); $eventStore->commit(); $eventStore->load($stream->streamName()); }
/** * @param AggregateType $aggregateType * @param string $aggregateId * @param null|int $minVersion * @return Message[] */ public function read(AggregateType $aggregateType, $aggregateId, $minVersion = null) { $stream = $this->eventStore->load($this->buildStreamName($aggregateType, $aggregateId), $minVersion); return $stream->streamEvents(); }