Download the PHP package ignislabs/flare-cqrs without Composer
On this page you can find all versions of the php package ignislabs/flare-cqrs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download ignislabs/flare-cqrs
More information about ignislabs/flare-cqrs
Files in ignislabs/flare-cqrs
Package flare-cqrs
Short Description Flare is a small and easy to use CQRS library.
License MIT
Informations about the package flare-cqrs
Flare CQRS
Flare is a small and easy to use CQRS library.
It drives CQRS by making use of the message bus pattern, separating Queries
(interrogatory messages) from Commands (imperative messages).
This library was greatly inspired by Messaging Flavours article by Mathias Verraes.
What is CQRS?
Originated with Bertrand Meyer's Command and Query Separation principle (CQS):
It states that every method should either be a command that performs an action, or a query that returns data to the caller, but not both. In other words, asking a question should not change the answer. More formally, methods should return a value only if they are referentially transparent and hence possess no side effects.
So, Command and Query Responsibility Segregation (CQRS):
... applies the CQS principle by using separate Query and Command objects to retrieve and modify data, respectively.
Source: Wikipedia
Installation
Through composer:
Usage
FlareCQRS is framework-agnostic, but it's really easy to bootstrap and use.
Bootstrapping
Usage
Now you can use the buses to dispatch any message, like so:
Message (Query & Command) classes
Your Message classes are simple DTO objects, so there are no rules or contracts to use, they can be whatever you like.
You can, however, take advantage of the DataAccessorTrait
. With it you can
have automatic accessor properties for your message classes. The trait defines
a data
private property, a get
accessor method and the __get
magic method
so you can access the data as properties:
Handlers
A handler can be anything, as long as it is callable
. Although, you
will probably want to make them classes. In order to make a class be
callable, just implement the __invoke
method in it.
The __invoke
method will receive an instance of the corresponding
command.
This way, the classes are 100% yours, no hard dependency on this library whatsoever and you can typehint freely.
Let's see a quick example:
Middlewares
You can create middlewares to interact with the messages before they reach their respective handlers.
Middlewares, same as with the Handlers, are callable
s, but you might
prefer them to be classes, by using the same __invoke
strategy.
You can pass your middlewares globally to your buses on instantiation as the last parameters like so:
Or you can add/replace the middlewares on a one time basis:
Buses are immutable, so adding or replacing middlewares on a bus will always return a new bus instance, so any subsequent calls to your buses will not be affected by these middleware changes.