定制 brenoroosevelt/oni-bus 二次开发

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

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

brenoroosevelt/oni-bus

Composer 安装命令:

composer require brenoroosevelt/oni-bus

包简介

Service Bus: Command Bus, Query Bus, Event Bus

README 文档

README

Build Scrutinizer Code Quality codecov Software License

Message Bus, Command Bus, Query Bus and Event Bus.

Features

  • Command Bus
  • Query Bus
  • Event Bus
  • Map Handlers using PHP 8 Attributes

Requirements

The following versions of PHP are supported: 7.1, 7.2, 7.3, 7.4, 8.0.

Install

$ composer require brenoroosevelt/oni-bus

Usage

Command Bus

<?php
use OniBus\Command\Command;   

class UserRegistrationCommand implements Command
{
    public $username;
    public $password;
}
<?php
use OniBus\Attributes\CommandHandler;

class UserRegistrationHandler
{
    #[CommandHandler]
    public function register(UserRegistrationCommand $userRegistration)
    {
        /* ... */
        return new UserDTO(/* ... */);
    }
}
<?php
use OniBus\Attributes\CommandHandler;
use OniBus\Buses\DispatchToHandler;
use OniBus\Command\CommandBus;
use OniBus\Handler\Builder\Resolver;

$resolver = 
    Resolver::new(new MyPsr11Container())
        ->withHandlers([UserRegistrationHandler::class])
        ->mapByAttributes(CommandHandler::class);

$commandBus = new CommandBus(new DispatchToHandler($resolver));

$userDTO = $commandBus->dispatch(new UserRegistrationCommand('username', 'secret'));

Query Bus

<?php
use OniBus\Query\Query;   

class UserByStatus implements Query
{
    protected $status;
    
    public function __construct(string $status)
    {
           $this->status = $status;
    }
    
    public function getStatus(): string
    {
        return $this->status;
    }
}
<?php
use OniBus\Attributes\QueryHandler;

class FindUserByStatusSQL
{
    #[QueryHandler]
    public function fetch(UserByStatus $query)
    {
        return [/* ... */];
    }
}
<?php
use OniBus\Attributes\QueryHandler;
use OniBus\Buses\DispatchToHandler;
use OniBus\Query\QueryBus;
use OniBus\Handler\Builder\Resolver;

$resolver = 
    Resolver::new(new MyPsr11Container())
        ->withHandlers([FindUserByStatusSQL::class])
        ->mapByAttributes(QueryHandler::class);

$queryBus = new QueryBus(new DispatchToHandler($resolver));

$users = $queryBus->dispatch(new UserByStatus('active'));

EventBus Bus

<?php
use OniBus\Event\Event;  

class UserCreatedEvent implements Event
{
    protected $username;
    
    public function __construct(string $username)
    {
           $this->username = $username;
    }
    
    public function getUsername(): string
    {
        return $this->username;
    }
}
<?php
use OniBus\Attributes\EventListener;

class CreateUserProfile
{
    #[EventListener]
    public function createProfile(UserCreatedEvent $event)
    {
        /* ... */
    }
}
<?php
use OniBus\Attributes\EventListener;
use OniBus\Buses\DispatchToHandler;
use OniBus\Event\EventBus;
use OniBus\Handler\Builder\Resolver;

$resolver = 
    Resolver::new(new MyPsr11Container())
        ->withHandlers([CreateUserProfile::class])
        ->mapByAttributes(EventListener::class);

$eventBus = new EventBus(new DispatchToHandler($resolver));

$eventBus->dispatch(new UserCreatedEvent('username'));

Bus Chain

<?php

/* CONTAINER */
$container = new MyPsr11Container();

/* EVENT BUS */
$eventResolver = 
    Resolver::new($container)
        ->withHandlers([CreateUserProfile::class])
        ->mapByAttributes(EventListener::class);
       
$eventBus = new EventBus(new DispatchToHandler($eventResolver));

/* COMMAND BUS */
$commandResolver = 
    Resolver::new($container)
        ->withHandlers([UserRegistrationHandler::class])
        ->mapByAttributes(CommandHandler::class);

$commandBus = 
     new CommandBus(
        new EventsDispatcher(
            EventManager::eventProvider(),
            $eventBus,
        ),
        // new ProtectOrder(),
        // new Transactional(),
        new DispatchToHandler($commandResolver)
    );

Container Configuration

<?php
use OniBus\Command\CommandBus;

$container->add(CommandBus::class, $commandBus);

Controller

<?php

class UserRegistrationController
{
    protected $commandBus;
    
    public function __construct(CommandBus $commandBus) 
    {
        $this->commandBus = $commandBus;    
    }

    public function __invoke(ServerRequestInterface $request, ResponseInterface $response, array $args = []): ResponseInterface
    {
        $body = $request->getParsedBody();
        $command = new UserRegistrationCommand($body['username'], $body['password']);
        $user = $this->commandBus->dispatch($command);
        $response->getBody()->write($user);
        return $response;
    }

Contributing

Please read the Contributing guide to learn about contributing to this project.

License

This project is licensed under the terms of the MIT license. See the LICENSE file for license rights and limitations.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-03-29

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固