承接 communitales/command-bus 相关项目开发

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

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

communitales/command-bus

最新稳定版本:2.2.0

Composer 安装命令:

composer require communitales/command-bus

包简介

Communitales Command Bus Component

README 文档

README

Decouple applications with a synchronous command bus.

Setup

composer require communitales/command-bus

Setup for Symfony in services.yaml:

services:

    _instanceof:
        Communitales\Component\CommandBus\CommandBusAwareInterface:
            calls:
                - [setCommandBus, ['@Communitales\Component\CommandBus\CommandBus']]

        Communitales\Component\CommandBus\Handler\CommandHandlerInterface:
            tags: ['communitales.command_handler']


    Communitales\Component\CommandBus\CommandBus:
        arguments:
            - !tagged_iterator communitales.command_handler

Usage

Example of a command:


namespace App\Domain\Command\Customer;

use Communitales\Component\CommandBus\Command\CommandInterface;
use App\Entity\Customer;

readonly class CreateCustomerCommand implements CommandInterface
{
    public function __construct(public Customer $customer)
    {
    }
}

Example of a command handler:


namespace App\Domain\Handler\Customer;

use App\Domain\Command\Customer\CreateCustomerCommand;
use App\Domain\Command\Customer\DeleteCustomerCommand;
use App\Domain\Command\Customer\UpdateCustomerCommand;
use App\Repository\CustomerRepository;
use Communitales\Component\CommandBus\Command\CommandInterface;
use Communitales\Component\CommandBus\Handler\CommandHandlerInterface;
use Communitales\Component\CommandBus\Handler\CommandHandlerTrait;
use Communitales\Component\CommandBus\Handler\Result\CommandHandlerResultInterface;
use Communitales\Component\CommandBus\Handler\Result\SuccessResult;
use Communitales\Component\StatusBus\StatusMessage;
use Override;
use Symfony\Component\Translation\TranslatableMessage;

use function sprintf;

class CustomerCommandHandler implements CommandHandlerInterface
{
    use CommandHandlerTrait;

    public function __construct(private readonly CustomerRepository $customerRepository) {
    }

    #[Override]
    public function canHandle(CommandInterface $command): bool
    {
        return $command instanceof CreateCustomerCommand
            || $command instanceof UpdateCustomerCommand
            || $command instanceof DeleteCustomerCommand;
    }

    private function createCustomer(CreateCustomerCommand $command): CommandHandlerResultInterface
    {
        $customer = $command->customer;

        $this->customerRepository->save($customer);

        return new SuccessResult(
            StatusMessage::createSuccessMessage(
                new TranslatableMessage(
                    'domain_customer.result_created', ['name' => $customer->getName()]
                )
            )
        );
    }

    private function updateCustomer(UpdateCustomerCommand $command): CommandHandlerResultInterface
    {
        // ...
    }

    private function deleteCustomer(DeleteCustomerCommand $command): CommandHandlerResultInterface
    {
        // ...
    }
}

统计信息

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

GitHub 信息

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

其他信息

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