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

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

kodcube/command-bus
===================

Middleware Command Bus

211PHP

Since Jul 17Pushed 9y ago1 watchersCompare

[ Source](https://github.com/kodcube/command-bus)[ Packagist](https://packagist.org/packages/kodcube/command-bus)[ RSS](/packages/kodcube-command-bus/feed)WikiDiscussions master Synced 2mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

Middleware Based Command Bus
============================

[](#middleware-based-command-bus)

A simple command bus that uses the middleware pattern for processing commands.

It has been designed to leaverage Dependency Injection with Autowiring support. All of it's dependencies and be configured &amp; overridden by a dependency injection container

### Main Features

[](#main-features)

- All Dependancies are injected at construction
- All Dependencies are based on interfaces
- Route Commands differently based on custom middleware
- Use simple arrays for middleware configuration
- Uses Dependency Injection Container when loading middleware
- Uses Dependency Injection Container when loading handlers

### Limitations

[](#limitations)

- Does not inject dependencies for methods other than \_\_constructor
- Does not inject dependencies for setters

### Requirements

[](#requirements)

- PHP 7
- Container-Interop - Dependancy Injection Container
- GunnaPHP\\Invoker

### Optional

[](#optional)

- KodCube\\DependencyInjection - Dependancy Injection Container
- KodCube\\MessageBus - Dispatching commands to background processes

Usage
-----

[](#usage)

- [Create Command Bus](#create-command-bus)
- [Dispatch Command](#dispatch-command)
- [Override Command Handler](#override-command-handler)
- [Multiple Handlers](#multiple-handlers)

### Create Command Bus

[](#create-command-bus)

This will create a command bus with the default handler detection

```
$commandbus = new KodCube\CommandBus\CommandBus(
			        new KodCube\CommandBus\MiddlewareManager(
			        	new KodCube\DependencyInjection\Container()
			        )
              );
```

or using dependancy injection container

```
DI Config
[
	MiddlewareManagerInterface::class => MiddlewareManager::class,
	ContainerInterface::class => Container::class
]

$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);
```

### Dispatch Command

[](#dispatch-command)

Assuming you have the following command and handler

Command Class: MyCommand

Handler Class: MyHandler

```
$command = new MyCommand();

$response = $commandbus($command);
or
$response = $commandbus->handle($command);
```

is the equivalant to

```
$command = new MyCommand();
$handler = new MyHandler();

$response = $handler($command);
```

### Override Command Handler

[](#override-command-handler)

The default handler middleware allows you to override which handler is responsible for handling the command

```
$commandbus = new KodCube\CommandBus\CommandBus(
			        new KodCube\CommandBus\MiddlewareManager(
			        	new KodCube\DependencyInjection\Container(),
			        	new KodCube\CommandBus\Middleware\Handler(
			        		[
			        			MyCommand::class => MyNewHandler::class
			        		]
			        	)
			        )
              );
```

is the equivalant to

```
$command = new MyCommand();
$handler = new MyNewHandler();

$response = $handler($command);
```

or using dependancy injection container

```
DI Config
[
	MiddlewareManagerInterface::class => MiddlewareManager::class,
	ContainerInterface::class => Container::class
	Handler::class => [
		[
			MyCommand::class => MyNewHandler::class
		]
	]
]

$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);
```

### Multiple Middlerware Handlers

[](#multiple-middlerware-handlers)

Using the middleware capablies we can easily add multiple command handlers, to process some commands differently from others.

In this example we add a Queueable Middleware Handler, that will look for commands that implement a *CommandQueueInterface::class*.

Theses commands will be routed to a message bus for processing on a background process, were as all other command will be handled by the final middleware handler.

```
$commandbus = new KodCube\CommandBus\CommandBus(
                new KodCube\CommandBus\MiddlewareManager(
                    [
                      KodCube\CommandBus\Middleware\Queue::class,
                      KodCube\CommandBus\Middleware\Handler::class
                    ],
                    new KodCube\DependencyInjection\Container(),
			      )
              );
```

or using dependancy injection container

```
DI Config
[
	MiddlewareManagerInterface::class => MiddlewareManager::class,
	MiddlewareManager::class => [
		Middleware\Queue::class,
		Middleware\Handler::class
	],
	ContainerInterface::class => Container::class,
	Handler::class => [
		[
			MyCommand::class => MyNewHandler::class
		]
	]
]

$commandbus = $di->get(KodCube\CommandBus\CommandBus::class);
```

###  Health Score

21

—

LowBetter than 19% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity8

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity41

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.

### Community

Maintainers

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

---

Top Contributors

[![srmiles](https://avatars.githubusercontent.com/u/1708004?v=4)](https://github.com/srmiles "srmiles (2 commits)")

---

Tags

command-busmiddlewarephp7

### Embed Badge

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

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

###  Alternatives

[ybazli/faker

Persian fake data package for laravel

7338.8k1](/packages/ybazli-faker)[hnassr/nova-key-value

A Laravel Nova field.

197.0k](/packages/hnassr-nova-key-value)

PHPackages © 2026

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