1. Go to this page and download the library: Download mad654/php-event-store 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/ */
mad654 / php-event-store example snippets
class LightSwitch {
private $state = false;
public function isOn(): bool {
return $this->state;
}
public function switchOn()
{
if ($this->state === true) return;
// do some stuff which does the hard work
$this->state = true;
}
public function switchOff()
{
if ($this->state === false) return;
// do some stuff which does the hard work
$this->state = false;
}
}
use mad654\eventstore\Event;
use mad654\eventstore\Event\StateChanged;
use mad654\eventstore\EventSourcedObject;
use mad654\eventstore\EventStream\AutoTrackingEventSourcedObjectTrait;
use mad654\eventstore\SubjectId;
class LightSwitch implements EventSourcedObject
{
use AutoTrackingEventSourcedObjectTrait;
/**
* @var bool
*/
private $state;
public function __construct(SubjectId $id)
{
$this->init($id, ['state' => false]);
}
public function isOn(): bool
{
return $this->state;
}
public function switchOn(): void
{
if ($this->state) return;
$this->record(new StateChanged($this->subjectId(), ['state' => true]));
}
public function switchOff(): void
{
if (!$this->state) return;
$this->record(new StateChanged($this->subjectId(), ['state' => false]));
}
public function on(Event $event): void
{
$this->state = $event->get('state', $this->state);
}
}
use mad654\eventstore\FileEventStream\FileEventStreamFactory;
use mad654\eventstore\EventObjectStore;
$factory = new FileEventStreamFactory("/tmp/eventstore-example");
$store = new EventSourcedObjectStore($factory);
use mad654\eventstore\FileEventStream\FileEventStreamFactory;
use mad654\eventstore\EventObjectStore;
$factory = new FileEventStreamFactory("/tmp/eventstore-example");
$store = new EventSourcedObjectStore($factory);
$switch = new LightSwitch('kitchen');
$store->attach($switch);