create() публичный Метод

public create ( Stream $stream ) : void
$stream Prooph\EventStore\Stream\Stream
Результат void
Пример #1
0
 /**
  * @param AggregateType $repositoryAggregateType
  * @param string $aggregateId
  * @param Message[] $streamEvents
  * @param object $aggregateRoot
  * @throws Exception\InvalidArgumentException
  * @return void
  */
 public function addEventsForNewAggregateRoot(AggregateType $repositoryAggregateType, $aggregateId, array $streamEvents, $aggregateRoot)
 {
     $arType = AggregateType::fromAggregateRoot($aggregateRoot);
     if (!$repositoryAggregateType->equals($arType)) {
         throw new Exception\InvalidArgumentException(sprintf('aggregate root mismatch between repository type %s and object type %s', $repositoryAggregateType->toString(), $arType->toString()));
     }
     $this->eventStore->create(new Stream($this->buildStreamName($repositoryAggregateType, $aggregateId), $streamEvents));
 }
 protected function setUp()
 {
     $this->eventStore = new EventStore(new InMemoryAdapter(), new ProophActionEventEmitter());
     $this->eventStore->beginTransaction();
     $this->eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator([])));
     $this->eventStore->commit();
     $this->resetRepository();
 }
Пример #3
0
 public function setUp()
 {
     $inMemoryAdapter = new InMemoryAdapter();
     $eventEmitter = new ProophActionEventEmitter();
     $this->eventStore = new EventStore($inMemoryAdapter, $eventEmitter);
     $this->repository = new AggregateRepository($this->eventStore, AggregateType::fromAggregateRootClass('ProophTest\\EventStore\\Mock\\User'), new ConfigurableAggregateTranslator());
     $this->result = [];
     $self = $this;
     $router = new CommandRouter();
     $router->route(TakeSnapshot::class)->to(function (TakeSnapshot $command) use($self) {
         $self->result[] = ['aggregate_type' => $command->aggregateType(), 'aggregate_id' => $command->aggregateId()];
     });
     $commandBus = new CommandBus();
     $commandBus->utilize($router);
     $plugin = new SnapshotPlugin($commandBus, 2);
     $plugin->setUp($this->eventStore);
     $this->eventStore->beginTransaction();
     $this->eventStore->create(new Stream(new StreamName('event_stream'), new \ArrayIterator()));
     $this->eventStore->commit();
 }
Пример #4
0
 /**
  * @param object $eventSourcedAggregateRoot
  * @throws Exception\AggregateTypeException
  */
 public function addAggregateRoot($eventSourcedAggregateRoot)
 {
     $this->aggregateType->assert($eventSourcedAggregateRoot);
     $domainEvents = $this->aggregateTranslator->extractPendingStreamEvents($eventSourcedAggregateRoot);
     $aggregateId = $this->aggregateTranslator->extractAggregateId($eventSourcedAggregateRoot);
     $streamName = $this->determineStreamName($aggregateId);
     $enrichedEvents = [];
     foreach ($domainEvents as $event) {
         $enrichedEvents[] = $this->enrichEventMetadata($event, $aggregateId);
     }
     if ($this->oneStreamPerAggregate) {
         $stream = new Stream($streamName, new ArrayIterator($enrichedEvents));
         $this->eventStore->create($stream);
     } else {
         $this->eventStore->appendTo($streamName, new ArrayIterator($enrichedEvents));
     }
 }
Пример #5
0
 /**
  * @return EventStore
  */
 protected function getOtherMachineEventStore()
 {
     if (is_null($this->otherMachineEventStore)) {
         $inMemoryAdapter = new InMemoryAdapter();
         $config = new Configuration();
         $config->setAdapter($inMemoryAdapter);
         $this->otherMachineEventStore = new EventStore($config);
         $this->otherMachineEventStore->getActionEventDispatcher()->attachListener("commit.post", function (PostCommitEvent $postCommitEvent) {
             $this->otherMachineLastPostCommitEvent = $postCommitEvent;
             foreach ($postCommitEvent->getRecordedEvents() as $event) {
                 $this->otherMachineEventNameLog[] = $event->messageName();
             }
         });
         $this->otherMachineEventStore->beginTransaction();
         $this->otherMachineEventStore->create(new Stream(new StreamName('prooph_processing_stream'), []));
         $this->otherMachineEventStore->commit();
     }
     return $this->otherMachineEventStore;
 }
Пример #6
0
 /**
  * @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());
 }
 */
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;
}
 /**
  * @param array $data
  * @return array
  */
 public function run(array $data)
 {
     switch ($data['adapter']) {
         case 'mysql':
             $connection = DriverManager::getConnection($data['options']);
             $connection->executeQuery('DROP TABLE IF EXISTS user_stream');
             $adapter = new DoctrineEventStoreAdapter($connection, new FQCNMessageFactory(), new NoOpMessageConverter(), new JsonPayloadSerializer());
             break;
         case 'postgres':
             $connection = DriverManager::getConnection($data['options']);
             $connection->executeQuery('DROP TABLE IF EXISTS user_stream');
             $adapter = new DoctrineEventStoreAdapter($connection, new FQCNMessageFactory(), new NoOpMessageConverter(), new JsonPayloadSerializer());
             break;
         case 'mongodb':
             $connection = new \MongoClient($data['options']['server']);
             $connection->selectDB('event_store_adapter_benchmarks')->selectCollection('user_stream')->drop();
             $adapter = new MongoDbEventStoreAdapter(new FQCNMessageFactory(), new NoOpMessageConverter(), $connection, $data['options']['db_name']);
             break;
         default:
             throw new \InvalidArgumentException('invalid adapter given');
             break;
     }
     $eventStore = new EventStore($adapter, new ProophActionEventEmitter());
     $result = [];
     foreach ($data['batchSizes'] as $batchSize) {
         for ($i = 0; $i < $this->repeats; $i++) {
             $events = [];
             $start = microtime(true);
             for ($b = 1; $b <= $batchSize * 2; $b++) {
                 $v1 = $b;
                 $b++;
                 $v2 = $b;
                 $events[] = UserCreated::with(['name' => 'Max Mustermann ' . $i . '-' . $b, 'email' => 'contact' . $i . '-' . $b . '@prooph.de'], $v1);
                 $events[] = UsernameChanged::with(['name' => 'John Doe ' . $i . '-' . $b], $v2);
             }
             $eventStore->beginTransaction();
             $eventStore->create(new Stream(new StreamName('user_stream'), new \ArrayIterator($events)));
             $eventStore->commit();
             $end = microtime(true);
             if ($connection instanceof Connection) {
                 $connection->executeQuery('DROP TABLE IF EXISTS user_stream');
             } else {
                 $connection->selectDB('event_store_adapter_benchmarks')->selectCollection('user_stream')->drop();
             }
             $diff = $end - $start;
             $result[$data['adapter'] . ' with batch size ' . $batchSize] = $diff;
         }
     }
     return $result;
 }