PHPackages                             mad654/php-event-store - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. mad654/php-event-store

ActiveLibrary

mad654/php-event-store
======================

Provides classes to express object state changes as events and store them in file based eventstreams

1.0.1(7y ago)03MITPHPPHP ^7.2

Since Mar 9Pushed 7y ago1 watchersCompare

[ Source](https://github.com/mad654/php-event-store)[ Packagist](https://packagist.org/packages/mad654/php-event-store)[ RSS](/packages/mad654-php-event-store/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependencies (5)Versions (3)Used By (0)

PHP Event Store
===============

[](#php-event-store)

Plain php event store implementation for easy persistence by utilizing [event sourcing](https://de.wikipedia.org/wiki/Event_Sourcing).

It provides classes to express object state changes as events and store them in file based eventstreams.

Installation
------------

[](#installation)

You can install mad654/php-event-store via composer by adding `"mad654/php-event-store": "dev-master"` as requirement to your composer.json.

By Example
----------

[](#by-example)

For a full working example take a look at [`src/example`](src/example). In your vagrant box you can use it like this:

```
# create storage folder
mkdir -p /tmp/var/eventstore-example/

# create a new instance of LightSwitch with id 'kitchen'
src/example/bin/console.php init kitchen

# switch on/off on 'kitchen'
src/example/bin/console.php switch kitchen --on
src/example/bin/console.php switch kitchen --off

# render history of 'kitchen'
src/example/bin/console.php history kitchen
```

### Step by step

[](#step-by-step)

Let's take this little example to get in touch with all the new stuff. Lets asume you want to control the light in your Kittchen and for this you have build some switch. All you need is an object which can control the state and keeps its current state over mutlitple requests:

```
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;
    }
}
```

This is a good beginning, but now you need a way to persist the state.

#### EventSourcedObject

[](#eventsourcedobject)

Instead of creating a database you can extend your class to implement the EventSourcedObject interface. An EventSourcedObject is simply an object which should be available in his current state in the next request and for this it can publish its events as a stream and can be build from scratch based on the events:

```
