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

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

andrewdyer/command-bus
======================

A framework-agnostic PHP library for dispatching commands to handlers, with support for a configurable middleware pipeline

1.0.1(1w ago)011[2 issues](https://github.com/andrewdyer/command-bus/issues)MITPHPPHP ^8.3CI passing

Since May 25Pushed 1w agoCompare

[ Source](https://github.com/andrewdyer/command-bus)[ Packagist](https://packagist.org/packages/andrewdyer/command-bus)[ Docs](https://github.com/andrewdyer/command-bus)[ RSS](/packages/andrewdyer-command-bus/feed)WikiDiscussions main Synced 1w ago

READMEChangelog (3)Dependencies (3)Versions (4)Used By (0)

Command Bus
===========

[](#command-bus)

A framework-agnostic PHP library for dispatching commands to handlers, with support for a configurable middleware pipeline.

[![Latest Stable Version](https://camo.githubusercontent.com/91dbf082a7a7bfbd3b9ece7c8668e7368de237a1fff5e187adb8d9a44b2b3268/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f636f6d6d616e642d6275732f763f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/command-bus)[![Total Downloads](https://camo.githubusercontent.com/147a923f062ae8c5bb33eba87c9c5d5724a9496d5edd4aca4026d6a06314c596/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f636f6d6d616e642d6275732f646f776e6c6f6164733f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/command-bus)[![License](https://camo.githubusercontent.com/7fd35173eb5fb8fbae3882f324d8a336cb945d03def868ed4a5629ea4e431e8f/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f636f6d6d616e642d6275732f6c6963656e73653f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/command-bus)[![PHP Version Require](https://camo.githubusercontent.com/1332483967e817f276846cbd9d6720d3b846e5339c568e78ce351e579c818e04/687474703a2f2f706f7365722e707567782e6f72672f616e64726577647965722f636f6d6d616e642d6275732f726571756972652f7068703f7374796c653d666c61742d737175617265)](https://packagist.org/packages/andrewdyer/command-bus)

Introduction
------------

[](#introduction)

This library provides a framework-agnostic command bus for PHP applications, dispatching commands to their registered handlers through a configurable middleware pipeline, with a PSR-3 compatible logging middleware included out of the box.

Prerequisites
-------------

[](#prerequisites)

- **[PHP](https://www.php.net/)**: Version 8.3 or higher is required.
- **[Composer](https://getcomposer.org/)**: Dependency management tool for PHP.

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

[](#installation)

```
composer require andrewdyer/command-bus
```

Getting Started
---------------

[](#getting-started)

### 1. Create a command

[](#1-create-a-command)

Create a command as a plain class carrying the data required to perform an operation:

```
class CreateUserCommand
{
    public function __construct(
        public readonly string $firstName,
        public readonly string $lastName,
        public readonly string $email,
    ) {}
}
```

### 2. Create a handler

[](#2-create-a-handler)

Create a handler with a `handle()` method that processes the command:

```
class CreateUserHandler
{
    public function handle(CreateUserCommand $command): mixed
    {
        // Handle the command...

        return $user;
    }
}
```

### 3. Create the command bus and register the handler

[](#3-create-the-command-bus-and-register-the-handler)

Create a `CommandBus` instance and register the handler against the command class it should handle:

```
use AndrewDyer\CommandBus\CommandBus;

$bus = new CommandBus();

$bus->register(CreateUserCommand::class, new CreateUserHandler());
```

Usage
-----

[](#usage)

Once the command bus is configured, commands can be dispatched to their registered handlers. If no handler is registered for a given command, a `HandlerNotFoundException` is thrown.

### Dispatching a command

[](#dispatching-a-command)

```
$user = $bus->dispatch(new CreateUserCommand(
    firstName: 'John',
    lastName: 'Doe',
    email: 'john.doe@example.com',
));
```

### Adding middleware

[](#adding-middleware)

Middleware intercepts commands before they reach the handler, allowing cross-cutting concerns such as logging, transactions, or validation to be applied consistently across all commands.

#### Logging middleware

[](#logging-middleware)

A `LoggingMiddleware` is included out of the box. It accepts any PSR-3 compatible logger and logs the command class name before and after dispatch:

```
use AndrewDyer\CommandBus\Middleware\LoggingMiddleware;

$bus->addMiddleware(new LoggingMiddleware($logger));
```

#### Custom middleware

[](#custom-middleware)

Custom middleware defines an `execute()` method that receives the command and a `$next` closure to pass control down the pipeline:

```
class TransactionMiddleware
{
    public function execute(object $command, \Closure $next): mixed
    {
        // Begin transaction...

        try {
            $result = $next($command);
        } catch (\Throwable $e) {
            // Rollback transaction...

            throw $e;
        }

        // Commit transaction...

        return $result;
    }
}
```

Middleware is executed in the order it is registered, so the first middleware added is the first to intercept the command:

```
$bus->addMiddleware(new TransactionMiddleware());
$bus->addMiddleware(new LoggingMiddleware($logger));
```

License
-------

[](#license)

Licensed under the [MIT license](https://opensource.org/licenses/MIT) and is free for private or commercial projects.

###  Health Score

43

—

FairBetter than 89% of packages

Maintenance98

Actively maintained with recent releases

Popularity7

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity50

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 ~1 days

Total

3

Last Release

12d ago

Major Versions

0.1.0 → 1.0.02026-05-26

### Community

Maintainers

![](https://www.gravatar.com/avatar/666597ea6e46748a89fe8764d1a45b4d0da97daf1bb1e9770ea34ae41f706d08?d=identicon)[andrewdyer](/maintainers/andrewdyer)

---

Top Contributors

[![andrewdyer](https://avatars.githubusercontent.com/u/8114523?v=4)](https://github.com/andrewdyer "andrewdyer (9 commits)")

---

Tags

commandcommand-busframework-agnosticmiddlewarephppipelinephpmiddlewarecommandcommand busframework agnosticpipeline

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[ecotone/ecotone

Enterprise architecture layer for Laravel and Symfony — CQRS, Event Sourcing, Durable Workflows (Sagas, Orchestrators), Projections, and Outbox messaging via PHP attributes.

562565.8k41](/packages/ecotone-ecotone)[jaxon-php/jaxon-core

Jaxon is an open source PHP library for easily creating Ajax web applications

73147.2k29](/packages/jaxon-php-jaxon-core)[rumenx/php-seo

AI-powered, framework-agnostic PHP package for automated SEO optimization. Intelligently generates meta tags, titles, descriptions, and alt texts using configurable AI providers or manual patterns.

113.7k](/packages/rumenx-php-seo)

PHPackages © 2026

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