PHPackages                             remotelyliving/php-command-bus - 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. remotelyliving/php-command-bus

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

remotelyliving/php-command-bus
==============================

A php query bus for abstracting querying, data loading, and graph building

1.0.0(5y ago)641[2 PRs](https://github.com/remotelyliving/php-command-bus/pulls)MITPHPPHP &gt;=7.4

Since May 26Pushed 3y ago1 watchersCompare

[ Source](https://github.com/remotelyliving/php-command-bus)[ Packagist](https://packagist.org/packages/remotelyliving/php-command-bus)[ RSS](/packages/remotelyliving-php-command-bus/feed)WikiDiscussions master Synced 6d ago

READMEChangelog (1)Dependencies (10)Versions (4)Used By (0)

[![Build Status](https://camo.githubusercontent.com/2736f9b301dd6d23771c4eed411169cce3796516439cec8375c31e065b021bbd/68747470733a2f2f7472617669732d63692e636f6d2f72656d6f74656c796c6976696e672f7068702d636f6d6d616e642d6275732e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/remotelyliving/php-command-bus)[![Total Downloads](https://camo.githubusercontent.com/0260ee4ef4b6f320f0f835291100ba2069a2724cde984ac6e708b03195876af9/68747470733a2f2f706f7365722e707567782e6f72672f72656d6f74656c796c6976696e672f7068702d636f6d6d616e642d6275732f646f776e6c6f616473)](https://packagist.org/packages/remotelyliving/php-command-bus)[![Coverage Status](https://camo.githubusercontent.com/3fa1008d1b76831bd9f02721b60d5f552d10f0f5521374b110893f6485c97b17/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f72656d6f74656c796c6976696e672f7068702d636f6d6d616e642d6275732f62616467652e7376673f6272616e63683d6d6173746572)](https://coveralls.io/github/remotelyliving/php-command-bus?branch=master)[![License](https://camo.githubusercontent.com/c6f3979d95a4aa19f2077fa0b1a02e9ac7043a391be6230a2f80b85b02c8866d/68747470733a2f2f706f7365722e707567782e6f72672f72656d6f74656c796c6976696e672f7068702d636f6d6d616e642d6275732f6c6963656e7365)](https://packagist.org/packages/remotelyliving/php-command-bus)[![Scrutinizer Code Quality](https://camo.githubusercontent.com/34ab0539e6f60ba9bcb5d1bf691720bb25802f8e8456da417ad2d1e4b317af6e/68747470733a2f2f7363727574696e697a65722d63692e636f6d2f672f72656d6f74656c796c6976696e672f7068702d636f6d6d616e642d6275732f6261646765732f7175616c6974792d73636f72652e706e673f623d6d6173746572)](https://scrutinizer-ci.com/g/remotelyliving/php-command-bus/?branch=master)

php-command-bus: 🚍 A Command Bus Implementation For PHP 🚍
=========================================================

[](#php-command-bus--a-command-bus-implementation-for-php-)

### Use Cases

[](#use-cases)

If you want a light weight compliment to your Query Bus for CQRS, hopefully this library helps out.

### Installation

[](#installation)

```
composer require remotelyliving/php-command-bus
```

### Usage

[](#usage)

#### Create the Command Resolver

[](#create-the-command-resolver)

The Resolver can have Handlers added manually or locate them in a PSR-11 Service Container Commands are mapped 1:1 with a handler and are mapped by the Command class name as the lookup key.

```
$resolver = Resolver::create($serviceContainer) // can locate in service container
    ->pushHandler(Commands\ReserveRoom::class, new Handlers\ReserveRoom()) // can locate in a local map {command => handler}
    ->pushHandlerDeferred(Commands\Checkout::class, fn() => new Handlers\Checkout()); // can locate deferred to save un unnecessary object instantiation
```

#### Create the Command Bus

[](#create-the-command-bus)

The Command Bus takes in a Command Resolver and optional PSR-14 Event Dispatcher and pushes whatever Middleware you want on the stack.

```
$resolver = Resolver::create($container);
$commandBus = CommandBus::create($resolver, $psr14EventDispatcher | null)
    ->pushMiddleware($myMiddleware1);

$command = new Commands\ReserveRoom(123);
$commandBus->handle($command);
```

Middleware is any callable. Some base middleware is included: [src/Middleware](https://github.com/remotelyliving/php-command-bus/tree/master/src/Middleware)

That's really all there is to it!

### Command

[](#command)

The Command for this library is left intentionally unimplemented. It's just an object. My suggestion for Command objects is to keep them as a DTO of what you need to perform a command.

An example command might look like this:

```
class ReserveRoom
{
    private User $user;

    private Room $room;

    public function __construct(User $user, Room $room)
    {
        $this->user = $user;
        $this->room = $room;
    }

    public function getUser(): User
    {
        return $this->user;
    }

    public function getRoom(): Room
    {
        return $this->room;
    }
}
```

As you can see, it's just a few getters

### Handler

[](#handler)

The Handlers are where the magic happens.

- Inject what ever Domain Services you need to perform your command in the contstuctor.
- Perform the logic for the command.
- Yield Domain Events that will be automatically dispatched by the PSR14 Dispatcher you pass in to the Bus (When calling a subsequent command from within the handler, make sure your events don't get out of order)
- Execute other commands from within the handler

Going with our ReserveRoom example, a Handler could look like:

```
class ReserveRoom implements Interfaces\Handler
{
    public function handle(object $command, Interfaces\CommandBus $bus)
    {
        $bus->handle(new Commands\MarkRoomAsReserved($command->getRoom()));
        $bus->handle(new Commands\CompleteInvoiceForRoom($command->getUser(), $command->getRoom()));

        yield new Events\RoomWasReserved();
    }
}
```

### Middleware

[](#middleware)

[Middleware](https://github.com/remotelyliving/php-command-bus/tree/master/src/Middleware) that this library ships with. The default execution order is LIFO and the signature very simple.

`public function __invoke(Interfaces\Command $command, callable $next);`

A Middleware must pass the command to the $next callable and execute it to continue the execution of the Command.

#### [CommandLogger](https://github.com/remotelyliving/php-command-bus/blob/master/src/Middleware/QueryLogger.php)

[](#commandlogger)

Helpful for debugging, but best left for dev and stage environments.

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community5

Small or concentrated contributor base

Maturity58

Maturing project, gaining track record

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

Unknown

Total

1

Last Release

2182d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/15091ba12470e0054da94a253c264751ef495c9897c59ea6837e986b31606e63?d=identicon)[remotelyliving](/maintainers/remotelyliving)

---

Tags

commandcommand busbuscqrs

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan, Psalm

Code StylePHP\_CodeSniffer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/remotelyliving-php-command-bus/health.svg)

```
[![Health](https://phpackages.com/badges/remotelyliving-php-command-bus/health.svg)](https://phpackages.com/packages/remotelyliving-php-command-bus)
```

###  Alternatives

[ecotone/ecotone

Supporting you in building DDD, CQRS, Event Sourcing applications with ease.

558549.8k17](/packages/ecotone-ecotone)[civicrm/civicrm-core

Open source constituent relationship management for non-profits, NGOs and advocacy organizations.

728272.9k20](/packages/civicrm-civicrm-core)[honeybee/honeybee

Library for implementing CQRS driven, event-sourced and distributed architectures.

222.1k4](/packages/honeybee-honeybee)

PHPackages © 2026

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