定制 upgate/laravel-command-bus 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

upgate/laravel-command-bus

Composer 安装命令:

composer require upgate/laravel-command-bus

包简介

Laravel Command Bus

README 文档

README

Build Status

Setup

  1. composer require upgate/laravel-command-bus
  2. Register Upgate\LaravelCommandBus\CommandBusServiceProvider as a service provider
  3. In your service provider, bind Upgate\LaravelCommandBus\HandlerResolver to an implementation of your choice.

HandlerResolver binding examples:

a) PatternHandlerResolver:

$this->app->singleton(
    \Upgate\LaravelCommandBus\HandlerResolver::class,
    function () {
        return new \Upgate\LaravelCommandBus\PatternHandlerResolver(
            '\YourAppNamespace\CommandHandlers\%sHandler'
        );
    }
);

b) MapHandlerResolver:

use YourAppNamespace\Commands;
use YourAppNamespace\CommandHandlers;
// ...
$this->app->singleton(
    \Upgate\LaravelCommandBus\HandlerResolver::class,
    function () {
        return new \Upgate\LaravelCommandBus\MapHandlerResolver(
            [
                Commands\FooCommand::class => Handlers\FooHandler::class,
                Commands\BarCommand::class => Handlers\BarHandler::class,
                // ...
            ]
        );
    }
);

c) Bind your own implementation (must extend \Upgate\LaravelCommandBus\HandlerResolver).

Usage

Simplified example:

// Command
class SignUpCommand {

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

// Handler
class SignUpHandler {

    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    
    public function handle(SignUpCommand $command)
    {
        $user = User::signUp($command->email(), $command->password());
        $this->userRepository->store($user);
    }

}

// HTTP Controller
use Upgate\LaravelCommandBus\CommandBus;

class UserController {

    private $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }
    
    public function signUp(Request $request)
    {
        $this->commandBus->execute(new SignUpCommand(
            $request->get('email'),
            $request->get('password')
        ));
    }

}

// Console command
use Upgate\LaravelCommandBus\CommandBus;

class SignUpUserConsoleCommand
{

    private $commandBus;

    public function __construct(CommandBus $commandBus)
    {
        $this->commandBus = $commandBus;
    }
    
    public function handle()
    {
        $this->commandBus->execute(new SignUpCommand(
            $this->argument('email'),
            $this->argument('password')
        ));
    }
    
}

Of course, you might (and should) want to introduce Controller and ConsoleCommand abstract classes with executeCommand() methods implemented.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-2-Clause
  • 更新时间: 2017-10-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固