承接 stixx/openapi-command-bundle 相关项目开发

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

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

stixx/openapi-command-bundle

Composer 安装命令:

composer require stixx/openapi-command-bundle

包简介

Create Command-Bus APIs with auto-generated OpenAPI docs and schema-driven request validation

README 文档

README

Latest Version on Packagist Total Downloads License

The OpenAPI Command Bundle is a Symfony bundle that allows you to build HTTP APIs around Command Bus messages (DTOs) without the need for manual controller creation or Symfony #[Route] attributes on your commands.

By using standard OpenAPI operation attributes (from zircote/swagger-php) directly on your command DTOs, this bundle automatically generates Symfony routes and handles the entire request-to-command lifecycle: deserialization, validation, dispatching to the messenger bus, and responding.

Key Features

  • OpenAPI-Driven Routing: Define your API endpoints directly on your command DTOs using #[OA\Post], #[OA\Get], #[OA\Put], etc.
  • No Manual Controllers: A single CommandController handles all generated routes by default.
  • Automatic Deserialization: Automatically maps JSON request bodies, route parameters, and query parameters to your command DTOs.
  • Two-Layer Validation: Each request is checked against the OpenAPI schema (headers, query parameters, body shape via league/openapi-psr7-validator) and the deserialized command is validated with Symfony Validator constraints before it reaches your handler.
  • Messenger Integration: Dispatches your commands directly to the Symfony Messenger bus.
  • Auto-Generated Documentation: Seamlessly integrates with NelmioApiDocBundle to include your command-based routes in your OpenAPI/Swagger documentation.
  • Problem Details Support: Returns RFC 7807 compliant error responses for validation and mapping errors.

Installation

1. Install via Composer

composer require stixx/openapi-command-bundle

2. Enable the Bundle

If you are using Symfony Flex, the bundle will be automatically enabled. Otherwise, add it to your config/bundles.php:

return [
    // ...
    Stixx\OpenApiCommandBundle\StixxOpenApiCommandBundle::class => ['all' => true],
];

Usage

1. Create a Command DTO

Annotate your command with OpenAPI attributes. No Symfony #[Route] is needed.

namespace App\Command;

use OpenApi\Attributes as OA;
use Symfony\Component\Validator\Constraints as Assert;

#[OA\Post(
    path: '/api/projects',
    operationId: 'create_project',
    summary: 'Create a new project'
)]
final class CreateProjectCommand
{
    public function __construct(
        #[Assert\NotBlank]
        #[Assert\Length(min: 3, max: 50)]
        public string $name,

        #[Assert\Length(max: 255)]
        public ?string $description = null,
    ) {}
}

2. Create a Message Handler

Implement a standard Symfony Messenger handler for your command.

namespace App\Handler;

use App\Command\CreateProjectCommand;
use Symfony\Component\Messenger\Attribute\AsMessageHandler;

#[AsMessageHandler]
final class CreateProjectHandler
{
    public function __invoke(CreateProjectCommand $command): array
    {
        // Your business logic here (e.g., persist to database)
        
        return [
            'id' => '123',
            'name' => $command->name,
        ];
    }
}

3. Call the API

The bundle automatically registers the route /api/projects (POST).

curl -X POST http://localhost:3000/api/projects \
     -H "Content-Type: application/json" \
     -d '{"name": "New Project", "description": "This is a project description"}'

The bundle will:

  1. Detect the route and map it to CreateProjectCommand.
  2. Deserialize the JSON body into the command object.
  3. Validate the command using Symfony Validator.
  4. Dispatch the command to the Messenger bus.
  5. Return the handler's result as a JSON response with an appropriate status code (e.g., 201 Created).

Configuration

You can customize the bundle's behavior in config/packages/stixx_openapi_command.yaml:

stixx_openapi_command:
    validation:
        enabled: true
        groups: ['Default']
    cache_control: 'no-store' # Any valid Cache-Control directives, or null to disable
    openapi:
        problem_details: true  # Enable RFC 7807 problem details for errors

Documentation

For more detailed information, please refer to the following documentation:

Stability & supported API

The bundle distinguishes a small public API surface from its internal implementation. Public surface is the contract you can safely depend on; internals can change in any release.

Public (@api) — guaranteed BC across minor releases:

Type What it is
Stixx\OpenApiCommandBundle\StixxOpenApiCommandBundle Bundle entry point
Stixx\OpenApiCommandBundle\Attribute\CommandObject Marker attribute for command arguments
Stixx\OpenApiCommandBundle\Exception\ApiProblemException Throw this from your code to express RFC 7807 problems
Stixx\OpenApiCommandBundle\Exception\ExceptionToApiProblemTransformerInterface Replace to customize how exceptions become problem responses
Stixx\OpenApiCommandBundle\Responder\ResponderInterface Implement to handle custom result shapes (CSV, XML, …)
Stixx\OpenApiCommandBundle\Response\StatusResolverInterface Replace to customize status code resolution
Stixx\OpenApiCommandBundle\Validator\ValidatorInterface Implement to add custom request-level validation
Stixx\OpenApiCommandBundle\Model\ProblemDetails OpenAPI schema model — reference from your annotations
Stixx\OpenApiCommandBundle\Model\ProblemDetailsInvalidRequestBody OpenAPI schema model — reference from your annotations
Stixx\OpenApiCommandBundle\Model\Violation OpenAPI schema model — reference from your annotations

Internal (@internal) — everything else, including default implementations like JsonResponder, DefaultExceptionToApiProblemTransformer, RequestValidator, the controllers, DI extension, compiler passes, route loaders, and event subscribers. Do not extend or rely on these directly; replace them through the @api interfaces above.

The bundle's configuration schema (the keys under stixx_openapi_command:) is also part of the supported API.

See Extension Points for a worked example of each extension interface.

Requirements

  • PHP 8.4+
  • Symfony 7.3+ or 8.0+

License

This bundle is released under the MIT License.

stixx/openapi-command-bundle 适用场景与选型建议

stixx/openapi-command-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.02k 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 11 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 2.02k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 3
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-26