gmizera/service-bus-lite-bundle

ServiceBus bundle 实现已更新以支持 Symfomy 4

1.0.3 2021-11-06 16:28 UTC

This package is auto-updated.

Last update: 2024-09-06 23:01:48 UTC


README

已更新以支持 Symfony 4.4

通过包集成的 Service Bus,使其能够通过包集成到 Symfony2 中。

使用 Composer 安装

可以通过在您的 composer.json 文件的 require 部分添加以下内容来安装此包

    "require": {
        ...
        "psamatt/service-bus-lite-bundle": "*"
    },

然后,在 AppKernel 中启用此包

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new Psamatt\ServiceBusLiteBundle\PsamattServiceBusLiteBundle(),
    );
}

如何使用

创建实现 ServiceBus\IQueryHandlerServiceBus\ICommandHandler 的查询或命令处理器,例如

use \ServiceBus\ICommand;

class FooCommandHandler implements \ServiceBus\ICommandHandler
{
    function handle(ICommand $command)
    {
        
    }
}

然后,将处理器注册为服务,并在 services.yml 中标记为 ServiceBus 处理器

services:
    # Command Handler
    foo.command.handler:
        class: Acme\HelloBundle\CommandHandler\FooCommandHandler
        tags:
            -  { name: servicebus.command_handler }

    # Query Handler 
    foo.query.handler:
        class: Acme\HelloBundle\QueryHandler\FooQueryHandler
        tags:
            -  { name: servicebus.query_handler }

将此服务标记为命令或查询处理器将允许服务总线将此类注册为即将到来的命令的等待处理器。

现在,您只需在控制器中初始化一个 FooCommand 并发送到 ServiceBus 即可。

class FooController extends Controller
{
    
    public function indexAction()
    {
        // we send Commands
        $this->get('servicebus.mediator')->send(new FooCommand('myEventName'));
        
        // we request Queries
        $response = $this->get('servicebus.mediator')->request(new FooQuery('myEventName'));
    }

在后台,ServiceBus 将找到关联的处理器,您将在那里编写执行该特定操作所需的逻辑。

更详细的代码示例可以在 主存储库的示例文件夹中找到

注意:对于 ServiceBus 来说,您的 CommandCommandHandler 类名称中必须包含 Command 和 CommandHandler,以便找到与提出的 Command 相关的 CommandHandler。