<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
twanhaverkamp / event-storage-in-mongodb-with-php example snippets
// ...
use MongoDB\Client as MongoDbClient;
use TwanHaverkamp\EventSourcingWithPhp\Event\EventDescriber;
use TwanHaverkamp\EventStorageInMongoDbWithPhp\Event\EventStore;
// ...
$eventStore = new EventStore\MongoDb(
new MongoDbClient(sprintf(
'mongodb://%s:%s@%s:%d',
'root',
'password',
'mongodb',
27017,
)),
new EventDescriber\KebabCase(),
'your-database-name',
'your-collection-name',
);
// ...
use TwanHaverkamp\EventSourcingWithPhp\Example;
use TwanHaverkamp\EventStorageInMongoDbWithPhp\Event\EventStore;
EventStore\MongoDb::register(
Example\Event\InvoiceWasCreated::class,
Example\Event\PaymentTransactionWasStarted::class,
Example\Event\PaymentTransactionWasCompleted::class,
Example\Event\PaymentTransactionWasCancelled::class,
);
namespace App\Providers;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Support\ServiceProvider;
use MongoDB\Client as MongoDbClient;
use TwanHaverkamp\EventSourcingWithPhp\Event\EventDescriber\KebabCase;
use TwanHaverkamp\EventSourcingWithPhp\Event\EventStore\EventStoreInterface;
use TwanHaverkamp\EventStorageInMongoDbWithPhp\Event\EventStore\MongoDb;
class EventStoreServiceProvider extends ServiceProvider
{
public function register(): void
{
$this->app->singleton(EventStoreInterface::class, function (Application $app) {
return new MongoDb(
new MongoDbClient(sprintf(
'mongodb://%s:%s@%s:%d',
config('mongodb.username'),
config('mongodb.password'),
config('mongodb.host'),
config('mongodb.port'),
)),
new EventDescriber\KebabCase(),
config('mongodb.database'),
config('mongodb.collection'),
});
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.