public function persist($aggregateRoot)
 {
     $aggregateRootId = $this->identifierExtractor->extract($aggregateRoot);
     $stream = new Stream(Stream\Id::fromString($aggregateRootId));
     // ensure that the sequence index is set to the latest item
     // TODO: we should actually use a StreamRepository to retrieve the existing stream and its index
     $this->eventStore->fetchEvents($stream);
     $events = $this->eventsHandler->extractUncommittedEvents($aggregateRoot);
     foreach ($events as $domainEvent) {
         $event = new Stream\Event(Stream\Event\Id::generate(), new Stream\Event\Payload($this->domainEventSerializer->toArray($domainEvent)), new \DateTimeImmutable(), new Metadata(['type' => get_class($domainEvent)]));
         $this->eventStore->persist($stream, [$event]);
         if ($this->emitter) {
             $this->emitter->emit($domainEvent);
         }
     }
 }
 public function project(Snapshot $fromSnapshot = null)
 {
     if ($fromSnapshot instanceof Snapshot && $fromSnapshot->projectorSignature() === $this->signature()) {
         $this->projection = $fromSnapshot->data();
         $this->lastIndex = $fromSnapshot->index();
     }
     $eventSources = [];
     foreach ($this->streams as $streamId) {
         $stream = new Stream(Stream\Id::fromString((string) $streamId));
         $eventSources = array_merge($eventSources, $this->eventStore->fetchEvents($stream));
     }
     $events = $this->removeProjectedEvents($this->sortEvents($this->filterEventsBasedOnSpecification($eventSources)));
     // Apply event on projection
     foreach ($events as $event) {
         $this->applyEventOnProjection($event);
     }
     return $this->projection();
 }
示例#3
0
<?php

use Doctrine\DBAL\DriverManager;
use PhpInPractice\EventStore\Storage\Doctrine;
use PhpInPractice\EventStore\Stream;
require_once '../../vendor/autoload.php';
$connection = DriverManager::getConnection(['url' => 'sqlite:////tmp/es-todo.sqlite']);
$storage = new Doctrine($connection);
$eventStore = new \PhpInPractice\EventStore\EventStore($storage);
$stream = new Stream(Stream\Id::generate());
 public static function fromArray(array $data)
 {
     return new static(Consumer\Id::fromString($data['id']), Stream\Id::fromString($data['streamId']), Privilege::fromString($data['privilege']));
 }