PHPackages                             lidskasila/glow - 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. lidskasila/glow

ActiveLibrary

lidskasila/glow
===============

Simple \[C\]ommand \[Q\]uery \[R\]esponsibility \[S\]egregation and \[E\]vent \[S\]ourcing library.

v0.9(8y ago)23.4kMITPHPPHP &gt;=7.1

Since Mar 6Pushed 8y ago8 watchersCompare

[ Source](https://github.com/LidskaSila/Glow)[ Packagist](https://packagist.org/packages/lidskasila/glow)[ RSS](/packages/lidskasila-glow/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (10)Dependencies (5)Versions (12)Used By (0)

PHP Glow library
================

[](#php-glow-library)

**This library is to show another way to tackle CQRS and event sourcing, but is not considered as suitable for real production projects. For this purpose rather use [Prooph toolbox](https://github.com/prooph)or [Nette extension for Prooph toolbox](https://github.com/LidskaSila/Prooph). It supports snapshoting, read model projections, is easy to make event replaying and is way more flexible.** This PHP CQRS EventSourcing library is based on Benjamin Eberlei's [LiteCQRS for php](https://github.com/beberlei/litecqrs-php) which was not maintained for quite a long time. This fork is bringing it back to life, but it is not to be considered as a stable library, since there may be BC breaks in the near future.

[![Build Status](https://camo.githubusercontent.com/bd9e774f2c38ff0bd6dd41aa29198cf6909a854b49d33891f9ccfc8f435d2fd1/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f4c6964736b6153696c612f476c6f772e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/LidskaSila/Glow)[![Quality Score](https://camo.githubusercontent.com/d5d6aade1230bc11276c0f479d8a9afa96df403ddc0e5c3f79897fe144e691be/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f4c6964736b6153696c612f476c6f772e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/LidskaSila/Glow)[![Code Coverage](https://camo.githubusercontent.com/3b5f7703ca511d34723eb6db0f70907a027750363a3dc00a26c7fa9096dd7b59/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f4c6964736b6153696c612f476c6f772e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/LidskaSila/Glow)[![Downloads this Month](https://camo.githubusercontent.com/82c5c687049e5f6390d50f13e3593e954ccc331a0c2199ef61f607b95247bc6a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f6c6964736b6173696c612f6772617068716c2d7068702d736368656d612d7061727365722e737667)](https://packagist.org/packages/lidskasila/graphql-php-schema-parser)

Main differences are:

- Minimal required version of PHP is 7.0, it will be 7.1 soon.
- Commanding - to the CommandBus you can register only implementations of ComandHandler (has only handle method). The reason behind this is to enforce explicitness in naming and structuring conventions. When you see XxxCommand and you need to see the implementation, you know there should be XxxCommandHandler implemented somewhere.

Original updated readme
-----------------------

[](#original-updated-readme)

Small naming-convention based CQRS library for PHP (loosely based on [LiteCQRS for C#](https://github.com/danielwertheim/LiteCQRS)) that relies on the MessageBus, Command, EventSourcing and Domain Event patterns.

Terminology
-----------

[](#terminology)

CQS is Command-Query-Separation: A paradigm where read methods never change state and write methods never return data. Build on top, CQRS suggests the separation of read- from write-model and uses the [DomainEvent pattern](http://martinfowler.com/eaaDev/DomainEvent.html) to notify the read model about changes in the write model.

Glow uses the command pattern and a central message bus service that finds the corresponding handler to execute a command. A command must implement `Glow\Commanding\Command`. It should be a plain DTO (data transfer object) with just some properties describing it (imutability is encouraged).

After this object is passed as a parameter into the CommandBus' `handle(Command  $command)` method, CommandBus finds a proper CommandHandler and invokes the same method with the same parameter on it. Enforced convention is that there has to be XxxCommandHandler (implementing CommandHandler) for every XxxCommand.

During the execution of a command, domain events can be triggered. These are again just simple classes with some properties and they can optionally implement `Glow\DomainEvent`.

An event queue knows what domain events have been triggered during a command and then publishes them to an event message bus, where many listeners can listen to them.

Changes
-------

[](#changes)

Conventions
-----------

[](#conventions)

- Each XxxCommand DTO is mapped to XxxCommandHandler when XxxCommand implements Command and XxxCommandHandler implements CommandHandler.
- Domain Events are applied to Event Handlers "Event Class Shortname" =&gt; "onEventClassShortname". An event listener is registered only if this matches.
- Domain Events are applied on Entities/Aggregate Roots "Event Class Shortname" =&gt; "applyEventClassShortname"
- You can optionally extend the `DefaultDomainEvent` which has a constructor that maps its array input to properties and throws an exception if an unknown property is passed.
- There is also a `DefaultCommand` with the same semantics as `DefaultDomainEvent`. Extending this is not required.

Examples:

- `GreetingCommand implements Command` maps to the `handle(GreetingCommand $command)` method on the registered GreetingCommandHandler implementing CommandHandler.
- `HelloWorld\GreetedEvent` is passed to all event handlers that have a method `onGreeted(GreetedEvent $event)`.
- `HelloWorld\Events\Greeted` is passed to all event handlers that have a method `onGreeted(Greeted $event)`.
- `HelloWorld\GreetedEvent` is delegated to `applyGreeted($event)` when created on the aggregate root

Installation &amp; Requirements
-------------------------------

[](#installation--requirements)

Install with [Composer](http://getcomposer.org):

```
composer require lidskasila/glow

```

Workflow
--------

[](#workflow)

These are the steps that a command regularly takes through the Glow stack during execution:

1. You push commands into a `CommandBus`. Commands are simple objects implementing `Command` created by you.
2. The `CommandBus` checks for a handler that can execute your command. Every command has exactly one handler.
3. The command handler changes state of the domain model. It does so by creating events (that represent a state change) and passing them to the `AggregateRoot::apply()` or `DomainEventProvider::raise()` method of your domain objects.
4. When the command is completed, the command bus will check all objects in the identity map for events.
5. All found events will be passed to the `EventMessageBus#publish()` method.
6. The EventMessageBus dispatches all events to observing event handlers.
7. Event Handlers can create new commands again using the `CommandBus`.

Command and Event handler execution can be wrapped in handlers that manage transactions. Event handling is always triggered outside of any command transaction. If the command fails with any exception all events created by the command are forgotten/ignored. No event handlers will be triggered in this case.

In the case of InMemory CommandBus and EventMessageBus Glow makes sure that the execution of commands and event handlers is never nested, but in sequential linearized order. This prevents independent transactions for each command from affecting each other.

\#TODO following is not updated

Examples
--------

[](#examples)

See [examples/](https://github.com/beberlei/litecqrs-php/tree/master/example) for some examples:

1. `example1.php` shows usage of the Command- and EventMessageBus with one domain object
2. `example2_event.php` shows direct usage of the EventMessageBus inside a command
3. `example3_sequential_commands.php` demonstrates how commands are processed sequentially.
4. `tictactoe.php` implements a tic tac toe game with CQRS.
5. `SymfonyExample.md` shows `example1.php` implemented within the scope of a Symfony2 project.

Setup
-----

[](#setup)

1. In Memory Command Handlers, no event publishing/observing

```
