desmart/laravel-commandbus

DeSmart CommandBus for Laravel

1.1.1 2020-11-02 15:39 UTC

This package is not auto-updated.

Last update: 2024-09-11 23:18:48 UTC


README

一个轻量级、可插拔的命令总线。

安装

  1. $ composer require desmart/laravel-commandbus
  2. DeSmart\CommandBus\ServiceProvider 添加到 app.php

示例使用

命令类

class RegisterUserCommand
{
    protected $email;

    public function __construct($email)
    {
        $this->email = $email;
    }

    public function getEmail()
    {
        return $this->email;
    }
}

命令验证类

class RegisterUserCommandValidator
{
    public function validate(RegisterUserCommand $command)
    {
        // it will be called before handler
    }
}

命令处理器类

class RegisterUserCommandHandler
{
    public function handle(RegisterUserCommand $command)
    {
        // it will be called if validator won't throw any exception
    }
}

执行命令

class Controller
{
    /**
     * @var \DeSmart\CommandBus\Contracts\CommandBus
     */
    protected $commandBus;

    public function __construct(\DeSmart\CommandBus\Contracts\CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }

    public function index()
    {
        $command = new RegisterUserCommand("foo@bar.net");
        $this->commandBus->handle($command);
    }
}