Download the PHP package bartoszbartniczak/cqrs without Composer
On this page you can find all versions of the php package bartoszbartniczak/cqrs. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download bartoszbartniczak/cqrs
More information about bartoszbartniczak/cqrs
Files in bartoszbartniczak/cqrs
Package cqrs
Short Description Command Query Responsibility Segregation pattern applied in PHP.
License Apache 2.0
Informations about the package cqrs
BartoszBartniczak / CQRS
Command Query Responsibility Segregation in PHP
Table of contents
- Preface
- Components
- Command
- Query
- Command Handler
- Example
- CommandBus
- How to register CommandHandler?
- How command is executed?
- How query is executed?
- Example
- Tests
- Unit tests
Preface
If you do not know what Command Query Responsibility Segregation is, you shoud read a very good article written by Martin Fowler. To read the article click here.
This library is an implementation of this pattern in PHP.
Below are described main components of the library.
Components
Command
Command usualy represents some Domain logic. It can contain data validation, data procesing, etc.. The result of the Command usualy is saved in database. E.g. RegisterUser, SendEmail, etc.. You should treat the Command as a "data holder". In the constructor parameters, you should pass all the data required to handle the Command.
Query
Query is a special type of Command. It looks for data in the Repository, and returs it as a result. E.g. FindUser, FindProduct, etc..
Command Handler
It handles the Command. In this object you shoud use the parameters passed in the Command constructor. In this object you can validate, change and process the data. CommandHandlers can be registered in CommandBus. See section [#how-to-register-commandhandler].
Example
CommandBus
CommandBus can receive Commands and execute them using CommandHandlers. To do that you need to register CommandHandler.
How to register CommandHandler?
Now you can execute the Command using CommandBus:
How command is executed?
After you pass the Command for execution, CommandBus is looking for proper CommandHandler to handle the Command. If the CommandHandler return data, it may be saved in the Repository. CommandHandler may pass another Commands to the CommandBus for further execution.
How query is executed?
You can pass Query for execution to the CommandBus. CommandBus looks for CommandHandler. In the handle() method, you can find data in Repository, and ten return as a Result. CommandBus knows that, in result of execution of Query, you return some results, so it saves it in the output. The output is returned as the result of the execute() method.
Example
Tests
Unit tests
To run unit tests execute command: