Exemple #1
0
<?php

use example\Shop\Model\Product;
use Knp\Event\Repository;
use Knp\Event\Store;
require __DIR__ . '/../vendor/autoload.php';
\Symfony\Component\Debug\Debug::enable();
$dispatcher = new \Knp\Event\Dispatcher();
$conn = \Doctrine\DBAL\DriverManager::getConnection(['dbname' => 'event_store_projection', 'user' => 'florian', 'password' => null, 'host' => 'localhost', 'driver' => 'pdo_pgsql']);
$dispatcher->add(new \Knp\Event\Crud\Projection($conn));
$store = new Store\Logger(new Store\InMemory());
$repository = (new Repository\Factory($store, $dispatcher))->create();
class Address implements \Knp\Event\Emitter
{
    use \Knp\Event\Popper, \Knp\Event\Crud\Emitter;
    private $id;
    private $street;
    private $number;
    private $city;
    private $country;
    private $previousOne;
    public function __construct($number, $street, $city, $country, Address $previousOne = null)
    {
        $this->emit(new \Knp\Event\Event\Generic('Created', $c = $this->changeSet(function () use($number, $street, $city, $country, $previousOne) {
            $this->previousOne = $previousOne;
            $this->id = \Rhumsaa\Uuid\Uuid::uuid4();
            $this->number = $number;
            $this->street = $street;
            $this->city = $city;
            $this->country = $country;
        })));
Exemple #2
0
    {
        return ['ProductCreated', 'ProductRenamed'];
    }
    public function ProductCreated(\Knp\Event\Event $event)
    {
        $statement = $this->pdo->prepare('INSERT INTO product ( id, name, created_at ) VALUES ( :id, :name, :created_at );');
        $statement->bindValue('id', $event->id);
        $statement->bindValue('name', $event->name);
        $statement->bindValue('created_at', $event->createdAt->format('Y-m-d'));
        $statement->execute();
    }
    public function ProductRenamed(\Knp\Event\Event $event)
    {
        $statement = $this->pdo->prepare('UPDATE product SET name = :name WHERE id = :id;');
        $statement->bindValue('id', $event->getEmitterId());
        $statement->bindValue('name', $event->name);
        $statement->execute();
    }
}
$dispatcher = new \Knp\Event\Dispatcher();
$dispatcher->add(new RDBMProjector(new \PDO('pgsql:dbname=event_store_projection', null, null, [\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION, \PDO::ATTR_EMULATE_PREPARES => 0])));
//$store = new \Knp\Event\Store\Pdo\Store(
//    new \PDO('pgsql:dbname=event_store', null, null, [
//        \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
//        \PDO::ATTR_EMULATE_PREPARES => 0,
//    ]),
//    $serializer
//);
//$store = new \Knp\Event\Store\InMemory;
$store = new \Knp\Event\Store\Mongo((new \MongoClient())->selectDB('event'), $serializer);
$repository = (new \Knp\Event\Repository\Factory(new \Knp\Event\Store\Concurrency\Optimistic($store), $dispatcher))->create();