PHP code example of twanhaverkamp / event-storage-in-mongodb-with-php

1. Go to this page and download the library: Download twanhaverkamp/event-storage-in-mongodb-with-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?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'),
        });
    }
}