php-cmd/cmd-bus

CommandBus for Mezzio

Installs: 300

Dependents: 0

Suggesters: 0

Security: 0

Stars: 2

Watchers: 0

Forks: 1

Open Issues: 1

pkg:composer/php-cmd/cmd-bus

0.3.0 2025-10-13 21:20 UTC

This package is auto-updated.

Last update: 2025-11-21 06:03:22 UTC


README

A Command Bus implementation for Mezzio applications, providing a clean way to handle commands through a middleware pipeline.

Table of Contents

Core Components

Interfaces

Container Factories

Middleware

Handlers

Exceptions

Quick Start

Installation

composer require php-cmd/cmd-bus

Basic Configuration

// config/config.php
return [
    PhpCmd\CmdBus\ConfigProvider::class => [
        'command-map' => [
            App\Command\CreateUserCommand::class => App\Handler\CreateUserHandler::class,
        ],
        'middleware_pipeline' => [
            ['middleware' => \PhpCmd\CmdBus\Middleware\CommandHandlerMiddleware::class, 'priority' => 1],
        ],
    ],
];

Usage in Mezzio

// In a request handler or middleware
class UserHandler
{
    public function __construct(
        private \PhpCmd\CmdBus\CmdBusInterface $commandBus
    ) {}

    public function handle(ServerRequestInterface $request): ResponseInterface
    {
        $data = $request->getParsedBody();

        $command = new CreateUserCommand(
            email: $data['email'],
            username: $data['username']
        );

        $user = $this->commandBus->handle($command);

        return new JsonResponse(['user' => $user->toArray()]);
    }
}

Architecture Overview

The cmd-bus library follows these key principles:

  1. Commands are data objects that implement CommandInterface
  2. Handlers process commands and implement CommandHandlerInterface
  3. Middleware can intercept and potentially modify command processing
  4. Pipeline manages middleware execution order
  5. Factory classes integrate with Laminas ServiceManager/ Psr\Container\ContainerInterface

Contributing

See the project repository for contribution guidelines and development setup instructions.