PHPackages                             walnut/lib\_actionbus - 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. walnut/lib\_actionbus

ActiveLibrary

walnut/lib\_actionbus
=====================

157PHP

Since May 13Pushed 4y ago1 watchersCompare

[ Source](https://github.com/kapitancho/walnut-lib-actionbus)[ Packagist](https://packagist.org/packages/walnut/lib_actionbus)[ RSS](/packages/walnut-lib-actionbus/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

PHP Action Bus
==============

[](#php-action-bus)

This is library that can be used as a Command Bus, Query Bus or any other Bus type.

Examples
--------

[](#examples)

In the examples below the following classes are used:

- For adding a product:

```
/**
 * @implements Action
 */
final class AddProductAction implements Action {
   public function __construct(
      public /*readonly*/ string $productId,
      public /*readonly*/ int $amount
   ) {}
}

/**
 * @implements ActionHandler
 */
final class AddProductActionHandler implements ActionHandler {
   /**
    * @param AddProductAction $action
    * @return bool
    */
   public function execute(Action $action): bool {
      return true; //@TODO - implement
   }
}
```

- For retrieving a product:

```
final class ProductData {
   public function __construct(
      public /*readonly*/ string $productId,
      public /*readonly*/ string $productName,
      public /*readonly*/ int $amount
   ) {}
}

/**
 * @implements Action
 */
final class GetProductAction implements Action {
   public function __construct(
      public /*readonly*/ string $productId
   ) {}
}

/**
 * @implements ActionHandler
 */
final class GetProductActionHandler implements ActionHandler {
   /**
    * @param GetProductAction $action
    * @return bool
    */
   public function execute(Action $action): ProductData {
      return new ProductData('p1', 'Test Product', 10); //@TODO - implement
   }
}
```

### Basic Example (Explicit Mapping)

[](#basic-example-explicit-mapping)

```
use Walnut\Lib\ActionBus\ActionBus;
use Walnut\Lib\ActionBus\Handler\DefaultActionHandler;
use Walnut\Lib\ActionBus\Handler\Mapper\ArrayActionHandlerMapper;
use Walnut\Lib\ActionBus\Handler\Loader\ContainerActionHandlerLoader;

$defaultActionHandler = new DefaultActionHandler(
    new ArrayActionHandlerMapper([
        AddProductAction::class => AddProductActionHandler::class,
        GetProductAction::class => GetProductActionHandler::class,
    ]),
    new ContainerActionHandlerLoader(getPsrContainer())
);

$actionBus = new ActionBus($defaultActionHandler);
$result = $actionBus->execute(new AddProductAction('p2', 5));

$productName = $actionBus->execute(new GetProductAction('p2'))->productName;
```

### Basic Example (Implicit Mapping)

[](#basic-example-implicit-mapping)

```
use Walnut\Lib\ActionBus\ActionBus;
use Walnut\Lib\ActionBus\Handler\DefaultActionHandler;
use Walnut\Lib\ActionBus\Handler\Mapper\NameActionHandlerMapper;
use Walnut\Lib\ActionBus\Handler\Loader\ContainerActionHandlerLoader;

$defaultActionHandler = new DefaultActionHandler(
    new NameActionHandlerMapper(),
    new ContainerActionHandlerLoader(getPsrContainer())
);

$actionBus = new ActionBus($defaultActionHandler);
$result = $actionBus->execute(new AddProductAction('p2', 5));

$productName = $actionBus->execute(new GetProductAction('p2'))->productName;
```

### Using middlewares

[](#using-middlewares)

```
use Walnut\Lib\ActionBus\Handler\DefaultActionHandler;
use Walnut\Lib\ActionBus\Handler\Mapper\NameActionHandlerMapper;
use Walnut\Lib\ActionBus\Handler\Loader\ContainerActionHandlerLoader;

$defaultActionHandler = new DefaultActionHandler(
    new NameActionHandlerMapper(),
    new ContainerActionHandlerLoader(getPsrContainer())
);

final class TransactionMiddleware implements ActionHandlerMiddleware {

   public function __construct(
      private /*readonly*/ TransactionContext $transactionContext
   ) {}

   /**
    * @throws Exception
    */
   public function process(Action $action, ActionHandler $actionHandler): mixed {
      try {
         $result = $actionHandler->execute($action);
         $this->transactionContext->saveChanges();
         return $result;
      } catch (Exception $e) {
         $this->transactionContext->revertChanges();
         throw $e;
      }
   }
}

final class ErrorLoggerMiddleware implements ActionHandlerMiddleware {

   public function __construct(
      private /*readonly*/ LoggerInterface $logger
   ) {}

   /**
    * @throws Throwable
    */
   public function process(Action $action, ActionHandler $actionHandler): mixed {
      try {
         return $actionHandler->execute($action);
      } catch (Throwable $e) {
         $this->logger->error($e);
         throw $e;
      }
   }
}

$actionBus = new ActionBus($defaultActionHandler, [
   new TransactionMiddleware(getTransactionContext()),
   new ErrorLoggerMiddleware(getPsrLogger())
]);
$result = $actionBus->execute(new AddProductAction('p2', 5));
```

###  Health Score

17

—

LowBetter than 6% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity26

Early-stage or recently created project

 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/12d2910601119ce65c373b7e21ffc647154bde10c5ad0a0598ab1143aa851701?d=identicon)[kapitancho](/maintainers/kapitancho)

---

Top Contributors

[![kapitancho](https://avatars.githubusercontent.com/u/792682?v=4)](https://github.com/kapitancho "kapitancho (7 commits)")

### Embed Badge

![Health badge](/badges/walnut-lib-actionbus/health.svg)

```
[![Health](https://phpackages.com/badges/walnut-lib-actionbus/health.svg)](https://phpackages.com/packages/walnut-lib-actionbus)
```

PHPackages © 2026

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