Skip to content

php-cmd/cmd-bus

Repository files navigation

cmd-bus Documentation

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.

About

Command Bus for Mezzio applications

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages