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

ActiveLibrary

vshf/php-bus
============

Simple PHP command-bus/query-bus system

v2.0.1(5mo ago)022MITPHPPHP &gt;=8.3.0

Since May 3Pushed 5mo ago1 watchersCompare

[ Source](https://github.com/VonStroheim/vshf-php-bus)[ Packagist](https://packagist.org/packages/vshf/php-bus)[ RSS](/packages/vshf-php-bus/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (5)Dependencies (3)Versions (6)Used By (0)

VSHF PHP Command Bus
====================

[](#vshf-php-command-bus)

A lightweight and expressive Command/Query Bus for PHP.

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

[](#installation)

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

Usage
-----

[](#usage)

Instantiate the Bus instance:

```
$bus = new \VSHF\Bus\Bus();
```

### Commands

[](#commands)

Dispatching a command:

```
$command = new MyCommand($someParamsIfAny);

$bus->dispatch($command);
```

Each command must have a corresponding handler in the same namespace:

```
MyCommand.php
MyCommandHandler.php

```

### Queries

[](#queries)

The bus supports query handlers via `ask()`:

```
$result = $bus->ask(new GetUserQuery($id));
```

Query handlers must implement `QueryHandlerInterface`, and the query class must declare its expected result type:

```
public function getResultType(): string
{
    return User::class;
}
```

The bus validates the returned type at runtime.

Middleware
----------

[](#middleware)

Registering a middleware:

```
$bus->addMiddleware(MyMiddleware::class);
```

The middleware class must implement *MiddlewareInterface*.

```
class MyMiddleware implements \VSHF\Bus\MiddlewareInterface {

    public function before() : void
    {
        //  You can access:
        //  $this->$command or $this->query
        //  $this->agent_type
        //  $this->agent_id

        $this->next(); // Omit this to stop command/query execution.
    }

    public function after() : void
    {
        //  Runs after the handler.
        //
        //  You can access:
        //  $this->$command or $this->query
        //  $this->agent_type
        //  $this->agent_id
    }
}
```

### Middleware Priority

[](#middleware-priority)

Higher values = later execution:

```
$bus->addMiddleware(MyMiddleware::class, 99);
```

Subscriptions
-------------

[](#subscriptions)

Handlers can be explicitly registered:

```
$bus->subscribeCommand(MyCommand::class, MyCommandHandler::class);
$bus->subscribeQuery(GetUserQuery::class, GetUserQueryHandler::class);
```

Retrieve them:

```
$bus->getCommandSubscriptions();
$bus->getQuerySubscriptions();
```

Breaking Changes (v2.0)
-----------------------

[](#breaking-changes-v20)

Version 2.0 introduces several important updates:

### 1. Middleware `after()` now receives the handler result

[](#1-middleware-after-now-receives-the-handler-result)

Old:

```
public function after(): void
```

New:

```
public function after($result = null): void
```

All middleware implementations must update their method signature.

### 2. Full Query Bus Support

[](#2-full-query-bus-support)

A complete query pipeline has been added:

- New method: ask()
- Query subscriptions
- Query handler interfaces
- Result type validation
- Middleware support for queries

Older patterns are not compatible.

### 3. `getSubscriptions()` removed

[](#3-getsubscriptions-removed)

Replaced by explicit methods:

- `getCommandSubscriptions()`
- `getQuerySubscriptions()`

Changelog
=========

[](#changelog)

\[2.0.0\] — 2025-11-20
----------------------

[](#200--2025-11-20)

Major release with breaking changes and new features.

### Added

[](#added)

- **Query Bus Support**

    - New `ask()` method.
    - Introduced `QueryInterface` and `QueryHandlerInterface`.
    - Added query subscriptions via `subscribeQuery()`.
    - Query handlers validated against the expected return type using `getResultType()`.
- **Middleware Enhancements**

    - Middleware `after()` now receives the handler result.
    - Middleware is executed for both commands and queries.
- **New Introspection Methods**

    - `getCommandSubscriptions()`
    - `getQuerySubscriptions()`
    - `getMiddlewares()` (sorted by priority)

### Changed (Breaking)

[](#changed-breaking)

- **Middleware signature change**
    - `after()` must now be declared as: ```
        public function after($result = null): void
        ```

### Removed

[](#removed)

- Implicit query behavior from v1 (no longer compatible).
- Deprecated `getSubscriptions()`.

### Migration Notes

[](#migration-notes)

- Update all middleware implementations to the new `after($result = null)` signature.
- Replace any use of `getSubscriptions()` with:
    - `getCommandSubscriptions()`
    - `getQuerySubscriptions()`

License
=======

[](#license)

This project is open-sourced software licensed under the [MIT license](https://opensource.org/MIT)

###  Health Score

40

—

FairBetter than 88% of packages

Maintenance70

Regular maintenance activity

Popularity7

Limited adoption so far

Community9

Small or concentrated contributor base

Maturity63

Established project with proven stability

 Bus Factor1

Top contributor holds 80% 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 ~233 days

Total

5

Last Release

178d ago

Major Versions

v1.0.2 → 2.0.02025-11-20

PHP version history (2 changes)v1.0.0PHP &gt;=7.2.0

2.0.0PHP &gt;=8.3.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/a211279bbd826007b157f4b7773768d1fe5965bb5e9476f09acba7aa837a2c16?d=identicon)[VonStroheim](/maintainers/VonStroheim)

---

Top Contributors

[![emanuele-v](https://avatars.githubusercontent.com/u/203672591?v=4)](https://github.com/emanuele-v "emanuele-v (4 commits)")[![VonStroheim](https://avatars.githubusercontent.com/u/29458049?v=4)](https://github.com/VonStroheim "VonStroheim (1 commits)")

---

Tags

phpquerycommandbus

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

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

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

###  Alternatives

[apicart/fql

Filter Query Language

1110.6k](/packages/apicart-fql)

PHPackages © 2026

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