Skip to content

Repository files navigation

SomeWork CQRS Bundle

CIcodecovPHPStan Level 8License: MITLatest VersionDownloads

A Symfony bundle that wires Command, Query, and Event buses on top of Symfony Messenger. It auto-discovers handlers via PHP attributes, provides a configurable stamp pipeline, and ships with testing utilities and production-grade patterns.

Why this bundle?

Symfony Messenger is a powerful transport layer, but it leaves CQRS wiring as an exercise for the developer. This bundle fills the gap:

  • Auto-discovery -- Annotate handlers with #[AsCommandHandler], #[AsQueryHandler], or #[AsEventHandler] and they are registered automatically. No YAML tags, no manual wiring.
  • Stamp pipeline -- A composable StampDecider pipeline attaches retry policies, transport routing, serializer stamps, metadata, and dispatch-after-current-bus stamps per message type or per individual message class.
  • Type-safe buses -- Three dedicated buses (CommandBus, QueryBus, EventBus) with distinct semantics: commands support sync/async dispatch, queries always return a result, events are fire-and-forget with zero-to-many handlers.
  • Testing utilities -- FakeCommandBus, FakeQueryBus, FakeEventBus with assertDispatched(), assertNotDispatched(), and callback-based property assertions for fast, isolated unit tests.

Architecture

flowchart LR
A[Your Code] --> B[CommandBus / QueryBus / EventBus]
B --> C[DispatchModeDecider]
C --> D[StampsDecider Pipeline]
D --> E1[RetryPolicy]
D --> E2[Transport]
D --> E3[Serializer]
D --> E4[Metadata]
D --> E5[DispatchAfterCurrentBus]
D --> F[Symfony Messenger]
F --> G[Handler]
Loading

How does it compare?

CapabilityRaw MessengerCQRS BundleEcotone
Handler discoveryManual YAML tags or #[AsMessageHandler]#[AsCommandHandler] / #[AsQueryHandler] / #[AsEventHandler] with auto-discoveryAttribute-based with conventions
Type safetySingle MessageBusInterfaceSeparate CommandBus, QueryBus, EventBus with typed dispatch methodsSeparate gateway interfaces
Bus abstractionYou build itThree buses with sync/async routing, DispatchMode enumCommand/Query/Event buses built-in
Retry configurationPer-transport YAML onlyPer-message-class via RetryPolicy interface + resolver hierarchyPer-endpoint via attributes
Testing supportInMemoryTransportFakeBus implementations with assertDispatched() + callback assertionsTest support module
Async routingrouting YAML configDispatchMode + #[Asynchronous] attribute + per-message transport mappingAsync via polled endpoints
Stamp pipelineManual stamp attachmentComposable StampDecider pipeline with priority orderingInterceptors (before/after/around)
Event orderingNot built-inSequenceAware interface + AggregateSequenceStampBuilt-in aggregate versioning
Transactional outboxNot built-inOutboxStorage interface + DBAL implementationBuilt-in with Doctrine
Sagas / Process managersNot built-inNot built-inBuilt-in saga support
Event sourcingNot built-inNot built-inBuilt-in event sourcing
OpenTelemetryNot built-inBridge middleware with trace spansNot built-in
Learning curveLow (part of Symfony)Low (thin layer over Messenger)Moderate (own conventions)
DependenciesSymfony onlySymfony MessengerEcotone framework

Choose raw Messenger when your app has simple dispatch needs and you want zero additional dependencies. Choose this bundle when you want structured CQRS buses, per-message configuration, and testing utilities while staying close to Messenger. Choose Ecotone when you need sagas, event sourcing, or a full CQRS/ES framework.

Feature matrix

Core

  • CommandBus with sync/async dispatch and result extraction
  • QueryBus with single-handler validation and typed results
  • EventBus with zero-to-many handlers and fire-and-forget semantics
  • Attribute-based handler discovery (#[AsCommandHandler], #[AsQueryHandler], #[AsEventHandler])
  • Handler interfaces optional -- attributes alone are sufficient

Stamp Pipeline

  • Composable StampDecider system with priority ordering (@api -- extend it yourself)
  • Per-message retry policies via RetryPolicy interface
  • Per-message transport routing with TransportNamesStamp or SendMessageToTransportsStamp
  • Per-message serializer stamps
  • Per-message metadata stamps with correlation ID support
  • DispatchAfterCurrentBusStamp control per message

Patterns

  • Causation ID propagation across nested dispatches
  • Idempotency bridge (IdempotencyStamp to DeduplicateStamp)
  • Event ordering with SequenceAware and AggregateSequenceStamp
  • Rate limiting via Symfony Rate Limiter integration
  • Transactional outbox with DBAL storage and relay command

Developer Experience

  • FakeCommandBus, FakeQueryBus, FakeEventBus for unit testing
  • assertDispatched() / assertNotDispatched() with callback-based property assertions
  • somework:cqrs:generate scaffold command for messages and handlers
  • somework:cqrs:list handler catalogue
  • somework:cqrs:debug-transports transport diagnostics
  • somework:cqrs:health-check for monitoring

Observability

  • OpenTelemetry bridge middleware (trace spans for dispatch and handling)
  • PSR-3 structured logging across buses, deciders, and resolvers

Integration

  • Symfony Flex recipe for zero-touch installation
  • CommandBusInterface, QueryBusInterface, EventBusInterface for DI and testing
  • #[Asynchronous] attribute for transport routing without YAML config

Installation

Requirements

  • PHP 8.2 or newer.
  • Symfony 7.2 or newer.

With Symfony Flex (recommended)

composer require somework/cqrs-bundle

Flex automatically registers the bundle in config/bundles.php and creates a commented config/packages/somework_cqrs.yaml with all available options.

Without Symfony Flex

Install the bundle via Composer:

composer require somework/cqrs-bundle

Then register it manually in config/bundles.php:

return [
// ...SomeWork\CqrsBundle\SomeWorkCqrsBundle::class => ['all' => true],
];

Create config/packages/somework_cqrs.yaml (see docs/flex-recipe/ for a template with all available options).

Verify the installation

Run the bundled console tooling to verify the bundle is registered:

bin/console somework:cqrs:list

Flex Recipe: The recipe files are in docs/flex-recipe/ and are pending submission to symfony/recipes-contrib. Until published, manual bundle registration is required.

Quick start

Step 1 -- Define a command message

namespaceApp\Application\Command;
useSomeWork\CqrsBundle\Contract\Command;
finalclass CreateTask implements Command
{
publicfunction__construct(
publicreadonlystring$id,
publicreadonlystring$name,
) {}
}

Step 2 -- Create the handler

namespaceApp\Application\Command;
useSomeWork\CqrsBundle\Attribute\AsCommandHandler;
useSomeWork\CqrsBundle\Contract\CommandHandler;
#[AsCommandHandler(command: CreateTask::class)]
finalclass CreateTaskHandler implements CommandHandler
{
publicfunction__invoke(CreateTask$command): mixed
{
// Save task to database...returnnull;
}
}

Step 3 -- Inject the bus and dispatch

namespaceApp\Controller;
useApp\Application\Command\CreateTask;
useSomeWork\CqrsBundle\Contract\CommandBusInterface;
useSymfony\Component\HttpFoundation\JsonResponse;
useSymfony\Component\HttpFoundation\Request;
useSymfony\Component\Routing\Attribute\Route;
finalclass TaskController
{
#[Route('/tasks', methods: ['POST'])]
publicfunctioncreate(Request$request, CommandBusInterface$commandBus): JsonResponse
{
$data = $request->toArray();
$commandBus->dispatch(newCreateTask(
id: uuid_create(),
name: $data['name'],
));
returnnewJsonResponse(['status' => 'ok'], 201);
}
}

Documentation

Full documentation is available at somework.github.io/cqrs.

Advanced topics

License

MIT. See LICENSE.

About

Symfony bundle wiring Command, Query and Event buses on top of Symfony Messenger. Attribute-based handler discovery, configurable stamp pipeline, PHPStan level 8.

Resources

Contributing

Security policy

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages