承接 somework/cqrs-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

somework/cqrs-bundle

Composer 安装命令:

composer require somework/cqrs-bundle

包简介

CQRS utilities for Symfony Messenger integration.

README 文档

README

CI codecov PHPStan Level 8 License: MIT Latest Version Downloads

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?

Capability Raw Messenger CQRS Bundle Ecotone
Handler discovery Manual YAML tags or #[AsMessageHandler] #[AsCommandHandler] / #[AsQueryHandler] / #[AsEventHandler] with auto-discovery Attribute-based with conventions
Type safety Single MessageBusInterface Separate CommandBus, QueryBus, EventBus with typed dispatch methods Separate gateway interfaces
Bus abstraction You build it Three buses with sync/async routing, DispatchMode enum Command/Query/Event buses built-in
Retry configuration Per-transport YAML only Per-message-class via RetryPolicy interface + resolver hierarchy Per-endpoint via attributes
Testing support InMemoryTransport FakeBus implementations with assertDispatched() + callback assertions Test support module
Async routing routing YAML config DispatchMode + #[Asynchronous] attribute + per-message transport mapping Async via polled endpoints
Stamp pipeline Manual stamp attachment Composable StampDecider pipeline with priority ordering Interceptors (before/after/around)
Event ordering Not built-in SequenceAware interface + AggregateSequenceStamp Built-in aggregate versioning
Transactional outbox Not built-in OutboxStorage interface + DBAL implementation Built-in with Doctrine
Sagas / Process managers Not built-in Not built-in Built-in saga support
Event sourcing Not built-in Not built-in Built-in event sourcing
OpenTelemetry Not built-in Bridge middleware with trace spans Not built-in
Learning curve Low (part of Symfony) Low (thin layer over Messenger) Moderate (own conventions)
Dependencies Symfony only Symfony Messenger Ecotone 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

namespace App\Application\Command;

use SomeWork\CqrsBundle\Contract\Command;

final class CreateTask implements Command
{
    public function __construct(
        public readonly string $id,
        public readonly string $name,
    ) {}
}

Step 2 -- Create the handler

namespace App\Application\Command;

use SomeWork\CqrsBundle\Attribute\AsCommandHandler;
use SomeWork\CqrsBundle\Contract\CommandHandler;

#[AsCommandHandler(command: CreateTask::class)]
final class CreateTaskHandler implements CommandHandler
{
    public function __invoke(CreateTask $command): mixed
    {
        // Save task to database...
        return null;
    }
}

Step 3 -- Inject the bus and dispatch

namespace App\Controller;

use App\Application\Command\CreateTask;
use SomeWork\CqrsBundle\Contract\CommandBusInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Attribute\Route;

final class TaskController
{
    #[Route('/tasks', methods: ['POST'])]
    public function create(Request $request, CommandBusInterface $commandBus): JsonResponse
    {
        $data = $request->toArray();

        $commandBus->dispatch(new CreateTask(
            id: uuid_create(),
            name: $data['name'],
        ));

        return new JsonResponse(['status' => 'ok'], 201);
    }
}

Documentation

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

Advanced topics

License

MIT. See LICENSE.

somework/cqrs-bundle 适用场景与选型建议

somework/cqrs-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 9 次下载、GitHub Stars 达 0, 最近一次更新时间为 2025 年 10 月 05 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「ddd」 「event-bus」 「Messenger」 「cqrs」 「command-bus」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 somework/cqrs-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 somework/cqrs-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 somework/cqrs-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 9
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 12
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-10-05