PHPackages                             phpgears/cqrs - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. phpgears/cqrs

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

phpgears/cqrs
=============

CQRS base

0.3.3(6y ago)44.2k3MITPHPPHP ^7.1CI failing

Since Sep 24Pushed 5y ago2 watchersCompare

[ Source](https://github.com/phpgears/cqrs)[ Packagist](https://packagist.org/packages/phpgears/cqrs)[ Docs](https://github.com/phpgears/cqrs)[ RSS](/packages/phpgears-cqrs/feed)WikiDiscussions master Synced 2w ago

READMEChangelog (1)Dependencies (16)Versions (9)Used By (3)

[![PHP version](https://camo.githubusercontent.com/d0b5687c6812c5d52d86a548e09db527eeb7860f82adbb677de00a36ddbed1b4/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f5048502d253345253344372e312d3838393242462e7376673f7374796c653d666c61742d737175617265)](http://php.net)[![Latest Version](https://camo.githubusercontent.com/37d8416ec470fd736136b9b37424a000e9b56ec0ec04ec6a06ad83ed916d7aa1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/cqrs)[![License](https://camo.githubusercontent.com/09821134ec84e10c288ce4d9e169e18e9b63781c7176ed22481667ef43533324/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://github.com/phpgears/cqrs/blob/master/LICENSE)

[![Build Status](https://camo.githubusercontent.com/ee1c9dd6eaafc5fa468f397302942aaa496062b34e2f7cc938ae69b8bf5c43fd/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f636f6d2f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.com/github/phpgears/cqrs)[![Style Check](https://camo.githubusercontent.com/b1da94fefabbe143eedbd7b15691ec849e16dc9ab71ea764381f70a865de59dd/68747470733a2f2f7374796c6563692e696f2f7265706f732f3134393033373535322f736869656c64)](https://styleci.io/repos/149037552)[![Code Quality](https://camo.githubusercontent.com/02e3ccdfb55927b4ee7af4bca092447582e064f884a2c75568a67594871698b5/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://scrutinizer-ci.com/g/phpgears/cqrs)[![Code Coverage](https://camo.githubusercontent.com/cfbd7315cab8834b2bed6feb3d30d831c9690c5d406d0dd93d5cd85ea1577395/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/github/phpgears/cqrs)

[![Total Downloads](https://camo.githubusercontent.com/18fc6c5195c32fdc2f31be61c42186bff348c4643afa8c0c662453d21240962e/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/cqrs/stats)[![Monthly Downloads](https://camo.githubusercontent.com/fe35eb4c30003a4b69161fce5b699a7ba0e3e2cd6bef3c38f68614e2c6cff3b5/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f646d2f70687067656172732f637172732e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/phpgears/cqrs/stats)

CQRS
====

[](#cqrs)

CQRS base classes and handling interfaces

This package only provides the building blocks to CQRS

Installation
------------

[](#installation)

### Composer

[](#composer)

```
composer require phpgears/cqrs

```

Usage
-----

[](#usage)

Require composer autoload file

```
require './vendor/autoload.php';
```

### Commands

[](#commands)

Commands are DTOs that carry all the information for an action to happen

You can create your own by implementing `Gears\CQRS\Command` or extend from `Gears\CQRS\AbstractCommand` which ensures command immutability and payload is composed only of **scalar values** which is a very interesting capability. AbstractCommand has a protected constructor forcing you to create custom static named constructors

```
use Gears\CQRS\AbstractCommand;

class CreateUserCommand extends AbstractCommand
{
    public static function fromPersonalData(
        string $name,
        string lastname,
        \DateTimeImmutable $birthDate
    ): self {
        return new self([
            'name' => $name,
            'lastname' => $lastname,
            'birthDate' => $birthDate->format('U'),
        ]);
    }
}
```

In case of a command without any payload you could extend `Gears\CQRS\AbstractEmptyCommand`

```
use Gears\CQRS\AbstractEmptyCommand;

class CreateUserCommand extends AbstractEmptyCommand
{
    public static function instance(): self {
        return new self();
    }
}
```

#### Async commands

[](#async-commands)

Having command assuring all of its payload is composed only of scalar values proves handy when you want to delegate command handling to a message queue system such as RabbitMQ, Gearman or Apache Kafka, serializing/deserializing scalar values is trivial in any format and language

Asynchronous behaviour must be implemented at CommandBus level, command bus must be able to identify async commands (a map of commands, implementing an interface, by a payload parameter, ...) and enqueue them

If you want to have asynchronous behaviour on your CommandBus have a look [phpgears/cqrs-async](https://github.com/phpgears/cqrs-async), there you'll find all the necessary pieces to start your async command bus, for example using queue-interop with [phpgears/cqrs-async-queue-interop](https://github.com/phpgears/cqrs-async-queue-interop)

### Queries

[](#queries)

Queries are DTOs that carry all the information for a request to be made to the data source

You can create your own by implementing `Gears\CQRS\Query` or extend from `Gears\CQRS\AbstractQuery` which ensures query immutability and payload is composed only of scalar values. AbstractQuery has a protected constructor forcing you to create a custom static named constructors

```
use Gears\CQRS\AbstractQuery;

class FindUserQuery extends AbstractQuery
{
    public static function fromName(string $name): self
    {
        return new self(['name' => $name]);
    }
}
```

In case of a query without any payload you could extend `Gears\CQRS\AbstractEmptyQuery`

```
use Gears\CQRS\AbstractEmptyQuery;

class FindAllUsersQuery extends AbstractEmptyQuery
{
    public static function instance(): self {
        return new self();
    }
}
```

### Handlers

[](#handlers)

Commands and Queries are handed over to `Gears\CQRS\CommandHandler` and `Gears\CQRS\QueryHandler` respectively on their corresponding buses

`AbstractCommandHandler` and `AbstractQueryHandler` are provided in this package, this abstract classes verifies the type of the command/query so you can focus only on implementing the handling logic

```
class CreateUserCommandHandler extends AbstractCommandHandler
{
    protected function getSupportedCommandType(): string
    {
        return CreateUserCommand::class;
    }

    protected function handleCommand(Command $command): void
    {
        /* @var CreateUserCommand $command */

        $user = new User(
            $command->getName(),
            $command->getLastname(),
            $command->getBirthDate()
        );

        // ...
    }
}

class FindUserQueryHandler extends AbstractQueryHandler
{
    protected function getSupportedQueryType(): string
    {
        return FindUserQuery::class;
    }

    protected function handleCommand(Query $query): DTO
    {
        /* @var FindUserQuery $query */

        // Retrieve user from persistence by it's name $query->getName()

        return new UserDTO(/* parameters */);
    }
}
```

Have a look at [phpgears/dto](https://github.com/phpgears/dto) fo a better understanding of how commands and queries are built out of DTOs and how they hold their payload

### Buses

[](#buses)

Only `Gears\CQRS\CommandBus` and `Gears\CQRS\QueryBus` interfaces are provided, you can easily use any of the good bus libraries available out there by simply adding an adapter layer

#### Implementations

[](#implementations)

CQRS buses implementations currently available

- [phpgears/cqrs-symfony-messenger](https://github.com/phpgears/cqrs-symfony-messenger) uses Symfony's Messenger
- [phpgears/cqrs-tactician](https://github.com/phpgears/cqrs-tactician) uses League's Tactician

Contributing
------------

[](#contributing)

Found a bug or have a feature request? [Please open a new issue](https://github.com/phpgears/cqrs/issues). Have a look at existing issues before.

See file [CONTRIBUTING.md](https://github.com/phpgears/cqrs/blob/master/CONTRIBUTING.md)

License
-------

[](#license)

See file [LICENSE](https://github.com/phpgears/cqrs/blob/master/LICENSE) included with the source code for a copy of the license terms.

###  Health Score

29

—

LowBetter than 57% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity21

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity52

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~95 days

Recently: every ~110 days

Total

7

Last Release

2263d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/4c50421f1ab4148354dc2dd5dcaba168656b17ea913b310d112deb39a6f73ca1?d=identicon)[juliangut](/maintainers/juliangut)

---

Top Contributors

[![juliangut](https://avatars.githubusercontent.com/u/1104131?v=4)](https://github.com/juliangut "juliangut (16 commits)")

---

Tags

querycommandimmutablecqrs

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/phpgears-cqrs/health.svg)

```
[![Health](https://phpackages.com/badges/phpgears-cqrs/health.svg)](https://phpackages.com/packages/phpgears-cqrs)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
