Download the PHP package zumba/cqrs without Composer
On this page you can find all versions of the php package zumba/cqrs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package cqrs
Short Description CQRS Library
License MIT
Homepage https://tech.zumba.com
Informations about the package cqrs
Zumba CQRS
Command Query Responsibility Segregation (CQRS) is an established design pattern. This library aims to implement the plumbing needed to create CQRS commands and queries.
- Command Bus Usage
- Creating a command
- Creating a command handler
- Dispatching a command
- Query Bus Usage
- Creating a query handler
- Dispatching a query
- Providers
- Class provider
- Method provider
- Simple dependency provider
- Create a custom provider
- Middleware
- Logger middleware
- Custom middleware
Command Bus Usage
The command bus allows commands with handlers to be dispatched with the result being either a Success
or Failure
.
By design, the command must not return any data.
Creating a command
Creating a command DTO to dispatch to the command bus requires us to extend the abstract Command
class.
Creating a command handler
With the MyCommand
created from the Creating a command section, we need to handle this command to perform an action.
Tip: The command handler is a great place to introduce any dependencies to be injected.
Dispatching a command
With both a command and handler for that command created, we're ready to dispatch our command to the command bus.
A default batteries-included command bus can be instanced that we can dispatch our command DTOs to the appropriate handlers.
Query Bus Usage
Similar to the command bus, the query bus operates with a query DTO with an accompanying handler dispatched to the query bus in order to get resulting data.
Creating a query
Creating a query DTO to dispatch to the query bus requires us to extend the abstract Query
class.
Create a query handler
Query handlers are designed to return a query response depending on the kind of data to return.
In the above created MyQuery
DTO, let's assume that this is retrieving a key-value result for a specific entity.
We would build our response using the Map
response type:
Dispatching a query
As with dispatching a command, we can use the batteries-included query bus which can be included using the QueryBusTrait
.
Providers
Providers offer the ability for the command bus to be able to supply a handler for a DTO dispatched to the bus.
zumba\cqrs
provides several providers out of the box.
Class Provider
The Zumba\CQRS\Provider\ClassProvider
can be used to construct handlers for a DTO via a handler factory.
This can be used when the handlers have complex dependencies (ie dependencies that require configuration).
To utilize this, create a handler factory in the same namespace as the DTO. Assume our DTO from the create a command section
of MyCommand
, the handler factory would look like this:
Method Provider
The Zumba\CQRS\Provider\MethodProvider
is similar to the ClassProvider
except the handler itself can serve as its own factory.
The MyCommandHandler
can be adapted from the create a command handler section:
Simple Dependency Provider
In cases where all dependencies can be instantiated without parameters, the Zumba\CQRS\Provider\SimpleDependencyProvider
can be used to construct the handler without the need of a HandlerFactory
.
Create a custom provider
To create your own custom provider, implement the Zumba\CQRS\Provider
interface and create the bus with the custom provider.
You can also provide additional providers to fallback on if your CustomProvider can't accommodate the DTO.
Middleware
The CQRS library is equipped to allow for middleware to be attached to a command or query bus to allow for generic actions to occur for any or all DTOs.
Logger middleware
The logger middleware allows logging all DTOs that are dispatched to through a bus.
Custom middleware
Custom middleware can be included in the MiddlewarePipeline
as long as it implements the Middleware
interface.
The handle
method will accept a DTO and a callable
to which to continue the process.
See the Logger
middleware as an example.