API Reference
Complete API documentation for all classes and interfaces in the message-bus package.
CommandBus
Section titled “CommandBus”Pipeline-based synchronous command bus.
Methods
Section titled “Methods”dispatch(object $command): mixed
Section titled “dispatch(object $command): mixed”Dispatch a command synchronously through the middleware pipeline.
$result = $commandBus->dispatch(new CreateUserCommand(...));middleware(array|string|callable|object $middleware): self
Section titled “middleware(array|string|callable|object $middleware): self”Append middleware to this bus instance. Persists across dispatches.
$commandBus->middleware(LoggingMiddleware::class);$commandBus->middleware([FirstMiddleware::class, SecondMiddleware::class]);$commandBus->middleware(fn ($cmd, $next) => $next($cmd));withMiddleware(array|string|callable|object $middleware): self
Section titled “withMiddleware(array|string|callable|object $middleware): self”Clone the bus with extra middleware for the next dispatch only.
$result = $commandBus ->withMiddleware(TransactionMiddleware::class) ->dispatch($command);QueryBus
Section titled “QueryBus”Pipeline-based synchronous query bus.
Methods
Section titled “Methods”ask(object $query): mixed
Section titled “ask(object $query): mixed”Execute a query synchronously through the middleware pipeline.
$result = $queryBus->ask(new GetUserByIdQuery(id: 123));middleware(array|string|callable|object $middleware): self
Section titled “middleware(array|string|callable|object $middleware): self”Append middleware to this bus instance.
$queryBus->middleware(CacheMiddleware::class);withMiddleware(array|string|callable|object $middleware): self
Section titled “withMiddleware(array|string|callable|object $middleware): self”Clone the bus with extra middleware for the next query only.
$result = $queryBus ->withMiddleware(new LoggingMiddleware($logger)) ->ask($query);Facades
Section titled “Facades”CommandBus Facade
Section titled “CommandBus Facade”use Cline\MessageBus\Facades\CommandBus;
CommandBus::dispatch($command);CommandBus::middleware($middleware);CommandBus::withMiddleware($middleware);QueryBus Facade
Section titled “QueryBus Facade”use Cline\MessageBus\Facades\QueryBus;
QueryBus::ask($query);QueryBus::middleware($middleware);QueryBus::withMiddleware($middleware);Interfaces
Section titled “Interfaces”CommandBusInterface
Section titled “CommandBusInterface”namespace Cline\MessageBus\Commands\Contracts;
interface CommandBusInterface{ public function dispatch(object $command): mixed;
public function middleware(array|string|callable|object $middleware): self;
public function withMiddleware(array|string|callable|object $middleware): self;}QueryBusInterface
Section titled “QueryBusInterface”namespace Cline\MessageBus\Queries\Contracts;
interface QueryBusInterface{ public function ask(object $query): mixed;
public function middleware(array|string|callable|object $middleware): self;
public function withMiddleware(array|string|callable|object $middleware): self;}BusMiddlewareInterface
Section titled “BusMiddlewareInterface”namespace Cline\MessageBus\Contracts;
interface BusMiddlewareInterface{ public function handle(object $message, Closure $next): mixed;}Attributes
Section titled “Attributes”AsCommandHandler
Section titled “AsCommandHandler”namespace Cline\MessageBus\Commands\Attributes;
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]final readonly class AsCommandHandler{ public function __construct( public string $command, // class-string of the command ) {}}AsQueryHandler
Section titled “AsQueryHandler”namespace Cline\MessageBus\Queries\Attributes;
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD | Attribute::IS_REPEATABLE)]final readonly class AsQueryHandler{ public function __construct( public string $query, // class-string of the query ) {}}Abstract Classes
Section titled “Abstract Classes”AbstractCommand
Section titled “AbstractCommand”Base class for command DTOs:
namespace Cline\MessageBus\Commands\Support;
abstract readonly class AbstractCommand {}AbstractQuery
Section titled “AbstractQuery”Base class for query DTOs:
namespace Cline\MessageBus\Queries\Support;
abstract readonly class AbstractQuery {}HandlerDiscovery
Section titled “HandlerDiscovery”Static utility for discovering handlers:
namespace Cline\MessageBus\Discovery;
final class HandlerDiscovery{ /** * @return array{commands: array<class-string, string>, queries: array<class-string, string>} */ public static function discover(): array;}Returns associative arrays mapping message classes to handler references.
LogExecutionTimeMiddleware
Section titled “LogExecutionTimeMiddleware”Built-in middleware for performance logging:
namespace Cline\MessageBus\Middleware;
final readonly class LogExecutionTimeMiddleware implements BusMiddlewareInterface{ public function __construct( private LoggerInterface $logger, ) {}
public function handle(object $message, Closure $next): mixed;}Logs at debug level with message class and elapsed milliseconds.