• This repository has been archived on 21/Feb/2022
  • Stars
    star
    553
  • Rank 80,462 (Top 2 %)
  • Language
    PHP
  • License
    MIT License
  • Created over 12 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Small convention based CQRS library for PHP

LiteCQRS for PHP

Small naming-convention based CQRS library for PHP (loosely based on LiteCQRS for C#) that relies on the MessageBus, Command, EventSourcing and Domain Event patterns.

Build Status (Master)

NOTE Use the 1.1 branch, as the dev-master is currently in heavy refactoring.

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 to notify the read model about changes in the write model.

LiteCQRS uses the command pattern and a central message bus service that finds the corresponding handler to execute a command. A command is just a class with some properties describing it, it can optionally implement LiteCQRS\Command.

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 LiteCQRS\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

From 1.0 to 1.1

  • Extending LiteCQRS\Command and LiteCQRS\DomainEvent is NOT required anymore. In fact you can use any class as command or event. The naming conventions alone make sure command handlers and event listeners are detected.

  • JMS Serializer Plugin cannot "detach" aggregate root properties that are part of an event that is serialized anymore. Putting related aggregate roots into an Event is therefore not supported anymore (and not a good idea even with JMS Serializer 0.9 anyways).

Conventions

  • All public methods of a command handler class are mapped to Commands "Command Class Shortname" => "MethodName" when the method and command class shortname match. Implementing an interface for the commands is NOT required (since 1.1)
  • Domain Events are applied to Event Handlers "Event Class Shortname" => "onEventClassShortname". Only if this matches is an event listener registered.
  • Domain Events are applied on Entities/Aggregate Roots "Event Class Shortname" => "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:

  • HelloWorld\GreetingCommand maps to the greeting(GreetingCommand $command) method on the registered handler.
  • HelloWorld\Commands\Greeting maps to the greeting(Greeting $command) method on the registered handler.
  • 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 & Requirements

Use the 1.1 branch, as the dev-master is currently in heavy refactoring.

The core library has no dependencies on other libraries. Plugins have dependencies on their specific libraries.

Install with Composer:

{
    "require": {
        "beberlei/lite-cqrs": "1.1"
    }
}

Workflow

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

  1. You push commands into a CommandBus. Commands are simple objects extending 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 that by creating events (that represent 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 LiteCQRS makes sure that the execution of command and event handlers is never nested, but in sequential linearized order. This prevents independent transactions for each command from affecting each other.

Examples

See examples/ 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

  1. In Memory Command Handlers, no event publishing/observing
<?php
$userService = new UserService();

$commandBus = new DirectCommandBus()
$commandBus->register('MyApp\ChangeEmailCommand', $userService);
  1. In Memory Commands and Events Handlers

This uses LiteCQRS\EventProviderInterface instances to trigger domain events.

<?php
// 1. Setup the Library with InMemory Handlers
$messageBus = new InMemoryEventMessageBus();
$identityMap = new SimpleIdentityMap();
$queue = new EventProviderQueue($identityMap);
$commandBus = new DirectCommandBus(array(
    new EventMessageHandlerFactory($messageBus, $queue)
));

// 2. Register a command service and an event handler
$userService = new UserService($identityMap);
$commandBus->register('MyApp\ChangeEmailCommand', $userService);

$someEventHandler = new MyEventHandler();
$messageBus->register($someEventHandler);
  1. In Memory Commands + Custom Event Queue

LiteCQRS knows about triggered events by asking LiteCQRS\Bus\EventQueue. Provide your own implementation to be independent of your domain objects having to implement EventProviderInterface.

<?php
$messageBus = new InMemoryEventMessageBus();
$queue = new MyCustomEventQueue();

$commandBus = new DirectCommandBus(array(
    new EventMessageHandlerFactory($messageBus, $queue)
));

Usage

To implement a Use Case of your application

  1. Create a command object that receives all the necessary input values. Use public properties and extend LiteCQRS\DefaultCommand to simplify.
  2. Add a new method with the name of the command to any of your services (command handler)
  3. Register the command handler to handle the given command on the CommandBus.
  4. Have your entities implement LiteCQRS\AggregateRoot or LiteCQRS\DomainEventProvider
  5. Use protected method raise(DomainEvent $event) or apply(DomainEvent $event)`` to attach events to your aggregate root objects.

That is all there is for simple use-cases.

If your command triggers events that listeners check for, you should:

  1. Create a domain specific event class. Use public properties to simplify.
  2. Create a event handler(s) or add method(s) to existing event handler(s).

While it seems "complicated" to create commands and events for every use-case. These objects are really dumb and only contain public properties. Using your IDE or editor functionality you can easily generate them in no time. In turn, they will make your code very explicit.

Difference between apply() and raise()

There are two ways to publish events to the outside world.

  • DomainEventProvider#raise(DomainEvent $event) is the simple one, it emits an event and does nothing more.
  • AggregateRoot#apply(DomainEvent $event) requires you to add a method apply$eventName($event) that can be used to replay events on objects. This is used to replay an object from events.

If you don't use event sourcing then you are fine just using raise() and ignoring apply() altogether.

Failing Events

The EventMessageBus prevents exceptions from bubbling up. To allow some debugging of failed event handler execution there is a special event "EventExecutionFailed" that you can listen to. You will get passed an instance of LiteCQRS\Bus\EventExecutionFailed with properties $exception, $service and $event to allow analysing failures in your application.

Extension Points

You should implement your own CommandBus or extend the existing to wire the whole process together exactly as you need it to work.

Plugins

Symfony

Inside symfony you can use LiteCQRS by registering services with lite_cqrs.command_handler or the lite_cqrs.event_handler tag. These services are then autodiscovered for commands and events.

Command- and Event-Handlers are lazily loaded from the Symfony Dependency Injection Container.

To enable the bundle put the following in your Kernel:

new \LiteCQRS\Plugin\SymfonyBundle\LiteCQRSBundle(),

You can enable/disable the bundle by adding the following to your config.yml:

lite_cqrs: ~

Please refer to the SymfonyExample.md document for a full demonstration of using LiteCQRS from within a Symfony2 project.

Monolog

A plugin that logs the execution of every command and handler using Monolog. It includes the type and name of the message, its parameters as json and if its execution succeeded or failed.

The Monolog integration into Symfony registers a specific channel lite_cqrs which you can configure differently from the default channels in Symfony. See the Symfony cookbook for more information.

More Repositories

1

assert

Thin assertion library for use in libraries and business-model
PHP
2,407
star
2

DoctrineExtensions

A set of Doctrine 2 extensions
PHP
1,956
star
3

metrics

Simple library that abstracts different metrics collectors. I find this necessary to have a consistent and simple metrics (functional) API that doesn't cause vendor lock-in.
PHP
316
star
4

composer-monorepo-plugin

Integrates Composer into monolithic repositories with many packages.
PHP
305
star
5

porpaginas

A simple abstraction for paginated and non-paginated results
PHP
159
star
6

AcmePizzaBundle

Acme Form Experimental Bundle
PHP
131
star
7

php-rfc-watch

Interactive voting results for PHP RFC process.
HTML
127
star
8

zf-doctrine

A Zend Framework 1.x and Doctrine 1.2 Integration - UNMAINTAINED
PHP
102
star
9

netbeans-php-enhancements

Netbeans PHP Enhancements, such as PHP Code Sniffer Support
Java
102
star
10

fastcgi-serve

Small webserver to use in front of fast-cgi servers (php-fpm, hhvm, ...)
Go
92
star
11

symfony-minimal-distribution

This contains the code from my blog post http://whitewashing.de/2014/10/26/symfony_all_the_things_web.html
PHP
67
star
12

bankaccount

Sample application used for PHPUnit training.
PHP
46
star
13

vim-php-refactor

Some simple vim refactoring functions for your PHP code
Vim Script
40
star
14

xdebug-trace-gui

Fork of Trace GUI by Thomas Hambach
PHP
35
star
15

license-manager

License Switch Project - Helping open source projects to switch licenses
CSS
34
star
16

Doctrine-Workflow

A Doctrine 2 persistence layer for ezcWorkflow
PHP
33
star
17

DoctrineCodeGenerator

Prototype of an AST based Event Driven CodeGenerator
PHP
31
star
18

http-client-middleware

Missing interfaces for HTTP-Client middlewares using PSR-7 messages.
PHP
28
star
19

Doctrine-ActiveEntity

ActiveRecord ORM (Mod) on top of Doctrine2
PHP
25
star
20

env

PHP PECL extension for loading 12factor env variables from a file
C
24
star
21

Whitewashing

Symfony2 Bundle that powers the www.whitewashing.de blog
PHP
23
star
22

azure-blob-storage

Small platform-independent library to access Microsoft Windows Azure Blob Storage with a Service or a StreamWrapper.
PHP
22
star
23

WhitewashingZFMvcCompatBundle

PHP
21
star
24

zelten

A social network website based on tent.io protocol
JavaScript
17
star
25

TentPHP

A Tent.io client written in PHP
PHP
16
star
26

githubpr_to_jira

Converts Github PR to Jira Issues
PHP
16
star
27

pearanha

Pearanha is deprecated, use Composer intead
PHP
15
star
28

context

DEPRECATED
PHP
15
star
29

WhitewashingLogglyBundle

loggly.com logger for Monolog integrated into Symfony2 - not really maintained anymore
PHP
15
star
30

php-compiletime-poc

Extension that counts the time spent in PHP file compilation during the execution of a script
C
13
star
31

php-ast-tracer-poc

C
13
star
32

doctrine-example-app

Example App Setup for Training/Workshops
PHP
13
star
33

dbdeploy-php

DBDeploy PHP clone
PHP
12
star
34

ZendNavigationBundle

A Zend Navigation Bundle for Symfony2
PHP
11
star
35

phpricot

A forgiving HTML Parsing library written in PHP
PHP
11
star
36

hdrhistogram-php

A PHP extension wrapper for the C hdrhistogram API.
C
10
star
37

symfony-azure-edition

Symfony2 on Windows Azure Edition
PHP
10
star
38

ReviewSquawkBundle

Symfony2 app that provides a static-code-analysis as a service platform
JavaScript
9
star
39

ZetaBundle

A Zeta Components Bundle for Symfony2
PHP
8
star
40

websocket-proxy-example

Example Go microservice that acts as a Websocket proxy for other server applications
Go
6
star
41

stdlib

Some fun
PHP
6
star
42

compilefile-ext

C
5
star
43

php8-benchmark-doctrine

PHP
5
star
44

Zend_Db-Adapter-for-ext-mysql

Legacy applications often work with ext/mysql all over the place. This Zend_Db adapter allows to share the resources and benefit from Zend_Db.
PHP
5
star
45

ZetaWorkflowCouchDB

Zeta Components CouchDB Backend for Workflow
PHP
4
star
46

php-overload-poc

C
4
star
47

php-couch-content-repository

PHP
3
star
48

flow3-doctrine2

prototype hack for doctrine2 as persistence manager for flow3
PHP
3
star
49

funcall

Fork of the pecl extension "funcall"
C
3
star
50

redmine_gherkinviewer

Redmine Plugin: Display feature files from project repository directly in a "Features" tab in the project.
Ruby
3
star
51

AzureTaskDemoBundle

Demo bundle for Azure Functionality with a Symfony+Doctrine application
JavaScript
2
star
52

beberlei

1
star
53

deprecations

PHP
1
star
54

pecl_http

PHP
1
star
55

cj-push-server

PubSubHubBub Server in Clojure
Clojure
1
star
56

interrupt-sampler-poc

C
1
star
57

hdrhistogram-php-stubs

Adding support for hdrhistogram in IDEs and Scrutinizer
PHP
1
star